Skip to content

Commit fb7d322

Browse files
authored
Tests for PollerAutoScaler (#977)
* Tests for PollerAutoScaler * Lint
1 parent 4f39f33 commit fb7d322

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

src/main/java/com/uber/cadence/internal/worker/autoscaler/PollerAutoScaler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public PollerAutoScaler(
4040
this.semaphoreSize = recommender.getUpperValue();
4141
}
4242

43+
// Sleep method which can be mocked in tests
44+
void sleep(long millis) throws InterruptedException {
45+
Thread.sleep(millis);
46+
}
47+
4348
public void start() {
4449
Executors.newSingleThreadExecutor()
4550
.submit(
@@ -48,7 +53,7 @@ public void start() {
4853
public void run() {
4954
while (!shuttingDown) {
5055
try {
51-
Thread.sleep(coolDownTime.toMillis());
56+
sleep(coolDownTime.toMillis());
5257
if (!shuttingDown) {
5358
resizePollers();
5459
}

src/test/java/com/uber/cadence/internal/worker/autoscaler/PollerAutoScalerTest.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
package com.uber.cadence.internal.worker.autoscaler;
1717

1818
import static org.junit.Assert.*;
19+
import static org.mockito.Mockito.*;
1920

21+
import java.lang.reflect.Field;
2022
import java.time.Duration;
23+
import java.util.concurrent.CountDownLatch;
2124
import org.junit.Test;
2225

2326
public class PollerAutoScalerTest {
@@ -42,4 +45,73 @@ public void testAutoScalerScalesPollers() {
4245

4346
assertEquals(10, pollerAutoScaler.getSemaphoreSize());
4447
}
48+
49+
@Test
50+
public void testStart() throws Exception {
51+
// Get a partial mock of PollerAutoScaler
52+
PollerAutoScaler pollerAutoScaler =
53+
spy(new PollerAutoScaler(Duration.ofSeconds(10), null, new Recommender(0.5f, 100, 10)));
54+
55+
// We want to test what resizePollers() is called, we don't want to test
56+
// the implementation of the resizePollers() method
57+
doNothing().when(pollerAutoScaler).resizePollers();
58+
59+
// We let the loop run 3 times on the 3rd iteration we stop the pollerAutoScaler
60+
// so we expect resizePollers() to be called 2 times and stop() to be called once
61+
CountDownLatch latch = new CountDownLatch(3);
62+
doAnswer(
63+
invocation -> {
64+
latch.countDown();
65+
if (latch.getCount() == 0) {
66+
pollerAutoScaler.stop();
67+
}
68+
return null;
69+
})
70+
.when(pollerAutoScaler)
71+
.sleep(anyLong());
72+
73+
pollerAutoScaler.start();
74+
75+
// Wait for the latch to be 0
76+
latch.await();
77+
assertEquals(0, latch.getCount());
78+
79+
// Verify that resizePollers() was called 2 times and stop() was called once
80+
verify(pollerAutoScaler, times(2)).resizePollers();
81+
verify(pollerAutoScaler, times(1)).stop();
82+
}
83+
84+
@Test
85+
public void testAquireRelease() throws Exception {
86+
PollerAutoScaler pollerAutoScaler =
87+
new PollerAutoScaler(Duration.ofSeconds(10), null, new Recommender(0.5f, 100, 10));
88+
89+
// access the private semaphore field
90+
Field semaphoreField = PollerAutoScaler.class.getDeclaredField("semaphore");
91+
semaphoreField.setAccessible(true);
92+
ResizableSemaphore semaphore = (ResizableSemaphore) semaphoreField.get(pollerAutoScaler);
93+
94+
assertEquals(100, semaphore.availablePermits());
95+
96+
pollerAutoScaler.acquire();
97+
assertEquals(99, semaphore.availablePermits());
98+
99+
pollerAutoScaler.release();
100+
assertEquals(100, semaphore.availablePermits());
101+
}
102+
103+
@Test
104+
public void testPollerIndirection() throws Exception {
105+
PollerUsageEstimator pollerUsageEstimator = mock(PollerUsageEstimator.class);
106+
107+
PollerAutoScaler pollerAutoScaler =
108+
new PollerAutoScaler(
109+
Duration.ofSeconds(10), pollerUsageEstimator, new Recommender(0.5f, 100, 10));
110+
111+
pollerAutoScaler.increaseNoopPollCount();
112+
verify(pollerUsageEstimator, times(1)).increaseNoopTaskCount();
113+
114+
pollerAutoScaler.increaseActionablePollCount();
115+
verify(pollerUsageEstimator, times(1)).increaseActionableTaskCount();
116+
}
45117
}

0 commit comments

Comments
 (0)