Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add unit tests in event bridge module #28

Merged
merged 2 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could pass if the EventReceiver never gets called. Maybe add additional checks to be safe.

var wasEventRecieverCalled = false;
eventBridge.setEventReceiver(EventChannel.EVENT, object : EventReceiver {
      override fun receive(channel: EventChannel, event: Event) {
          assertEquals(event, testEvent)
          wasEventRecieverCalled = true;
      }
})

....

assertTrue(wasEventRecieverCalled)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the assertEquals check inside receiver can work? Let me add that to safeguard this.

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)
}
}