@@ -11,6 +11,7 @@ import com.mparticle.testutils.MPLatch
1111import kotlinx.coroutines.test.StandardTestDispatcher
1212import kotlinx.coroutines.test.runTest
1313import org.json.JSONException
14+ import org.junit.After
1415import org.junit.Assert
1516import org.junit.Before
1617import org.junit.Test
@@ -65,42 +66,52 @@ class AppStateManagerInstrumentedTest : BaseCleanStartedEachTest() {
6566
6667 @Test
6768 @Throws(Exception ::class )
68- fun testDontIncludeDefaultMpidSessionEnd () {
69- val mpids: MutableSet <Long > = HashSet ()
70- for (i in 0 .. 4 ) {
71- mpids.add(ran.nextLong())
72- }
69+ fun testSessionEndExcludesTemporaryMpid () {
70+ // Arrange
71+ val mpids: MutableSet <Long > = mutableSetOf ()
72+ repeat(5 ) { mpids.add(ran.nextLong()) }
7373 mpids.add(Constants .TEMPORARY_MPID )
74+
7475 mAppStateManager?.ensureActiveSession()
75- for (mpid in mpids) {
76+ mpids.forEach { mpid ->
7677 mAppStateManager?.fetchSession()?.addMpid(mpid)
7778 }
78- val latch: CountDownLatch = MPLatch (1 )
79- val checked = MutableBoolean (false )
80- AccessUtils .setMessageStoredListener(
81- MParticleDBManager .MessageListener { message ->
82- if (message.messageType == Constants .MessageType .SESSION_END ) {
83- try {
84- val mpidsArray =
85- message.getJSONArray(Constants .MessageKey .SESSION_SPANNING_MPIDS )
86- if (mpidsArray.length() == mpids.size - 1 ) {
87- for (i in 0 until mpidsArray.length()) {
88- if (! mpids.contains(mpidsArray.getLong(i)) || mpidsArray.getLong(i) == Constants .TEMPORARY_MPID ) {
89- return @MessageListener
90- }
91- }
92- checked.value = true
93- latch.countDown()
94- }
95- } catch (e: JSONException ) {
96- e.printStackTrace()
79+
80+ val latch = CountDownLatch (1 )
81+ val wasChecked = MutableBoolean (false )
82+
83+ AccessUtils .setMessageStoredListener { message ->
84+ if (message.messageType != Constants .MessageType .SESSION_END ) return @setMessageStoredListener
85+
86+ try {
87+ val mpidsArray = message.getJSONArray(Constants .MessageKey .SESSION_SPANNING_MPIDS )
88+
89+ // Assert size (TEMPORARY_MPID should not be present)
90+ if (mpidsArray.length() != mpids.size - 1 ) return @setMessageStoredListener
91+
92+ // Validate each MPID
93+ for (i in 0 until mpidsArray.length()) {
94+ val mpid = mpidsArray.getLong(i)
95+ if (mpid == Constants .TEMPORARY_MPID || ! mpids.contains(mpid)) {
96+ return @setMessageStoredListener
9797 }
9898 }
99+
100+ wasChecked.value = true
101+ latch.countDown()
102+ } catch (e: JSONException ) {
103+ e.printStackTrace()
99104 }
100- )
105+ }
106+
107+ // Act
101108 mAppStateManager?.endSession()
109+
110+ // Await verification
102111 latch.await()
103- Assert .assertTrue(checked.value)
112+
113+ // Assert
114+ Assert .assertTrue(" Temporary MPID should not be included in session end event" , wasChecked.value)
104115 }
105116
106117 @Test
@@ -149,4 +160,9 @@ class AppStateManagerInstrumentedTest : BaseCleanStartedEachTest() {
149160 latch.countDown()
150161 }
151162 }
163+
164+ @After
165+ fun tearDown () {
166+ AccessUtils .setMessageStoredListener(null ) // Clear listener
167+ }
152168}
0 commit comments