Skip to content

Commit

Permalink
fix(session): schedule session handling to background thread
Browse files Browse the repository at this point in the history
SUITEDEV-26047

Co-authored-by: LasOri <[email protected]>
Co-authored-by: kovacszsoltizsolt <[email protected]>
  • Loading branch information
3 people committed Jan 5, 2021
1 parent e838907 commit b44611b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package com.emarsys.core.app

import android.os.HandlerThread
import com.emarsys.core.concurrency.CoreSdkHandler
import com.emarsys.core.session.Session
import com.emarsys.testUtil.TimeoutUtils
import com.emarsys.testUtil.mockito.ThreadSpy
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import java.util.*
import java.util.concurrent.CountDownLatch

class AppLifecycleObserverTest {
private lateinit var mockSession: Session
private lateinit var appLifecycleObserver: AppLifecycleObserver
private lateinit var coreSdkHandler: CoreSdkHandler

@Rule
@JvmField
Expand All @@ -19,19 +26,57 @@ class AppLifecycleObserverTest {
@Before
fun setUp() {
mockSession = mock()
val handlerThread = HandlerThread("CoreSDKHandlerThread-" + UUID.randomUUID().toString())
handlerThread.start()
coreSdkHandler = CoreSdkHandler(handlerThread)
appLifecycleObserver = AppLifecycleObserver(mockSession, coreSdkHandler)
}

@Test
fun onEnterForeground_sessionStart_shouldBeCalled() {
AppLifecycleObserver(mockSession).onEnterForeground()
val latch = CountDownLatch(1)

appLifecycleObserver.onEnterForeground()
coreSdkHandler.post {
latch.countDown()
}

latch.await()

verify(mockSession).startSession()
}

@Test
fun onEnterBackground_endSession_shouldBeCalled() {
AppLifecycleObserver(mockSession).onEnterBackground()
val latch = CountDownLatch(1)

appLifecycleObserver.onEnterBackground()
coreSdkHandler.post {
latch.countDown()
}

latch.await()

verify(mockSession).endSession()
}

@Test
fun testStartSession_startsSessionOnCoreSdkThread() {
val threadSpy = ThreadSpy<Unit>()
org.mockito.Mockito.doAnswer(threadSpy).`when`(mockSession).startSession()

appLifecycleObserver.onEnterForeground()

threadSpy.verifyCalledOnCoreSdkThread()
}

@Test
fun testEndSession_endsSessionOnCoreSdkThread() {
val threadSpy = ThreadSpy<Unit>()

org.mockito.Mockito.doAnswer(threadSpy).`when`(mockSession).endSession()

appLifecycleObserver.onEnterBackground()
threadSpy.verifyCalledOnCoreSdkThread()
}
}
12 changes: 9 additions & 3 deletions core/src/main/java/com/emarsys/core/app/AppLifecycleObserver.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package com.emarsys.core.app

import android.os.Handler
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.emarsys.core.Mockable
import com.emarsys.core.session.Session

@Mockable
class AppLifecycleObserver(private val session: Session) : LifecycleObserver {
class AppLifecycleObserver(private val session: Session,
private val coreSdkHandler: Handler) : LifecycleObserver {

@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onEnterForeground() {
session.startSession()
coreSdkHandler.post {
session.startSession()
}
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onEnterBackground() {
session.endSession()
coreSdkHandler.post {
session.endSession()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ open class DefaultEmarsysDependencyContainer(emarsysConfig: EmarsysConfig) : Ema
addDependency(dependencies, it as EventServiceInternal, "defaultInstance")
}
val mobileEngageSession = MobileEngageSession(getTimestampProvider(), getUuidProvider(), getEventServiceInternal(), sessionIdHolder)
AppLifecycleObserver(mobileEngageSession).also {
AppLifecycleObserver(mobileEngageSession, getCoreSdkHandler()).also {
addDependency(dependencies, it)
}
DefaultMessageInboxInternal(requestManager, getRequestContext(), requestModelFactory, uiHandler, MessageInboxResponseMapper()).also {
Expand Down

0 comments on commit b44611b

Please sign in to comment.