Skip to content

Commit

Permalink
Fix broken polymorphism
Browse files Browse the repository at this point in the history
  • Loading branch information
pomianowski committed Jun 1, 2024
1 parent a06c794 commit d963b55
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>3.0.0</Version>
<Version>3.0.1</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/ReflectionEventing/HashedConsumerTypesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public IEnumerable<Type> GetConsumerTypes(Type eventType)
if (consumedEventType == eventType)
{
yield return consumer.Key;

break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public IEnumerable<Type> GetConsumerTypes(Type eventType)
{
yield return consumer.Key;

continue;
break;
}

if (AreTypesRelated(consumedEventType, eventType))
Expand Down Expand Up @@ -56,6 +56,6 @@ public IEnumerable<Type> GetConsumerTypes(Type eventType)

private static bool AreTypesRelated(Type type1, Type type2)
{
return type1.IsAssignableFrom(type2) || type2.IsAssignableFrom(type1);
return type1.IsAssignableFrom(type2);
}
}
23 changes: 9 additions & 14 deletions tests/ReflectionEventing.UnitTests/EventBusBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public void AddConsumer_ShouldAddConsumerToDictionary()
eventBusBuilder.AddConsumer(consumerType);

IConsumerTypesProvider typesProvider = eventBusBuilder.BuildTypesProvider();
_ = typesProvider.GetConsumerTypes(typeof(TestEvent)).Should().Contain(consumerType);
_ = typesProvider.GetConsumerTypes(typeof(SecondaryEvent)).Should().Contain(consumerType);
_ = typesProvider.GetConsumerTypes(typeof(ITestEvent)).Should().Contain(consumerType);
_ = typesProvider.GetConsumerTypes(typeof(IBaseEvent)).Should().Contain(consumerType);
}

[Fact]
Expand All @@ -32,7 +32,7 @@ public void AddConsumer_ShouldNotReturnConsumersTypes_WithoutPolymorphismEnabled

IConsumerTypesProvider typesProvider = eventBusBuilder.BuildTypesProvider();
GenericCollectionAssertions<Type>? consumers = typesProvider
.GetConsumerTypes(typeof(ITestEvent))
.GetConsumerTypes(typeof(TestEvent))
.Should();
consumers.HaveCount(0);
}
Expand All @@ -49,7 +49,7 @@ public void AddConsumer_ShouldReturnConsumersTypes_WhenPolymorphismEnabled()
IConsumerTypesProvider typesProvider = eventBusBuilder.BuildTypesProvider();

GenericCollectionAssertions<Type>? consumers = typesProvider
.GetConsumerTypes(typeof(ITestEvent))
.GetConsumerTypes(typeof(TestEvent))
.Should();
consumers.HaveCount(1).And.Contain(consumerType);
}
Expand Down Expand Up @@ -81,29 +81,24 @@ public sealed record TestEvent : ITestEvent, IBaseEvent;

public sealed record SecondaryEvent : IBaseEvent;

public sealed record MySampleConsumer : IConsumer<TestEvent>, IConsumer<SecondaryEvent>
public sealed record MySampleConsumer : IConsumer<ITestEvent>, IConsumer<IBaseEvent>
{
public Task ConsumeAsync(TestEvent payload, CancellationToken cancellationToken)
public Task ConsumeAsync(ITestEvent payload, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}

/// <inheritdoc />
public Task ConsumeAsync(SecondaryEvent payload, CancellationToken cancellationToken)
public Task ConsumeAsync(IBaseEvent payload, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}

public sealed record MySecondarySampleConsumer : IConsumer<SecondaryEvent>
public sealed record MySecondarySampleConsumer : IConsumer<IBaseEvent>
{
public Task ConsumeAsync(TestEvent payload, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}

/// <inheritdoc />
public Task ConsumeAsync(SecondaryEvent payload, CancellationToken cancellationToken)
public Task ConsumeAsync(IBaseEvent payload, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
Expand Down

0 comments on commit d963b55

Please sign in to comment.