generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CosmosSourceTaskTest.kt
210 lines (182 loc) · 8.11 KB
/
CosmosSourceTaskTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.okp4.connect.cosmos
import io.grpc.Status
import io.grpc.StatusException
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe
import io.mockk.*
import org.apache.kafka.connect.errors.ConnectException
import tendermint.types.BlockOuterClass
import tendermint.types.Types
class CosmosSourceTaskTest : BehaviorSpec({
val cosmosClient = mockk<CosmosServiceClient>()
val cosmosSourceTask = spyk(CosmosSourceTask(), recordPrivateCalls = true)
val props = mutableMapOf(
CosmosSourceConnector.TOPIC_CONFIG to "topic",
CosmosSourceConnector.NODE_ADDRESS_CONFIG to "localhost",
CosmosSourceConnector.NODE_PORT_CONFIG to "25568",
CosmosSourceConnector.CHAIN_ID_CONFIG to "okp4-testnet-1",
CosmosSourceConnector.MAX_POLL_LENGTH_CONFIG to "10",
CosmosSourceConnector.TLS_ENABLE_CONFIG to "false",
)
afterTest {
clearMocks(cosmosClient, cosmosSourceTask)
}
mockkObject(CosmosServiceClient)
coEvery {
CosmosServiceClient.with(any(), any(), any())
} returns cosmosClient
fun mockForPoll(offset: Int?, height: Int, failAt: Int?, closedAfter: Int?) {
cosmosSourceTask.initialize(
mockk {
every {
offsetStorageReader()
} returns mockk {
every {
offset(any() as Map<String, String>)
} answers {
offset?.let { mapOf(CosmosSourceTask.HEIGHT_FIELD to it.toLong()) }
}
}
}
)
var callCount = 0
every { cosmosClient.isClosed() } answers {
(closedAfter != null && callCount >= closedAfter)
}
coEvery {
cosmosClient.getBlockByHeight(any())
} answers {
++callCount
val reqHeight = (offset ?: 0) + callCount
if (callCount == failAt)
Result.failure(StatusException(Status.DEADLINE_EXCEEDED))
else if (reqHeight > height)
Result.failure(StatusException(Status.INVALID_ARGUMENT))
else
Result.success(BlockOuterClass.Block.newBuilder().setHeader(Types.Header.newBuilder().setHeight(reqHeight.toLong())).build())
}
}
given("A cosmos service") {
withData(
mapOf(
"poll stopped by reaching max poll" to arrayOf(10, 5, 0, 5),
"poll stopped by reaching height" to arrayOf(10, 15, 0, 10),
"limit case: reach both height and max poll" to arrayOf(10, 10, 0, 10),
"poll stopped by reaching max poll (with offset)" to arrayOf(15, 5, 5, 5),
"poll stopped by reaching height (with offset)" to arrayOf(15, 15, 5, 10),
"limit case: reach both height and max poll (with offset)" to arrayOf(15, 10, 5, 10),
"offset is last block" to arrayOf(5, 10, 5, 0),
)
) { (height, maxPoll, offset, pollLen) ->
and("an height of $height") {
and("a max poll length of $maxPoll") {
props[CosmosSourceConnector.MAX_POLL_LENGTH_CONFIG] = maxPoll.toString()
and("an offset of $offset") {
mockForPoll(offset.takeUnless { it < 1 }, height, null, null)
cosmosSourceTask.start(props)
When("poll is called") {
val resp = cosmosSourceTask.poll()
then("it shall poll $pollLen blocks") {
resp.size shouldBe pollLen
if (pollLen > 0) {
resp.last().sourceOffset() shouldBe mapOf(CosmosSourceTask.HEIGHT_FIELD to offset + pollLen)
}
var nbCalls = pollLen
if (height - offset < maxPoll) {
nbCalls += 1
}
coVerify(exactly = nbCalls) {
cosmosClient.getBlockByHeight(any())
}
}
}
}
}
}
}
When("an error occurred during poll") {
mockForPoll(null, 4, 3, null)
props[CosmosSourceConnector.MAX_POLL_LENGTH_CONFIG] = "10"
cosmosSourceTask.start(props)
then("it shall complete exceptionally") {
val thrown = shouldThrow<StatusException> {
cosmosSourceTask.poll()
}
thrown.status shouldBe Status.DEADLINE_EXCEEDED
coVerifyOrder {
cosmosClient.getBlockByHeight(1)
cosmosClient.getBlockByHeight(2)
cosmosClient.getBlockByHeight(3)
}
}
}
When("the client is closed during poll") {
mockForPoll(null, 4, null, 3)
props[CosmosSourceConnector.MAX_POLL_LENGTH_CONFIG] = "10"
cosmosSourceTask.start(props)
then("it shall interrupt poll") {
val resp = cosmosSourceTask.poll()
resp.size shouldBe 3
coVerifyOrder {
cosmosClient.getBlockByHeight(1)
cosmosClient.getBlockByHeight(2)
cosmosClient.getBlockByHeight(3)
}
}
}
When("stop is called") {
props[CosmosSourceConnector.MAX_POLL_LENGTH_CONFIG] = "10"
cosmosSourceTask.start(props)
every {
cosmosClient.close()
} answers {}
cosmosSourceTask.stop()
then("it shall close the cosmos client") {
verify(exactly = 1) { cosmosClient.close() }
}
}
When("poll is called multiple times") {
mockForPoll(10, 50, null, null)
props[CosmosSourceConnector.MAX_POLL_LENGTH_CONFIG] = "2"
cosmosSourceTask.start(props)
val resp1 = cosmosSourceTask.poll()
val resp2 = cosmosSourceTask.poll()
then("the offset is not reset") {
resp1.size shouldBe 2
resp2.size shouldBe 2
resp1.last().sourceOffset() shouldBe mapOf(CosmosSourceTask.HEIGHT_FIELD to 12)
resp2.last().sourceOffset() shouldBe mapOf(CosmosSourceTask.HEIGHT_FIELD to 14)
coVerifyOrder {
cosmosClient.getBlockByHeight(11)
cosmosClient.getBlockByHeight(12)
cosmosClient.getBlockByHeight(13)
cosmosClient.getBlockByHeight(14)
}
}
}
}
given("A config") {
withData(
mapOf(
"Fail without config: " + CosmosSourceConnector.TOPIC_CONFIG to CosmosSourceConnector.TOPIC_CONFIG,
"Fail without config: " + CosmosSourceConnector.NODE_ADDRESS_CONFIG to CosmosSourceConnector.NODE_ADDRESS_CONFIG,
"Fail without config: " + CosmosSourceConnector.NODE_PORT_CONFIG to CosmosSourceConnector.NODE_PORT_CONFIG,
"Fail without config: " + CosmosSourceConnector.CHAIN_ID_CONFIG to CosmosSourceConnector.CHAIN_ID_CONFIG,
"Fail without config: " + CosmosSourceConnector.MAX_POLL_LENGTH_CONFIG to CosmosSourceConnector.MAX_POLL_LENGTH_CONFIG,
"Fail without config: " + CosmosSourceConnector.TLS_ENABLE_CONFIG to CosmosSourceConnector.TLS_ENABLE_CONFIG,
)
) { missingProp ->
val config = props.toMutableMap()
config.remove(missingProp)
When("start is called with a config without $missingProp") {
then("it shall complete exceptionally") {
shouldThrow<ConnectException> {
cosmosSourceTask.start(config)
}
}
}
}
}
})