Skip to content

Commit

Permalink
refactor: consolidate thread count
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Jan 2, 2024
1 parent 0013d68 commit 95b2c72
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ internal class PeriodicSessionCacheTest {
val clock = FakeClock(IntegrationTestRule.DEFAULT_SDK_START_TIME_MS)
IntegrationTestRule.Harness(
fakeClock = clock,
workerThreadModule = FakeWorkerThreadModule(clock, SESSION_CACHING)
workerThreadModule = FakeWorkerThreadModule(clock, PERIODIC_CACHE)
)
}

@Test
fun `session is periodically cached`() {
with(testRule) {
val executor =
harness.workerThreadModule.scheduledExecutor(SESSION_CACHING) as BlockingScheduledExecutorService
harness.workerThreadModule.scheduledExecutor(PERIODIC_CACHE) as BlockingScheduledExecutorService

harness.recordSession {
executor.runCurrentlyBlocked()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ internal class TimedSessionTest {
val clock = FakeClock(IntegrationTestRule.DEFAULT_SDK_START_TIME_MS)
IntegrationTestRule.Harness(
fakeClock = clock,
workerThreadModule = FakeWorkerThreadModule(clock, SESSION_CLOSER),
workerThreadModule = FakeWorkerThreadModule(clock, BACKGROUND_REGISTRATION),
)
}

@Test
fun `timed session automatically ends session`() {
with(testRule) {
val executor =
harness.workerThreadModule.scheduledExecutor(SESSION_CLOSER) as BlockingScheduledExecutorService
harness.workerThreadModule.scheduledExecutor(BACKGROUND_REGISTRATION) as BlockingScheduledExecutorService
harness.fakeConfigService.sessionBehavior = fakeSessionBehavior(
localCfg = { SessionLocalConfig(90) }) {
RemoteConfig(sessionConfig = SessionRemoteConfig(isEnabled = true))
Expand All @@ -68,7 +68,7 @@ internal class TimedSessionTest {
fun `timed session has no effect when config disabled`() {
with(testRule) {
val executor =
harness.workerThreadModule.scheduledExecutor(SESSION_CLOSER) as BlockingScheduledExecutorService
harness.workerThreadModule.scheduledExecutor(BACKGROUND_REGISTRATION) as BlockingScheduledExecutorService
harness.fakeConfigService.sessionBehavior = fakeSessionBehavior {
RemoteConfig(sessionConfig = SessionRemoteConfig(isEnabled = true))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal class EmbraceEventService(
deliveryService,
logger,
clock,
workerThreadModule.scheduledExecutor(ExecutorName.SCHEDULED_REGISTRATION)
workerThreadModule.scheduledExecutor(ExecutorName.BACKGROUND_REGISTRATION)
)
executorService =
workerThreadModule.backgroundExecutor(ExecutorName.BACKGROUND_REGISTRATION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal class DataCaptureServiceModuleImpl @JvmOverloads constructor(
) : DataCaptureServiceModule {

private val backgroundExecutorService = workerThreadModule.backgroundExecutor(ExecutorName.BACKGROUND_REGISTRATION)
private val scheduledExecutor = workerThreadModule.scheduledExecutor(ExecutorName.SCHEDULED_REGISTRATION)
private val scheduledExecutor = workerThreadModule.scheduledExecutor(ExecutorName.BACKGROUND_REGISTRATION)
private val configService = essentialServiceModule.configService

override val memoryService: MemoryService by singleton {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ internal class SessionModuleImpl(
sessionProperties,
initModule.clock,
initModule.spansService,
workerThreadModule.scheduledExecutor(ExecutorName.SESSION_CLOSER),
workerThreadModule.scheduledExecutor(ExecutorName.SESSION_CACHING)
workerThreadModule.scheduledExecutor(ExecutorName.BACKGROUND_REGISTRATION),
workerThreadModule.scheduledExecutor(ExecutorName.PERIODIC_CACHE)
)
}

Expand Down Expand Up @@ -108,7 +108,7 @@ internal class SessionModuleImpl(
nativeModule.ndkService,
initModule.clock,
backgroundActivityCollator,
lazy { workerThreadModule.backgroundExecutor(ExecutorName.SESSION_CACHE_EXECUTOR) }
lazy { workerThreadModule.backgroundExecutor(ExecutorName.PERIODIC_CACHE) }
)
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ internal class NativeModuleImpl(
coreModule.logger,
embraceNdkServiceRepository,
NdkDelegateImpl(),
workerThreadModule.backgroundExecutor(ExecutorName.NATIVE_CRASH_CLEANER),
workerThreadModule.backgroundExecutor(ExecutorName.NATIVE_STARTUP),
workerThreadModule.backgroundExecutor(ExecutorName.BACKGROUND_REGISTRATION),
workerThreadModule.backgroundExecutor(ExecutorName.BACKGROUND_REGISTRATION),
essentialServiceModule.deviceArchitecture,
coreModule.jsonSerializer
)
Expand All @@ -54,7 +54,7 @@ internal class NativeModuleImpl(
EmbraceNativeThreadSamplerService(
essentialServiceModule.configService,
lazy { ndkService.getSymbolsForCurrentArch() },
executorService = workerThreadModule.scheduledExecutor(ExecutorName.SCHEDULED_REGISTRATION),
executorService = workerThreadModule.scheduledExecutor(ExecutorName.BACKGROUND_REGISTRATION),

Check warning on line 57 in embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/ndk/NativeModule.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/ndk/NativeModule.kt#L57

Added line #L57 was not covered by tests
deviceArchitecture = essentialServiceModule.deviceArchitecture
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,10 @@ internal interface WorkerThreadModule : Closeable {
*/
internal enum class ExecutorName(internal val threadName: String) {
BACKGROUND_REGISTRATION("background-reg"),
SCHEDULED_REGISTRATION("scheduled-reg"),
CACHED_SESSIONS("cached-sessions"),
SEND_SESSIONS("send-sessions"),
DELIVERY_CACHE("delivery-cache"),
NETWORK_REQUEST("network-request"),
NATIVE_CRASH_CLEANER("native-crash-cleaner"),
NATIVE_STARTUP("native-startup"),
SESSION_CACHE_EXECUTOR("session-cache"),
PERIODIC_CACHE("periodic-cache"),
REMOTE_LOGGING("remote-logging"),
SESSION_CLOSER("session-closer"),
SESSION_CACHING("session-caching"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ internal class EmbraceEventServiceTest {
deliveryService = deliveryService,
logger = logger,
clock = fakeClock,
scheduledExecutor = fakeWorkerThreadModule.scheduledExecutor(ExecutorName.SCHEDULED_REGISTRATION)
scheduledExecutor = fakeWorkerThreadModule.scheduledExecutor(ExecutorName.BACKGROUND_REGISTRATION)
)
eventService = EmbraceEventService(
1,
Expand Down Expand Up @@ -473,8 +473,7 @@ internal class EmbraceEventServiceTest {
eventService.sendStartupMoment()
assertNull(eventService.getStartupMomentInfo())
fakeClock.tick(10000L)
fakeWorkerThreadModule.backgroundExecutor(ExecutorName.BACKGROUND_REGISTRATION).runCurrentlyBlocked()
fakeWorkerThreadModule.scheduledExecutor(ExecutorName.SCHEDULED_REGISTRATION).runCurrentlyBlocked()
fakeWorkerThreadModule.scheduledExecutor(ExecutorName.BACKGROUND_REGISTRATION).runCurrentlyBlocked()
assertNotNull(eventService.getStartupMomentInfo())
val completedSpans = checkNotNull(spansService.completedSpans())
assertEquals(0, completedSpans.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ internal class WorkerThreadModuleImplTest {
val module = WorkerThreadModuleImpl()
assertNotNull(module)

val backgroundExecutor = module.backgroundExecutor(ExecutorName.SESSION_CACHE_EXECUTOR)
val backgroundExecutor = module.backgroundExecutor(ExecutorName.PERIODIC_CACHE)
assertNotNull(backgroundExecutor)
val scheduledExecutor = module.scheduledExecutor(ExecutorName.SESSION_CACHE_EXECUTOR)
val scheduledExecutor = module.scheduledExecutor(ExecutorName.PERIODIC_CACHE)
assertNotNull(scheduledExecutor)

// test caching
assertSame(backgroundExecutor, module.backgroundExecutor(ExecutorName.SESSION_CACHE_EXECUTOR))
assertSame(backgroundExecutor, module.backgroundExecutor(ExecutorName.PERIODIC_CACHE))
assertSame(backgroundExecutor, scheduledExecutor)

// test shutting down module
Expand Down

0 comments on commit 95b2c72

Please sign in to comment.