16
16
package com .uber .cadence .internal .worker .autoscaler ;
17
17
18
18
import static org .junit .Assert .*;
19
+ import static org .mockito .Mockito .*;
19
20
21
+ import java .lang .reflect .Field ;
20
22
import java .time .Duration ;
23
+ import java .util .concurrent .CountDownLatch ;
21
24
import org .junit .Test ;
22
25
23
26
public class PollerAutoScalerTest {
@@ -42,4 +45,73 @@ public void testAutoScalerScalesPollers() {
42
45
43
46
assertEquals (10 , pollerAutoScaler .getSemaphoreSize ());
44
47
}
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
+ }
45
117
}
0 commit comments