use of com.uber.cadence.internal.replay.DecisionContext in project cadence-client by uber-java.
the class DeterministicRunnerTest method workflowThreadsWillEvictCacheWhenMaxThreadCountIsHit.
@Test
public void workflowThreadsWillEvictCacheWhenMaxThreadCountIsHit() throws Throwable {
// Arrange
// Arrange
Map<String, String> tags = new ImmutableMap.Builder<String, String>(2).put(MetricsTag.DOMAIN, "domain").put(MetricsTag.TASK_LIST, "stickyTaskList").build();
StatsReporter reporter = mock(StatsReporter.class);
Scope scope = new RootScopeBuilder().reporter(reporter).reportEvery(com.uber.m3.util.Duration.ofMillis(300)).tagged(tags);
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 3, 1, TimeUnit.SECONDS, new SynchronousQueue<>());
AtomicReference<String> status = new AtomicReference<>();
DeciderCache cache = new DeciderCache(3, scope);
DecisionContext decisionContext = mock(DecisionContext.class);
when(decisionContext.getMetricsScope()).thenReturn(scope);
when(decisionContext.getDomain()).thenReturn("domain");
when(decisionContext.getWorkflowType()).thenReturn(new WorkflowType());
DeterministicRunnerImpl d = new DeterministicRunnerImpl(threadPool, new SyncDecisionContext(decisionContext, JsonDataConverter.getInstance(), null, next -> next, null), // clock override
() -> 0L, () -> {
Promise<Void> thread = Async.procedure(() -> {
status.set("started");
WorkflowThread.await("doing something", () -> false);
status.set("done");
});
thread.get();
}, cache);
Decider decider = new DetermisiticRunnerContainerDecider(d);
PollForDecisionTaskResponse response = HistoryUtils.generateDecisionTaskWithInitialHistory();
cache.getOrCreate(response, () -> decider);
cache.addToCache(response, decider);
d.runUntilAllBlocked();
assertEquals(2, threadPool.getActiveCount());
DeterministicRunnerImpl d2 = new DeterministicRunnerImpl(threadPool, new SyncDecisionContext(decisionContext, JsonDataConverter.getInstance(), null, next -> next, null), // clock override
() -> 0L, () -> {
Promise<Void> thread = Async.procedure(() -> {
status.set("started");
WorkflowThread.await("doing something else", () -> false);
status.set("done");
});
thread.get();
}, cache);
// Root thread added for d2 therefore we expect a total of 3 threads used
assertEquals(3, threadPool.getActiveCount());
assertEquals(1, cache.size());
// Act: This should kick out threads consumed by 'd'
d2.runUntilAllBlocked();
// Assert: Cache is evicted and only threads consumed by d2 remain.
assertEquals(2, threadPool.getActiveCount());
// cache was evicted
assertEquals(0, cache.size());
// Wait for reporter
Thread.sleep(600);
verify(reporter, atLeastOnce()).reportCounter(eq(MetricsType.STICKY_CACHE_THREAD_FORCED_EVICTION), eq(tags), anyInt());
}
Aggregations