15
15
import static org .mockito .ArgumentMatchers .any ;
16
16
import static org .mockito .Mockito .atLeastOnce ;
17
17
import static org .mockito .Mockito .doAnswer ;
18
- import static org .mockito .Mockito .doReturn ;
19
18
import static org .mockito .Mockito .times ;
20
19
import static org .mockito .Mockito .verify ;
21
20
import static org .mockito .Mockito .when ;
27
26
import io .kubernetes .client .openapi .ApiException ;
28
27
import java .net .HttpURLConnection ;
29
28
import java .time .Duration ;
29
+ import java .util .concurrent .Semaphore ;
30
30
import java .util .concurrent .atomic .AtomicReference ;
31
31
import org .junit .Test ;
32
32
import org .junit .runner .RunWith ;
@@ -40,22 +40,15 @@ public class LeaderElectingControllerTest {
40
40
41
41
@ Mock private Lock mockLock ;
42
42
43
- private final int stepCooldownIntervalInMillis = 2000 ;
44
-
45
- private void cooldown () {
46
- try {
47
- Thread .sleep (stepCooldownIntervalInMillis );
48
- } catch (InterruptedException e ) {
49
- e .printStackTrace ();
50
- }
51
- }
52
-
53
43
@ Test
54
- public void testLeaderElectingController () throws ApiException {
44
+ public void testLeaderElectingController () throws ApiException , InterruptedException {
55
45
56
46
AtomicReference <LeaderElectionRecord > record = new AtomicReference <>();
57
47
record .set (new LeaderElectionRecord ());
58
48
49
+ Semaphore latch = new Semaphore (2 );
50
+ Semaphore controllerLatch = new Semaphore (2 );
51
+
59
52
when (mockLock .identity ()).thenReturn ("foo" );
60
53
when (mockLock .get ())
61
54
.thenThrow (
@@ -65,12 +58,35 @@ public void testLeaderElectingController() throws ApiException {
65
58
doAnswer (
66
59
invocationOnMock -> {
67
60
record .set (invocationOnMock .getArgument (0 ));
61
+ latch .release ();
68
62
return true ;
69
63
})
70
64
.when (mockLock )
71
65
.create (any ());
72
66
73
- doReturn (false ).when (mockLock ).update (any ());
67
+ doAnswer (
68
+ invocationOnMock -> {
69
+ latch .release ();
70
+ return false ;
71
+ })
72
+ .when (mockLock )
73
+ .update (any ());
74
+
75
+ doAnswer (
76
+ invocationOnMock -> {
77
+ controllerLatch .release ();
78
+ return null ;
79
+ })
80
+ .when (mockController )
81
+ .run ();
82
+
83
+ doAnswer (
84
+ invocationOnMock -> {
85
+ controllerLatch .release ();
86
+ return null ;
87
+ })
88
+ .when (mockController )
89
+ .shutdown ();
74
90
75
91
LeaderElectingController leaderElectingController =
76
92
new LeaderElectingController (
@@ -82,14 +98,18 @@ public void testLeaderElectingController() throws ApiException {
82
98
Duration .ofMillis (100 ))),
83
99
mockController );
84
100
101
+ latch .acquire (2 );
102
+ controllerLatch .acquire (2 );
103
+
85
104
Thread controllerThread = new Thread (leaderElectingController ::run );
86
105
controllerThread .start ();
87
- cooldown ( );
106
+ latch . acquire ( 2 );
88
107
controllerThread .interrupt ();
89
108
90
109
verify (mockLock , times (1 )).create (any ());
91
110
verify (mockLock , atLeastOnce ()).update (any ());
92
111
112
+ controllerLatch .acquire (2 );
93
113
verify (mockController , times (1 )).run ();
94
114
verify (mockController , times (1 )).shutdown ();
95
115
}
0 commit comments