Skip to content

Commit

Permalink
Add ability to provide custom tracker plugins to inspect and enrich t…
Browse files Browse the repository at this point in the history
…racked events (close #574)

PR #577
  • Loading branch information
matus-tomlein authored Feb 2, 2023
1 parent 73fe85d commit 15d1fb4
Show file tree
Hide file tree
Showing 24 changed files with 834 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ class GlobalContextTest {
.base64encoding(false)
.sessionContext(true)
val gcConfig = GlobalContextsConfiguration(generators)
return createTracker(context, "aNamespace", networkConfig, trackerConfig, gcConfig)
return createTracker(
context,
"aNamespace" + Math.random().toString(),
networkConfig,
trackerConfig,
gcConfig
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.snowplowanalytics.core.emitter.Emitter
import com.snowplowanalytics.core.statemachine.State
import com.snowplowanalytics.core.statemachine.StateMachineInterface
import com.snowplowanalytics.core.statemachine.StateManager
import com.snowplowanalytics.core.statemachine.TrackerState
import com.snowplowanalytics.core.tracker.*
import com.snowplowanalytics.snowplow.event.*
import com.snowplowanalytics.snowplow.payload.SelfDescribingJson
Expand All @@ -16,13 +17,14 @@ import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import java.util.*
import kotlin.collections.ArrayList

@RunWith(AndroidJUnit4::class)
class StateManagerTest {
@Test
fun testStateManager() {
val stateManager = StateManager()
stateManager.addOrReplaceStateMachine(MockStateMachine(), "identifier")
stateManager.addOrReplaceStateMachine(MockStateMachine())
val eventInc = SelfDescribing("inc", object : HashMap<String, Any?>() {
init {
put("value", 1)
Expand Down Expand Up @@ -86,7 +88,7 @@ class StateManagerTest {
@Test
fun testAddRemoveStateMachine() {
val stateManager = StateManager()
stateManager.addOrReplaceStateMachine(MockStateMachine(), "identifier")
stateManager.addOrReplaceStateMachine(MockStateMachine())
stateManager.removeStateMachine("identifier")
val eventInc = SelfDescribing("inc", object : HashMap<String, Any?>() {
init {
Expand Down Expand Up @@ -333,8 +335,8 @@ class StateManagerTest {
@Throws(InterruptedException::class)
fun testAllowsMultipleStateMachines() {
val stateManager = StateManager()
stateManager.addOrReplaceStateMachine(MockStateMachine(), "identifier1")
stateManager.addOrReplaceStateMachine(MockStateMachine(), "identifier2")
stateManager.addOrReplaceStateMachine(MockStateMachine("identifier1"))
stateManager.addOrReplaceStateMachine(MockStateMachine("identifier2"))
val eventInc = SelfDescribing("inc", object : HashMap<String, Any?>() {
init {
put("value", 1)
Expand All @@ -350,8 +352,8 @@ class StateManagerTest {
@Throws(InterruptedException::class)
fun testDoesntDuplicateStateFromStateMachinesWithSameId() {
val stateManager = StateManager()
stateManager.addOrReplaceStateMachine(MockStateMachine(), "identifier")
stateManager.addOrReplaceStateMachine(MockStateMachine(), "identifier")
stateManager.addOrReplaceStateMachine(MockStateMachine())
stateManager.addOrReplaceStateMachine(MockStateMachine())
val eventInc = SelfDescribing("inc", object : HashMap<String, Any?>() {
init {
put("value", 1)
Expand All @@ -366,7 +368,7 @@ class StateManagerTest {
@Test
fun testReplacingStateMachineDoesntResetTrackerState() {
val stateManager = StateManager()
stateManager.addOrReplaceStateMachine(MockStateMachine(), "identifier")
stateManager.addOrReplaceStateMachine(MockStateMachine())
stateManager.trackerStateForProcessedEvent(
SelfDescribing(
"inc",
Expand All @@ -377,7 +379,7 @@ class StateManagerTest {
})
)
val state1 = stateManager.trackerState.getState("identifier")
stateManager.addOrReplaceStateMachine(MockStateMachine(), "identifier")
stateManager.addOrReplaceStateMachine(MockStateMachine())
val state2 = stateManager.trackerState.getState("identifier")
Assert.assertNotNull(state1)
Assert.assertSame(state1, state2)
Expand All @@ -389,7 +391,7 @@ class StateManagerTest {
class MockStateMachine2 : MockStateMachine()

val stateManager = StateManager()
stateManager.addOrReplaceStateMachine(MockStateMachine1(), "identifier")
stateManager.addOrReplaceStateMachine(MockStateMachine1())
stateManager.trackerStateForProcessedEvent(
SelfDescribing(
"inc",
Expand All @@ -400,15 +402,37 @@ class StateManagerTest {
})
)
val state1 = stateManager.trackerState.getState("identifier")
stateManager.addOrReplaceStateMachine(MockStateMachine2(), "identifier")
stateManager.addOrReplaceStateMachine(MockStateMachine2())
val state2 = stateManager.trackerState.getState("identifier")
Assert.assertNotNull(state1)
Assert.assertNotSame(state1, state2)
}

@Test
fun testAfterTrackCallback() {
val stateManager = StateManager()
val stateMachine = MockStateMachine()
stateManager.addOrReplaceStateMachine(stateMachine)

stateManager.afterTrack(
TrackerEvent(
Structured("cat", "act"),
TrackerState()
)
)

Thread.sleep(100)
Assert.assertEquals(1, stateMachine.afterTrackEvents.size)
Assert.assertEquals("cat", stateMachine.afterTrackEvents.first().payload["se_ca"])
}
} // Mock classes

internal class MockState(var value: Int) : State
internal open class MockStateMachine : StateMachineInterface {
internal open class MockStateMachine(
override val identifier: String = "identifier"
) : StateMachineInterface {
var afterTrackEvents: MutableList<InspectableEvent> = ArrayList()

override val subscribedEventSchemasForTransitions: List<String>
get() = LinkedList(listOf("inc", "dec"))

Expand All @@ -418,6 +442,9 @@ internal open class MockStateMachine : StateMachineInterface {
override val subscribedEventSchemasForPayloadUpdating: List<String>
get() = LinkedList(listOf("event"))

override val subscribedEventSchemasForAfterTrackCallback: List<String>
get() = Collections.singletonList("*")

override fun transition(event: Event, state: State?): State? {
val e = event as SelfDescribing
var currentState = state as MockState?
Expand Down Expand Up @@ -454,4 +481,8 @@ internal open class MockStateMachine : StateMachineInterface {
}
}
}

override fun afterTrack(event: InspectableEvent) {
afterTrackEvents.add(event)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class TrackerTest {
fun testTrackSelfDescribingEvent() {
shutdown()

val namespace = "myNamespace"
val namespace = "trackSelfDescribingEvent"
TestUtils.createSessionSharedPreferences(context, namespace)
val mockWebServer = getMockServer(1)
var emitter: Emitter? = null
Expand Down Expand Up @@ -243,7 +243,7 @@ class TrackerTest {
fun testTrackWithNoContext() {
shutdown()

val namespace = "myNamespaceNoContext"
val namespace = "trackWithNoContext"
TestUtils.createSessionSharedPreferences(context, namespace)

val mockWebServer = getMockServer(1)
Expand Down Expand Up @@ -308,7 +308,7 @@ class TrackerTest {
fun testTrackWithoutDataCollection() {
threadCount = 30
shutdown()
val namespace = "myNamespace"
val namespace = "trackWithoutDataCollection"
TestUtils.createSessionSharedPreferences(context, namespace)
val mockWebServer = getMockServer(1)
val builder = { emitter: Emitter -> emitter.bufferOption = BufferOption.Single }
Expand Down Expand Up @@ -340,7 +340,7 @@ class TrackerTest {
fun testTrackWithSession() {
threadCount = 30
shutdown()
val namespace = "myNamespace"
val namespace = "trackWithSession"
TestUtils.createSessionSharedPreferences(context, namespace)
val mockWebServer = getMockServer(1)
val builder = { emitter: Emitter -> emitter.bufferOption = BufferOption.Single }
Expand Down Expand Up @@ -369,7 +369,7 @@ class TrackerTest {

@Test
fun testTrackScreenView() {
val namespace = "myNamespace"
val namespace = "trackScreenView"
TestUtils.createSessionSharedPreferences(context, namespace)
val builder = { emitter: Emitter -> emitter.bufferOption = BufferOption.Single }
val emitter = Emitter(
Expand Down Expand Up @@ -412,7 +412,7 @@ class TrackerTest {

@Test
fun testTrackUncaughtException() {
val namespace = "myNamespace"
val namespace = "trackUncaughtException"
TestUtils.createSessionSharedPreferences(context, namespace)
Thread.setDefaultUncaughtExceptionHandler(
TestExceptionHandler("Illegal State Exception has been thrown!")
Expand Down Expand Up @@ -441,7 +441,7 @@ class TrackerTest {

@Test
fun testExceptionHandler() {
val namespace = "myNamespace"
val namespace = "exceptionHandler"
TestUtils.createSessionSharedPreferences(context, namespace)
val handler = TestExceptionHandler("Illegal State Exception has been thrown!")
Thread.setDefaultUncaughtExceptionHandler(handler)
Expand Down
Loading

0 comments on commit 15d1fb4

Please sign in to comment.