-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathEmitterSpec.scala
217 lines (162 loc) · 7.79 KB
/
EmitterSpec.scala
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
211
212
213
214
215
216
217
/**
* Copyright (c) 2014-2020 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.snowplowanalytics.stream.loader
// Java
import java.util.Properties
import emitters.Emitter
import org.slf4j.Logger
// Scala
import scala.collection.mutable.ListBuffer
import scala.collection.JavaConverters._
// AWS libs
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain
// AWS Kinesis Connector libs
import com.amazonaws.services.kinesis.connectors.{KinesisConnectorConfiguration, UnmodifiableBuffer}
import com.amazonaws.services.kinesis.connectors.impl.BasicMemoryBuffer
// cats
import cats.syntax.validated._
import io.circe.Json
// Specs2
import org.specs2.mutable.Specification
// This project
import sinks._
import clients._
class MockElasticsearchSender extends BulkSender[EmitterJsonInput] {
var sentRecords: List[EmitterJsonInput] = List.empty
var callCount: Int = 0
val calls: ListBuffer[List[EmitterJsonInput]] = new ListBuffer
override val log: Logger = null
override def send(records: List[EmitterJsonInput]): List[EmitterJsonInput] = {
sentRecords = sentRecords ::: records
callCount += 1
calls += records
List.empty
}
override def close() = {}
override def logHealth(): Unit = ()
override val tracker = None
override val maxConnectionWaitTimeMs: Long = 1000L
override val maxAttempts: Int = 1
}
class EmitterSpec extends Specification {
val documentType = "enriched"
"The emitter" should {
"return all invalid records" in {
val fakeSender: BulkSender[EmitterJsonInput] = new BulkSender[EmitterJsonInput] {
override def send(records: List[EmitterJsonInput]): List[EmitterJsonInput] = List.empty
override def close(): Unit = ()
override def logHealth(): Unit = ()
override val tracker = None
override val log: Logger = null
override val maxConnectionWaitTimeMs: Long = 1000L
override val maxAttempts: Int = 1
}
val kcc =
new KinesisConnectorConfiguration(new Properties, new DefaultAWSCredentialsProviderChain)
val eem = new Emitter(fakeSender, new StdouterrSink, 1, 1L)
val validInput: EmitterJsonInput = "good" -> JsonRecord(Json.obj(), None).valid
val invalidInput: EmitterJsonInput = "bad" -> "malformed event".invalidNel
val input = List(validInput, invalidInput)
val bmb = new BasicMemoryBuffer[EmitterJsonInput](kcc, input.asJava)
val ub = new UnmodifiableBuffer[EmitterJsonInput](bmb)
val actual = eem.emit(ub)
actual must_== List(invalidInput).asJava
}
"send multiple records in seperate requests where single record size > buffer bytes size" in {
val props = new Properties
props.setProperty(KinesisConnectorConfiguration.PROP_BUFFER_BYTE_SIZE_LIMIT, "1000")
val kcc = new KinesisConnectorConfiguration(props, new DefaultAWSCredentialsProviderChain)
val ess = new MockElasticsearchSender
val eem = new Emitter(ess, new StdouterrSink, 1, 1000L)
val validInput: EmitterJsonInput = "good" -> JsonRecord(Json.obj(), None).valid
val input = List.fill(50)(validInput)
val bmb = new BasicMemoryBuffer[EmitterJsonInput](kcc, input.asJava)
val ub = new UnmodifiableBuffer[EmitterJsonInput](bmb)
eem.emit(ub)
ess.sentRecords mustEqual input
ess.callCount mustEqual 50
forall(ess.calls) { c =>
c.length mustEqual 1
}
}
"send a single record in 1 request where record size > buffer bytes size " in {
val props = new Properties
props.setProperty(KinesisConnectorConfiguration.PROP_BUFFER_BYTE_SIZE_LIMIT, "1000")
val kcc = new KinesisConnectorConfiguration(props, new DefaultAWSCredentialsProviderChain)
val ess = new MockElasticsearchSender
val eem = new Emitter(ess, new StdouterrSink, 1, 1000L)
val validInput: EmitterJsonInput = "good" -> JsonRecord(Json.obj(), None).valid
val input = List(validInput)
val bmb = new BasicMemoryBuffer[EmitterJsonInput](kcc, input.asJava)
val ub = new UnmodifiableBuffer[EmitterJsonInput](bmb)
eem.emit(ub)
ess.sentRecords mustEqual input
ess.callCount mustEqual 1
forall(ess.calls) { c =>
c.length mustEqual 1
}
}
"send multiple records in 1 request where total byte size < buffer bytes size" in {
val props = new Properties
props.setProperty(KinesisConnectorConfiguration.PROP_BUFFER_BYTE_SIZE_LIMIT, "1048576")
val kcc = new KinesisConnectorConfiguration(props, new DefaultAWSCredentialsProviderChain)
val ess = new MockElasticsearchSender
val eem = new Emitter(ess, new StdouterrSink, 100, 1048576L)
val validInput: EmitterJsonInput = "good" -> JsonRecord(Json.obj(), None).valid
val input = List.fill(50)(validInput)
val bmb = new BasicMemoryBuffer[EmitterJsonInput](kcc, input.asJava)
val ub = new UnmodifiableBuffer[EmitterJsonInput](bmb)
eem.emit(ub)
ess.sentRecords mustEqual input
ess.callCount mustEqual 1
forall(ess.calls) { c =>
c.length mustEqual 50
}
}
"send a single record in 1 request where single record size < buffer bytes size" in {
val props = new Properties
props.setProperty(KinesisConnectorConfiguration.PROP_BUFFER_BYTE_SIZE_LIMIT, "1048576")
val kcc = new KinesisConnectorConfiguration(props, new DefaultAWSCredentialsProviderChain)
val ess = new MockElasticsearchSender
val eem = new Emitter(ess, new StdouterrSink, 1, 1048576L)
val validInput: EmitterJsonInput = "good" -> JsonRecord(Json.obj(), None).valid
val input = List(validInput)
val bmb = new BasicMemoryBuffer[EmitterJsonInput](kcc, input.asJava)
val ub = new UnmodifiableBuffer[EmitterJsonInput](bmb)
eem.emit(ub)
ess.sentRecords mustEqual input
ess.callCount mustEqual 1
forall(ess.calls) { c =>
c.length mustEqual 1
}
}
"send multiple records in batches where single record byte size < buffer size and total byte size > buffer size" in {
val props = new Properties
props.setProperty(KinesisConnectorConfiguration.PROP_BUFFER_BYTE_SIZE_LIMIT, "200")
val kcc = new KinesisConnectorConfiguration(props, new DefaultAWSCredentialsProviderChain)
val ess = new MockElasticsearchSender
val eem = new Emitter(ess, new StdouterrSink, 2, 200L)
// record size is 95 bytes
val validInput: EmitterJsonInput = "good" -> JsonRecord(Json.obj(), None).valid
val input = List.fill(20)(validInput)
val bmb = new BasicMemoryBuffer[EmitterJsonInput](kcc, input.asJava)
val ub = new UnmodifiableBuffer[EmitterJsonInput](bmb)
eem.emit(ub)
ess.sentRecords mustEqual input
ess.callCount mustEqual 10 // 10 buffers of 2 records each
forall(ess.calls) { c =>
c.length mustEqual 2
}
}
}
}