Skip to content

Commit

Permalink
feat: add unit tests in event bridge module (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen authored Apr 25, 2022
1 parent dc78bc6 commit 8d63c28
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
9 changes: 8 additions & 1 deletion event-bridge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@ plugins {
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
test {
useJUnitPlatform()
}
dependencies {
testImplementation platform("org.junit:junit-bom:5.7.2")
testImplementation "org.junit.jupiter:junit-jupiter"
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ interface EventReceiver {
fun receive(channel: EventChannel, event: Event)
}

/**
* Bridges to transfer data between modules, like analytics and experiment
*/
interface EventBridge {
fun sendEvent(channel: EventChannel, event: Event)
fun setEventReceiver(channel: EventChannel, receiver: EventReceiver)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.amplitude.eventbridge

/**
* Container of EventBridge
*/
class EventBridgeContainer {

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.amplitude.eventbridge

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class EventBridgeContainerTest {

@Test
fun `test getInstance return same instance for certain name`() {
val eventBridge1 = EventBridgeContainer.getInstance("testInstance")
val eventBridge2 = EventBridgeContainer.getInstance("testInstance")
Assertions.assertEquals(eventBridge1, eventBridge2)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.amplitude.eventbridge

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class EventBridgeTest {

@Test
fun `test addEventListener, sendEvent, listener called`() {
val testEvent = Event("test")
val identifyEvent = Event("identify")
val eventBridge = EventBridgeImpl()
var wasEventReceiverCalled = false
eventBridge.setEventReceiver(
EventChannel.EVENT,
object : EventReceiver {
override fun receive(channel: EventChannel, event: Event) {
assertEquals(event, testEvent)
wasEventReceiverCalled = true
}
}
)
eventBridge.setEventReceiver(
EventChannel.IDENTIFY,
object : EventReceiver {
override fun receive(channel: EventChannel, event: Event) {
assertEquals(event, identifyEvent)
wasEventReceiverCalled = true
}
}
)
eventBridge.sendEvent(EventChannel.EVENT, testEvent)
eventBridge.sendEvent(EventChannel.IDENTIFY, identifyEvent)
assertTrue(wasEventReceiverCalled)
}

@Test
fun `test sendEvent first, add listener later and listener receive event`() {
val testEvent1 = Event("test1")
val testEvent2 = Event("test2")
val testEvent3 = Event("test3")
val eventBridge = EventBridgeImpl()
eventBridge.sendEvent(EventChannel.EVENT, testEvent1)
eventBridge.sendEvent(EventChannel.EVENT, testEvent2)
eventBridge.sendEvent(EventChannel.EVENT, testEvent3)
var eventCount = 0
eventBridge.setEventReceiver(
EventChannel.EVENT,
object : EventReceiver {
override fun receive(channel: EventChannel, event: Event) {
eventCount++
}
}
)
assertEquals(3, eventCount)
}
}

0 comments on commit 8d63c28

Please sign in to comment.