Skip to content

Commit

Permalink
Flush events only when the buffer is full (close #648)
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-tomlein committed Nov 30, 2023
1 parent c6ad42b commit caf3d05
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,29 @@ class EmitterTest {
emitter.flush()
}

@Test
fun testDoesntMakeRequestUnlessBufferSizeIsReached() {
val networkConnection = MockNetworkConnection(HttpMethod.GET, 200)
val emitter = getEmitter(networkConnection, BufferOption.SmallGroup)

for (payload in generatePayloads(9)) {
emitter.add(payload)
}
Thread.sleep(1000)

// all events waiting in queue
Assert.assertEquals(0, networkConnection.previousResults.size)
Assert.assertEquals(9, emitter.eventStore!!.size())

emitter.add(generatePayloads(1)[0])
Thread.sleep(1000)

// all events sent
Assert.assertEquals(0, emitter.eventStore!!.size())
Assert.assertEquals(1, networkConnection.previousResults.size)
emitter.flush()
}

// Emitter Builder
private fun getEmitter(networkConnection: NetworkConnection?, option: BufferOption?): Emitter {
val builder = { emitter: Emitter ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,15 @@ class Emitter(context: Context, collectorUri: String, builder: ((Emitter) -> Uni
*/
fun add(payload: Payload) {
Executor.execute(TAG) {
eventStore?.add(payload)
if (isRunning.compareAndSet(false, true)) {
try {
attemptEmit(networkConnection)
} catch (t: Throwable) {
isRunning.set(false)
Logger.e(TAG, "Received error during emission process: %s", t)
eventStore?.let { eventStore ->
eventStore.add(payload)
if (eventStore.size() >= bufferOption.code && isRunning.compareAndSet(false, true)) {
try {
attemptEmit(networkConnection)
} catch (t: Throwable) {
isRunning.set(false)
Logger.e(TAG, "Received error during emission process: %s", t)
}
}
}
}
Expand Down

0 comments on commit caf3d05

Please sign in to comment.