Skip to content

Commit afb9347

Browse files
committed
Improve tests for invocation count
1 parent 014ff50 commit afb9347

File tree

4 files changed

+87
-34
lines changed

4 files changed

+87
-34
lines changed

projects/build.gradle.kts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ kover {
99
filters {
1010
excludes {
1111
androidGeneratedClasses()
12-
this.packages("**generated**")
13-
this.classes("**generated**")
12+
packages("**generated**")
13+
classes("**generated**")
1414
}
1515
}
1616
}
@@ -23,6 +23,10 @@ subprojects {
2323

2424
kotlin {
2525
jvmToolchain(17)
26+
compilerOptions {
27+
freeCompilerArgs = listOf("-Xjsr305=strict")
28+
allWarningsAsErrors = true
29+
}
2630
}
2731

2832
dependencies {

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import com.trendyol.kediatr.HandlerNotFoundException
44
import io.kotest.assertions.throwables.shouldThrow
55
import io.kotest.matchers.shouldBe
66
import kotlinx.coroutines.test.runTest
7-
import org.junit.jupiter.api.*
7+
import org.junit.jupiter.api.Test
8+
import org.junit.jupiter.api.assertThrows
89

910
abstract class MediatorUseCases : MediatorDIConvention {
1011
@Test
@@ -26,8 +27,10 @@ abstract class MediatorUseCases : MediatorDIConvention {
2627
@Test
2728
fun commandWithResult() = runTest {
2829
val count = 0
29-
val result = testMediator.send(TestCommandWithResult(count))
30+
val command = TestCommandWithResult(count)
31+
val result = testMediator.send(command)
3032
result.value shouldBe count + 1
33+
command.invocationCount() shouldBe 1
3134
}
3235

3336
@Test
@@ -41,22 +44,29 @@ abstract class MediatorUseCases : MediatorDIConvention {
4144

4245
@Test
4346
fun notification() = runTest {
44-
testMediator.publish(TestNotification())
47+
val notification = TestNotification()
48+
testMediator.publish(notification)
49+
notification.invocationCount() shouldBe 1
4550
}
4651

4752
@Test
48-
fun `should process command with async pipeline`() = runTest {
49-
testMediator.send(TestPipelineCommand())
53+
fun pipeline() = runTest {
54+
val command = TestPipelineCommand()
55+
testMediator.send(command)
56+
command.visitedPipelines() shouldBe setOf(
57+
ExceptionPipelineBehavior::class.simpleName,
58+
LoggingPipelineBehavior::class.simpleName
59+
)
5060
}
5161

5262
@Test
53-
fun `should process exception in async handler`() = runTest {
63+
fun command_throws_exception() = runTest {
5464
val act = suspend { testMediator.send(TestBrokenCommand()) }
5565
assertThrows<Exception> { act() }
5666
}
5767

5868
@Test
59-
fun `should throw exception if given async query does not have handler bean`() = runTest {
69+
fun query_without_handler() = runTest {
6070
val exception = shouldThrow<HandlerNotFoundException> {
6171
testMediator.send(NonExistQuery())
6272
}
@@ -65,9 +75,8 @@ abstract class MediatorUseCases : MediatorDIConvention {
6575
}
6676

6777
@Test
68-
fun `should retrieve result from async query handler bean`() = runTest {
78+
fun query() = runTest {
6979
val result = testMediator.send(TestQuery(1))
70-
7180
result shouldBe "hello 1"
7281
}
7382
}
Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
@file:Suppress("UNCHECKED_CAST")
2+
13
package com.trendyol.kediatr.framewokUseCases
24

35
import com.trendyol.kediatr.*
46
import io.kotest.matchers.shouldNotBe
57

68
class TestNonExistCommand : Command
79

8-
class TestCommand : Command
10+
class TestCommand : Command, EnrichedWithMetadata()
911

1012
class TestCommandHandler(
1113
private val mediator: Mediator
1214
) : CommandHandler<TestCommand> {
1315
override suspend fun handle(command: TestCommand) {
1416
mediator shouldNotBe null
17+
command.incrementInvocationCount()
1518
}
1619
}
1720

@@ -21,33 +24,69 @@ class Result(
2124

2225
class NonExistCommandWithResult : CommandWithResult<Result>
2326

24-
data class TestCommandWithResult(val invoked: Int = 0) : CommandWithResult<Result>
27+
data class TestCommandWithResult(
28+
val invoked: Int = 0
29+
) : CommandWithResult<Result>, EnrichedWithMetadata()
2530

2631
class TestCommandWithResultCommandHandler(
2732
val mediator: Mediator
2833
) : CommandWithResultHandler<TestCommandWithResult, Result> {
29-
override suspend fun handle(command: TestCommandWithResult): Result = Result(command.invoked + 1)
34+
override suspend fun handle(
35+
command: TestCommandWithResult
36+
): Result = Result(command.invoked + 1).also { command.incrementInvocationCount() }
3037
}
3138

32-
class TestNotification : Notification
39+
class TestNotification : Notification, EnrichedWithMetadata()
3340

3441
class TestNotificationHandler(
3542
private val mediator: Mediator
3643
) : NotificationHandler<TestNotification> {
3744
override suspend fun handle(notification: TestNotification) {
3845
mediator shouldNotBe null
46+
notification.incrementInvocationCount()
3947
}
4048
}
4149

42-
class TestBrokenCommand : Command
50+
class TestBrokenCommand : Command, EnrichedWithMetadata()
51+
52+
class TestPipelineCommand : Command, EnrichedWithMetadata()
53+
54+
abstract class EnrichedWithMetadata {
55+
private val metadata = mutableMapOf<String, Any>()
56+
57+
internal fun incrementInvocationCount() {
58+
val invocationCount = invocationCount()
59+
addMetadata(INVOCATION_COUNT, invocationCount + 1)
60+
}
61+
62+
fun invocationCount(): Int = getMetadata(INVOCATION_COUNT) as? Int ?: 0
63+
64+
internal fun visitedPipeline(pipeline: String) {
65+
val visitedPipelines = visitedPipelines().toMutableSet()
66+
visitedPipelines.add(pipeline)
67+
addMetadata(VISITED_PIPELINES, visitedPipelines)
68+
}
69+
70+
fun visitedPipelines(): Set<String> = getMetadata(VISITED_PIPELINES) as? Set<String> ?: emptySet()
71+
72+
private fun addMetadata(key: String, value: Any) {
73+
metadata[key] = value
74+
}
75+
76+
private fun getMetadata(key: String): Any? = metadata[key]
4377

44-
class TestPipelineCommand : Command
78+
companion object {
79+
private const val INVOCATION_COUNT = "invocationCount"
80+
private const val VISITED_PIPELINES = "visitedPipelines"
81+
}
82+
}
4583

4684
class TestPipelineCommandHandler(
4785
val mediator: Mediator
4886
) : CommandHandler<TestPipelineCommand> {
4987
override suspend fun handle(command: TestPipelineCommand) {
5088
mediator shouldNotBe null
89+
command.incrementInvocationCount()
5190
}
5291
}
5392

@@ -56,6 +95,7 @@ class TestBrokenCommandHandler(
5695
) : CommandHandler<TestBrokenCommand> {
5796
override suspend fun handle(command: TestBrokenCommand) {
5897
mediator shouldNotBe null
98+
command.incrementInvocationCount()
5999
throw Exception()
60100
}
61101
}
@@ -64,14 +104,13 @@ class ExceptionPipelineBehavior : PipelineBehavior {
64104
override suspend fun <TRequest, TResponse> handle(
65105
request: TRequest,
66106
next: RequestHandlerDelegate<TRequest, TResponse>
67-
): TResponse {
68-
try {
69-
// exceptionPipelineBehaviorHandleCounter++
70-
return next(request)
71-
} catch (ex: Exception) {
72-
// exceptionPipelineBehaviorHandleCatchCounter++
73-
throw ex
107+
): TResponse = try {
108+
when (request) {
109+
is EnrichedWithMetadata -> request.visitedPipeline(this::class.java.simpleName)
74110
}
111+
next(request)
112+
} catch (ex: Exception) {
113+
throw ex
75114
}
76115
}
77116

@@ -80,22 +119,23 @@ class LoggingPipelineBehavior : PipelineBehavior {
80119
request: TRequest,
81120
next: RequestHandlerDelegate<TRequest, TResponse>
82121
): TResponse {
83-
// loggingPipelineBehaviorHandleBeforeNextCounter++
84-
val result = next(request)
85-
// loggingPipelineBehaviorHandleAfterNextCounter++
86-
return result
122+
when (request) {
123+
is EnrichedWithMetadata -> request.visitedPipeline(this::class.java.simpleName)
124+
}
125+
return next(request)
87126
}
88127
}
89128

90129
class NonExistQuery : Query<String>
91130

92-
class TestQuery(val id: Int) : Query<String>
131+
class TestQuery(val id: Int) : Query<String>, EnrichedWithMetadata()
93132

94133
class TestQueryHandler(
95134
private val mediator: Mediator
96135
) : QueryHandler<TestQuery, String> {
97136
override suspend fun handle(query: TestQuery): String {
98137
mediator shouldNotBe null
138+
query.incrementInvocationCount()
99139
return "hello " + query.id
100140
}
101141
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.trendyol.kediatr.quarkus
22

3-
import com.trendyol.kediatr.*
3+
import com.trendyol.kediatr.Mediator
44
import com.trendyol.kediatr.framewokUseCases.*
55
import io.quarkus.test.junit.QuarkusTest
66
import jakarta.enterprise.inject.Produces
@@ -29,10 +29,10 @@ class MediatorTests : MediatorUseCases() {
2929
fun notificationHandler(mediator: Mediator) = TestNotificationHandler(mediator)
3030

3131
@Produces
32-
fun pipelineBehaviors(): List<PipelineBehavior> = listOf(
33-
ExceptionPipelineBehavior(),
34-
LoggingPipelineBehavior()
35-
)
32+
fun pipeline1() = ExceptionPipelineBehavior()
33+
34+
@Produces
35+
fun pipeline2() = LoggingPipelineBehavior()
3636

3737
@Produces
3838
fun provideQueryHandler(mediator: Mediator) = TestQueryHandler(mediator)

0 commit comments

Comments
 (0)