Skip to content

Commit 5cabef3

Browse files
committed
add test for notification handling being contravariant
1 parent a094a49 commit 5cabef3

File tree

7 files changed

+37
-7
lines changed

7 files changed

+37
-7
lines changed

projects/kediatr-core/src/test/kotlin/com/trendyol/kediatr/MediatorTests.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class MediatorTests : MediatorUseCases() {
7272
ConditionalPipelineBehavior(),
7373
TimingPipelineBehavior(),
7474
ComplexPipelineRequestHandler(),
75-
ComplexDataRequestHandler()
75+
ComplexDataRequestHandler(),
76+
CatchAllNotificationsHandler()
7677
)
7778
)
7879

projects/kediatr-core/src/testFixtures/kotlin/com/trendyol/kediatr/testing/MediatorUseCases.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ abstract class MediatorUseCases : MediatorTestConvention() {
109109
val command = TestCommandForInheritanceWithFallback.TestCommandInherited1("id")
110110
testMediator.send(command)
111111
command.invocationCount().get() shouldBe 1
112-
command.whereItWasInvokedFrom() shouldBe TestCommandForInheritanceWithFallbackHandlerHandler::class.java.name
112+
command.whereItWasInvokedFrom() shouldContain TestCommandForInheritanceWithFallbackHandlerHandler::class.java.name
113113

114114
val command2 = TestCommandForInheritanceWithFallback.TestCommandInherited2("id")
115115
testMediator.send(command2)
116116
command2.invocationCount().get() shouldBe 1
117-
command2.whereItWasInvokedFrom() shouldBe TestRequestHandlerForCommandInherited2::class.java.name
117+
command2.whereItWasInvokedFrom() shouldContain TestRequestHandlerForCommandInherited2::class.java.name
118118
}
119119

120120
@Test
@@ -601,4 +601,13 @@ abstract class MediatorUseCases : MediatorTestConvention() {
601601
result shouldBe 2
602602
request.invocationCount().get() shouldBe 1
603603
}
604+
605+
@Test
606+
fun notification_handling_is_contravariant_and_catch_all() = runTest {
607+
val notification = TestNotification()
608+
testMediator.publish(notification)
609+
notification.invocationCount().get() shouldBe 1
610+
notification.whereItWasInvokedFrom() shouldContain "CatchAllNotificationsHandler"
611+
notification.whereItWasInvokedFrom() shouldContain "TestNotificationHandler"
612+
}
604613
}

projects/kediatr-core/src/testFixtures/kotlin/com/trendyol/kediatr/testing/models.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ abstract class EnrichedWithMetadata {
2020

2121
fun invocationCount(): AtomicInteger = getMetadata(INVOCATION_COUNT) as? AtomicInteger ?: AtomicInteger(0)
2222

23-
fun whereItWasInvokedFrom(): String = getMetadata("invokedFrom") as? String ?: "unknown"
23+
fun whereItWasInvokedFrom(): List<String> = invokedPlaces()
2424

2525
fun invokedFrom(nameOfTheHandler: String) {
26-
addMetadata("invokedFrom", nameOfTheHandler)
26+
val invokedPlaces = invokedPlaces().toMutableList()
27+
invokedPlaces.add(nameOfTheHandler)
28+
addMetadata(INVOKED_FROM, invokedPlaces)
2729
}
2830

2931
internal fun visitedPipeline(pipeline: String) {
@@ -42,6 +44,8 @@ abstract class EnrichedWithMetadata {
4244

4345
fun orderedVisitedPipelines(): List<String> = getMetadata(ORDERED_VISITED_PIPELINES) as? List<String> ?: emptyList()
4446

47+
fun invokedPlaces(): List<String> = getMetadata(INVOKED_FROM) as? List<String> ?: emptyList()
48+
4549
internal fun recordExecutionTime(time: Long) {
4650
addMetadata("executionTime", time)
4751
}
@@ -66,6 +70,7 @@ abstract class EnrichedWithMetadata {
6670
private const val INVOCATION_COUNT = "invocationCount"
6771
private const val VISITED_PIPELINES = "visitedPipelines"
6872
private const val ORDERED_VISITED_PIPELINES = "orderedVisitedPipelines"
73+
private const val INVOKED_FROM = "invokedFrom"
6974
}
7075
}
7176

@@ -93,6 +98,7 @@ class TestNotificationHandler(
9398
override suspend fun handle(notification: TestNotification) {
9499
mediator shouldNotBe null
95100
notification.incrementInvocationCount()
101+
notification.invokedFrom("TestNotificationHandler")
96102
}
97103
}
98104

@@ -999,3 +1005,11 @@ class FirstHandlerForCommand : RequestHandler.Unit<CommandWithMultipleHandlers>
9991005
class SecondHandlerForCommand : RequestHandler.Unit<CommandWithMultipleHandlers> {
10001006
override suspend fun handle(request: CommandWithMultipleHandlers) = Unit
10011007
}
1008+
1009+
class CatchAllNotificationsHandler : NotificationHandler<Notification> {
1010+
override suspend fun handle(notification: Notification) {
1011+
when (notification) {
1012+
is EnrichedWithMetadata -> notification.invokedFrom("CatchAllNotificationsHandler")
1013+
}
1014+
}
1015+
}

projects/kediatr-koin-starter/src/test/kotlin/com/trendyol/kediatr/koin/MediatorTests.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class MediatorTests :
8686
single { SlowNotificationHandler2() }
8787
single { SlowNotificationHandler3() }
8888
single { SelfReferencingRequestHandler(get()) }
89+
single { CatchAllNotificationsHandler() }
8990

9091
// Extra
9192
single<MediatorAccessor> { { get<Mediator>() } }

projects/kediatr-quarkus-starter/src/test/kotlin/com/trendyol/kediatr/quarkus/MediatorTests.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,7 @@ class MediatorTests : MediatorUseCases() {
198198

199199
@Produces
200200
fun selfReferencingRequestHandler(mediator: Mediator) = SelfReferencingRequestHandler { mediator }
201+
202+
@Produces
203+
fun catchAllNotificationsHandler() = CatchAllNotificationsHandler()
201204
}

projects/kediatr-spring-boot-2x-starter/src/test/kotlin/com/trendyol/kediatr/spring/MediatorTests.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ import org.springframework.context.annotation.*
7171
SlowNotificationHandler1::class,
7272
SlowNotificationHandler2::class,
7373
SlowNotificationHandler3::class,
74-
SelfReferencingRequestHandler::class
74+
SelfReferencingRequestHandler::class,
75+
CatchAllNotificationsHandler::class
7576
]
7677
)
7778
class MediatorTests : MediatorUseCases() {

projects/kediatr-spring-boot-3x-starter/src/test/kotlin/com/trendyol/kediatr/spring/MediatorTests.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ import org.springframework.context.annotation.*
7171
SlowNotificationHandler1::class,
7272
SlowNotificationHandler2::class,
7373
SlowNotificationHandler3::class,
74-
SelfReferencingRequestHandler::class
74+
SelfReferencingRequestHandler::class,
75+
CatchAllNotificationsHandler::class
7576
]
7677
)
7778
class MediatorTests : MediatorUseCases() {

0 commit comments

Comments
 (0)