36
36
import org .apache .zookeeper .data .ACL ;
37
37
import org .apache .zookeeper .data .Stat ;
38
38
import org .apache .zookeeper .ZooDefs .Ids ;
39
- import org .junit .Before ;
40
- import org .junit .Test ;
41
- import org .junit .Assert ;
39
+ import org .junit .jupiter .api .BeforeEach ;
40
+ import org .junit .jupiter .api .Test ;
42
41
import org .mockito .ArgumentCaptor ;
43
42
import org .mockito .Mockito ;
44
43
49
48
import org .apache .hadoop .util .ZKUtil .ZKAuthInfo ;
50
49
import org .apache .hadoop .test .GenericTestUtils ;
51
50
52
- import static org .mockito .ArgumentMatchers .any ;
51
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
52
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
53
+ import static org .junit .jupiter .api .Assertions .assertNull ;
54
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
55
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
56
+ import static org .junit .jupiter .api .Assertions .fail ;
57
+ import static org .mockito .Mockito .any ;
53
58
54
59
public class TestActiveStandbyElector {
55
60
@@ -92,7 +97,7 @@ protected void sleepFor(int ms) {
92
97
private static final String ZK_BREADCRUMB_NAME = ZK_PARENT_NAME + "/" +
93
98
ActiveStandbyElector .BREADCRUMB_FILENAME ;
94
99
95
- @ Before
100
+ @ BeforeEach
96
101
public void init () throws IOException , KeeperException {
97
102
count = 0 ;
98
103
mockZK = Mockito .mock (ZooKeeper .class );
@@ -123,9 +128,10 @@ private void mockPriorActive(byte[] data) throws Exception {
123
128
/**
124
129
* verify that joinElection checks for null data
125
130
*/
126
- @ Test ( expected = HadoopIllegalArgumentException . class )
131
+ @ Test
127
132
public void testJoinElectionException () {
128
- elector .joinElection (null );
133
+ assertThrows (HadoopIllegalArgumentException .class ,
134
+ () -> elector .joinElection (null ));
129
135
}
130
136
131
137
/**
@@ -177,7 +183,7 @@ public void testCreateNodeResultBecomeActive() throws Exception {
177
183
public void testFailToBecomeActive () throws Exception {
178
184
mockNoPriorActive ();
179
185
elector .joinElection (data );
180
- Assert . assertEquals (0 , elector .sleptFor );
186
+ assertEquals (0 , elector .sleptFor );
181
187
182
188
Mockito .doThrow (new ServiceFailedException ("failed to become active" ))
183
189
.when (mockApp ).becomeActive ();
@@ -189,8 +195,8 @@ public void testFailToBecomeActive() throws Exception {
189
195
// should re-join
190
196
Mockito .verify (mockZK , Mockito .times (2 )).create (ZK_LOCK_NAME , data ,
191
197
Ids .OPEN_ACL_UNSAFE , CreateMode .EPHEMERAL , elector , mockZK );
192
- Assert . assertEquals (2 , count );
193
- Assert . assertTrue (elector .sleptFor > 0 );
198
+ assertEquals (2 , count );
199
+ assertTrue (elector .sleptFor > 0 );
194
200
}
195
201
196
202
/**
@@ -202,7 +208,7 @@ public void testFailToBecomeActive() throws Exception {
202
208
public void testFailToBecomeActiveAfterZKDisconnect () throws Exception {
203
209
mockNoPriorActive ();
204
210
elector .joinElection (data );
205
- Assert . assertEquals (0 , elector .sleptFor );
211
+ assertEquals (0 , elector .sleptFor );
206
212
207
213
elector .processResult (Code .CONNECTIONLOSS .intValue (), ZK_LOCK_NAME , mockZK ,
208
214
ZK_LOCK_NAME );
@@ -226,8 +232,8 @@ public void testFailToBecomeActiveAfterZKDisconnect() throws Exception {
226
232
// should re-join
227
233
Mockito .verify (mockZK , Mockito .times (3 )).create (ZK_LOCK_NAME , data ,
228
234
Ids .OPEN_ACL_UNSAFE , CreateMode .EPHEMERAL , elector , mockZK );
229
- Assert . assertEquals (2 , count );
230
- Assert . assertTrue (elector .sleptFor > 0 );
235
+ assertEquals (2 , count );
236
+ assertTrue (elector .sleptFor > 0 );
231
237
}
232
238
233
239
@@ -331,7 +337,7 @@ public void testCreateNodeResultRetryBecomeActive() throws Exception {
331
337
332
338
elector .joinElection (data );
333
339
// recreate connection via getNewZooKeeper
334
- Assert . assertEquals (2 , count );
340
+ assertEquals (2 , count );
335
341
elector .processResult (Code .CONNECTIONLOSS .intValue (), ZK_LOCK_NAME , mockZK ,
336
342
ZK_LOCK_NAME );
337
343
elector .processResult (Code .NODEEXISTS .intValue (), ZK_LOCK_NAME , mockZK ,
@@ -457,10 +463,10 @@ public void testProcessCallbackEventNone() throws Exception {
457
463
Event .KeeperState .SyncConnected );
458
464
elector .processWatchEvent (mockZK , mockEvent );
459
465
verifyExistCall (1 );
460
- Assert . assertTrue (elector .isMonitorLockNodePending ());
466
+ assertTrue (elector .isMonitorLockNodePending ());
461
467
elector .processResult (Code .SESSIONEXPIRED .intValue (), ZK_LOCK_NAME ,
462
468
mockZK , new Stat ());
463
- Assert . assertFalse (elector .isMonitorLockNodePending ());
469
+ assertFalse (elector .isMonitorLockNodePending ());
464
470
465
471
// session expired should enter safe mode and initiate re-election
466
472
// re-election checked via checking re-creation of new zookeeper and
@@ -471,7 +477,7 @@ public void testProcessCallbackEventNone() throws Exception {
471
477
Mockito .verify (mockApp , Mockito .times (1 )).enterNeutralMode ();
472
478
// called getNewZooKeeper to create new session. first call was in
473
479
// constructor
474
- Assert . assertEquals (2 , count );
480
+ assertEquals (2 , count );
475
481
// once in initial joinElection and one now
476
482
Mockito .verify (mockZK , Mockito .times (2 )).create (ZK_LOCK_NAME , data ,
477
483
Ids .OPEN_ACL_UNSAFE , CreateMode .EPHEMERAL , elector , mockZK );
@@ -504,13 +510,13 @@ public void testProcessCallbackEventNode() throws Exception {
504
510
ZK_LOCK_NAME );
505
511
Mockito .verify (mockApp , Mockito .times (1 )).becomeStandby ();
506
512
verifyExistCall (1 );
507
- Assert . assertTrue (elector .isMonitorLockNodePending ());
513
+ assertTrue (elector .isMonitorLockNodePending ());
508
514
509
515
Stat stat = new Stat ();
510
516
stat .setEphemeralOwner (0L );
511
517
Mockito .when (mockZK .getSessionId ()).thenReturn (1L );
512
518
elector .processResult (Code .OK .intValue (), ZK_LOCK_NAME , mockZK , stat );
513
- Assert . assertFalse (elector .isMonitorLockNodePending ());
519
+ assertFalse (elector .isMonitorLockNodePending ());
514
520
515
521
WatchedEvent mockEvent = Mockito .mock (WatchedEvent .class );
516
522
Mockito .when (mockEvent .getPath ()).thenReturn (ZK_LOCK_NAME );
@@ -520,18 +526,18 @@ public void testProcessCallbackEventNode() throws Exception {
520
526
Event .EventType .NodeDataChanged );
521
527
elector .processWatchEvent (mockZK , mockEvent );
522
528
verifyExistCall (2 );
523
- Assert . assertTrue (elector .isMonitorLockNodePending ());
529
+ assertTrue (elector .isMonitorLockNodePending ());
524
530
elector .processResult (Code .OK .intValue (), ZK_LOCK_NAME , mockZK , stat );
525
- Assert . assertFalse (elector .isMonitorLockNodePending ());
531
+ assertFalse (elector .isMonitorLockNodePending ());
526
532
527
533
// monitoring should be setup again after event is received
528
534
Mockito .when (mockEvent .getType ()).thenReturn (
529
535
Event .EventType .NodeChildrenChanged );
530
536
elector .processWatchEvent (mockZK , mockEvent );
531
537
verifyExistCall (3 );
532
- Assert . assertTrue (elector .isMonitorLockNodePending ());
538
+ assertTrue (elector .isMonitorLockNodePending ());
533
539
elector .processResult (Code .OK .intValue (), ZK_LOCK_NAME , mockZK , stat );
534
- Assert . assertFalse (elector .isMonitorLockNodePending ());
540
+ assertFalse (elector .isMonitorLockNodePending ());
535
541
536
542
// lock node deletion when in standby mode should create znode again
537
543
// successful znode creation enters active state and sets monitor
@@ -546,10 +552,10 @@ public void testProcessCallbackEventNode() throws Exception {
546
552
ZK_LOCK_NAME );
547
553
Mockito .verify (mockApp , Mockito .times (1 )).becomeActive ();
548
554
verifyExistCall (4 );
549
- Assert . assertTrue (elector .isMonitorLockNodePending ());
555
+ assertTrue (elector .isMonitorLockNodePending ());
550
556
stat .setEphemeralOwner (1L );
551
557
elector .processResult (Code .OK .intValue (), ZK_LOCK_NAME , mockZK , stat );
552
- Assert . assertFalse (elector .isMonitorLockNodePending ());
558
+ assertFalse (elector .isMonitorLockNodePending ());
553
559
554
560
// lock node deletion in active mode should enter neutral mode and create
555
561
// znode again successful znode creation enters active state and sets
@@ -564,17 +570,17 @@ public void testProcessCallbackEventNode() throws Exception {
564
570
ZK_LOCK_NAME );
565
571
Mockito .verify (mockApp , Mockito .times (2 )).becomeActive ();
566
572
verifyExistCall (5 );
567
- Assert . assertTrue (elector .isMonitorLockNodePending ());
573
+ assertTrue (elector .isMonitorLockNodePending ());
568
574
elector .processResult (Code .OK .intValue (), ZK_LOCK_NAME , mockZK , stat );
569
- Assert . assertFalse (elector .isMonitorLockNodePending ());
575
+ assertFalse (elector .isMonitorLockNodePending ());
570
576
571
577
// bad path name results in fatal error
572
578
Mockito .when (mockEvent .getPath ()).thenReturn (null );
573
579
elector .processWatchEvent (mockZK , mockEvent );
574
580
Mockito .verify (mockApp , Mockito .times (1 )).notifyFatalError (
575
581
"Unexpected watch error from Zookeeper" );
576
582
// fatal error means no new connection other than one from constructor
577
- Assert . assertEquals (1 , count );
583
+ assertEquals (1 , count );
578
584
// no new watches after fatal error
579
585
verifyExistCall (5 );
580
586
@@ -599,13 +605,13 @@ public void testSuccessiveStandbyCalls() {
599
605
ZK_LOCK_NAME );
600
606
Mockito .verify (mockApp , Mockito .times (1 )).becomeStandby ();
601
607
verifyExistCall (1 );
602
- Assert . assertTrue (elector .isMonitorLockNodePending ());
608
+ assertTrue (elector .isMonitorLockNodePending ());
603
609
604
610
Stat stat = new Stat ();
605
611
stat .setEphemeralOwner (0L );
606
612
Mockito .when (mockZK .getSessionId ()).thenReturn (1L );
607
613
elector .processResult (Code .OK .intValue (), ZK_LOCK_NAME , mockZK , stat );
608
- Assert . assertFalse (elector .isMonitorLockNodePending ());
614
+ assertFalse (elector .isMonitorLockNodePending ());
609
615
610
616
WatchedEvent mockEvent = Mockito .mock (WatchedEvent .class );
611
617
Mockito .when (mockEvent .getPath ()).thenReturn (ZK_LOCK_NAME );
@@ -644,7 +650,7 @@ public void testQuitElection() throws Exception {
644
650
byte [] data = new byte [8 ];
645
651
elector .joinElection (data );
646
652
// getNewZooKeeper called 2 times. once in constructor and once now
647
- Assert . assertEquals (2 , count );
653
+ assertEquals (2 , count );
648
654
elector .processResult (Code .NODEEXISTS .intValue (), ZK_LOCK_NAME , mockZK ,
649
655
ZK_LOCK_NAME );
650
656
Mockito .verify (mockApp , Mockito .times (1 )).becomeStandby ();
@@ -669,7 +675,7 @@ public void testGetActiveData() throws ActiveNotFoundException,
669
675
Mockito .when (
670
676
mockZK .getData (Mockito .eq (ZK_LOCK_NAME ), Mockito .eq (false ),
671
677
any ())).thenReturn (data );
672
- Assert . assertEquals (data , elector .getActiveData ());
678
+ assertEquals (data , elector .getActiveData ());
673
679
Mockito .verify (mockZK , Mockito .times (1 )).getData (
674
680
Mockito .eq (ZK_LOCK_NAME ), Mockito .eq (false ), any ());
675
681
@@ -680,7 +686,7 @@ public void testGetActiveData() throws ActiveNotFoundException,
680
686
new KeeperException .NoNodeException ());
681
687
try {
682
688
elector .getActiveData ();
683
- Assert . fail ("ActiveNotFoundException expected" );
689
+ fail ("ActiveNotFoundException expected" );
684
690
} catch (ActiveNotFoundException e ) {
685
691
Mockito .verify (mockZK , Mockito .times (2 )).getData (
686
692
Mockito .eq (ZK_LOCK_NAME ), Mockito .eq (false ), any ());
@@ -693,7 +699,7 @@ public void testGetActiveData() throws ActiveNotFoundException,
693
699
any ())).thenThrow (
694
700
new KeeperException .AuthFailedException ());
695
701
elector .getActiveData ();
696
- Assert . fail ("KeeperException.AuthFailedException expected" );
702
+ fail ("KeeperException.AuthFailedException expected" );
697
703
} catch (KeeperException .AuthFailedException ke ) {
698
704
Mockito .verify (mockZK , Mockito .times (3 )).getData (
699
705
Mockito .eq (ZK_LOCK_NAME ), Mockito .eq (false ), any ());
@@ -762,7 +768,7 @@ public void testEnsureBaseNodeFails() throws Exception {
762
768
Mockito .eq (Ids .OPEN_ACL_UNSAFE ), Mockito .eq (CreateMode .PERSISTENT ));
763
769
try {
764
770
elector .ensureParentZNode ();
765
- Assert . fail ("Did not throw!" );
771
+ fail ("Did not throw!" );
766
772
} catch (IOException ioe ) {
767
773
if (!(ioe .getCause () instanceof KeeperException .ConnectionLossException )) {
768
774
throw ioe ;
@@ -791,7 +797,7 @@ protected ZooKeeper createZooKeeper() throws IOException {
791
797
};
792
798
793
799
794
- Assert . fail ("Did not throw zookeeper connection loss exceptions!" );
800
+ fail ("Did not throw zookeeper connection loss exceptions!" );
795
801
} catch (KeeperException ke ) {
796
802
GenericTestUtils .assertExceptionContains ( "ConnectionLoss" , ke );
797
803
}
@@ -842,14 +848,14 @@ protected synchronized ZooKeeper connectToZooKeeper() {
842
848
= ArgumentCaptor .forClass (ZKClientConfig .class );
843
849
Mockito .verify (e ).initiateZookeeper (configArgumentCaptor .capture ());
844
850
ZKClientConfig clientConfig = configArgumentCaptor .getValue ();
845
- Assert . assertEquals (defaultConfig .getProperty (ZKClientConfig .SECURE_CLIENT ),
846
- clientConfig .getProperty (ZKClientConfig .SECURE_CLIENT ));
847
- Assert . assertEquals (defaultConfig .getProperty (ZKClientConfig .ZOOKEEPER_CLIENT_CNXN_SOCKET ),
848
- clientConfig .getProperty (ZKClientConfig .ZOOKEEPER_CLIENT_CNXN_SOCKET ));
849
- Assert . assertNull (clientConfig .getProperty (clientX509Util .getSslKeystoreLocationProperty ()));
850
- Assert . assertNull (clientConfig .getProperty (clientX509Util .getSslKeystorePasswdProperty ()));
851
- Assert . assertNull (clientConfig .getProperty (clientX509Util .getSslTruststoreLocationProperty ()));
852
- Assert . assertNull (clientConfig .getProperty (clientX509Util .getSslTruststorePasswdProperty ()));
851
+ assertEquals (defaultConfig .getProperty (ZKClientConfig .SECURE_CLIENT ),
852
+ clientConfig .getProperty (ZKClientConfig .SECURE_CLIENT ));
853
+ assertEquals (defaultConfig .getProperty (ZKClientConfig .ZOOKEEPER_CLIENT_CNXN_SOCKET ),
854
+ clientConfig .getProperty (ZKClientConfig .ZOOKEEPER_CLIENT_CNXN_SOCKET ));
855
+ assertNull (clientConfig .getProperty (clientX509Util .getSslKeystoreLocationProperty ()));
856
+ assertNull (clientConfig .getProperty (clientX509Util .getSslKeystorePasswdProperty ()));
857
+ assertNull (clientConfig .getProperty (clientX509Util .getSslTruststoreLocationProperty ()));
858
+ assertNull (clientConfig .getProperty (clientX509Util .getSslTruststorePasswdProperty ()));
853
859
}
854
860
855
861
/**
@@ -882,17 +888,17 @@ protected synchronized ZooKeeper connectToZooKeeper() {
882
888
= ArgumentCaptor .forClass (ZKClientConfig .class );
883
889
Mockito .verify (e ).initiateZookeeper (configArgumentCaptor .capture ());
884
890
ZKClientConfig clientConfig = configArgumentCaptor .getValue ();
885
- Assert . assertEquals ("true" , clientConfig .getProperty (ZKClientConfig .SECURE_CLIENT ));
886
- Assert . assertEquals ("org.apache.zookeeper.ClientCnxnSocketNetty" ,
887
- clientConfig .getProperty (ZKClientConfig .ZOOKEEPER_CLIENT_CNXN_SOCKET ));
888
- Assert . assertEquals ("keystore_location" ,
889
- clientConfig .getProperty (clientX509Util .getSslKeystoreLocationProperty ()));
890
- Assert . assertEquals ("keystore_password" ,
891
- clientConfig .getProperty (clientX509Util .getSslKeystorePasswdProperty ()));
892
- Assert . assertEquals ("truststore_location" ,
893
- clientConfig .getProperty (clientX509Util .getSslTruststoreLocationProperty ()));
894
- Assert . assertEquals ("truststore_password" ,
895
- clientConfig .getProperty (clientX509Util .getSslTruststorePasswdProperty ()));
891
+ assertEquals ("true" , clientConfig .getProperty (ZKClientConfig .SECURE_CLIENT ));
892
+ assertEquals ("org.apache.zookeeper.ClientCnxnSocketNetty" ,
893
+ clientConfig .getProperty (ZKClientConfig .ZOOKEEPER_CLIENT_CNXN_SOCKET ));
894
+ assertEquals ("keystore_location" ,
895
+ clientConfig .getProperty (clientX509Util .getSslKeystoreLocationProperty ()));
896
+ assertEquals ("keystore_password" ,
897
+ clientConfig .getProperty (clientX509Util .getSslKeystorePasswdProperty ()));
898
+ assertEquals ("truststore_location" ,
899
+ clientConfig .getProperty (clientX509Util .getSslTruststoreLocationProperty ()));
900
+ assertEquals ("truststore_password" ,
901
+ clientConfig .getProperty (clientX509Util .getSslTruststorePasswdProperty ()));
896
902
897
903
}
898
904
}
0 commit comments