23
23
import static org .junit .Assert .assertSame ;
24
24
import static org .junit .Assert .assertTrue ;
25
25
import static org .mockito .Mockito .mock ;
26
+ import static org .mockito .Mockito .verify ;
26
27
import static org .mockito .Mockito .when ;
27
28
28
29
import java .util .ArrayList ;
39
40
import org .apache .hadoop .hbase .HBaseClassTestRule ;
40
41
import org .apache .hadoop .hbase .HBaseConfiguration ;
41
42
import org .apache .hadoop .hbase .ServerName ;
43
+ import org .apache .hadoop .hbase .client .AsyncClusterConnection ;
42
44
import org .apache .hadoop .hbase .master .HMaster ;
43
45
import org .apache .hadoop .hbase .master .MasterServices ;
44
46
import org .apache .hadoop .hbase .master .ServerManager ;
61
63
import org .junit .ClassRule ;
62
64
import org .junit .Test ;
63
65
import org .junit .experimental .categories .Category ;
66
+ import org .mockito .Mockito ;
64
67
65
68
import org .apache .hbase .thirdparty .com .google .common .collect .ImmutableMap ;
66
69
@@ -77,16 +80,21 @@ public class TestReplicationLogCleaner {
77
80
78
81
private ReplicationLogCleaner cleaner ;
79
82
83
+ private ReplicationPeerManager rpm ;
84
+
80
85
@ Before
81
86
public void setUp () throws ReplicationException {
82
87
services = mock (MasterServices .class );
83
88
when (services .getReplicationLogCleanerBarrier ()).thenReturn (new ReplicationLogCleanerBarrier ());
84
- ReplicationPeerManager rpm = mock (ReplicationPeerManager .class );
89
+ AsyncClusterConnection asyncClusterConnection = mock (AsyncClusterConnection .class );
90
+ when (services .getAsyncClusterConnection ()).thenReturn (asyncClusterConnection );
91
+ when (asyncClusterConnection .isClosed ()).thenReturn (false );
92
+ rpm = mock (ReplicationPeerManager .class );
85
93
when (services .getReplicationPeerManager ()).thenReturn (rpm );
86
94
when (rpm .listPeers (null )).thenReturn (new ArrayList <>());
87
95
ReplicationQueueStorage rqs = mock (ReplicationQueueStorage .class );
88
96
when (rpm .getQueueStorage ()).thenReturn (rqs );
89
- when (rpm . getQueueStorage () .hasData ()).thenReturn (true );
97
+ when (rqs .hasData ()).thenReturn (true );
90
98
when (rqs .listAllQueues ()).thenReturn (new ArrayList <>());
91
99
ServerManager sm = mock (ServerManager .class );
92
100
when (services .getServerManager ()).thenReturn (sm );
@@ -383,4 +391,39 @@ public void testDeadRegionServerShouldDeleteTwoPeers() throws ReplicationExcepti
383
391
assertSame (file , iter .next ());
384
392
assertFalse (iter .hasNext ());
385
393
}
394
+
395
+ @ Test
396
+ public void testPreCleanWhenAsyncClusterConnectionClosed () throws ReplicationException {
397
+ assertFalse (services .getAsyncClusterConnection ().isClosed ());
398
+ verify (services .getAsyncClusterConnection (), Mockito .times (1 )).isClosed ();
399
+ cleaner .preClean ();
400
+ verify (services .getAsyncClusterConnection (), Mockito .times (2 )).isClosed ();
401
+ verify (rpm .getQueueStorage (), Mockito .times (1 )).hasData ();
402
+
403
+ when (services .getAsyncClusterConnection ().isClosed ()).thenReturn (true );
404
+ assertTrue (services .getAsyncClusterConnection ().isClosed ());
405
+ verify (services .getAsyncClusterConnection (), Mockito .times (3 )).isClosed ();
406
+ cleaner .preClean ();
407
+ verify (services .getAsyncClusterConnection (), Mockito .times (4 )).isClosed ();
408
+ // rpm.getQueueStorage().hasData() was not executed, indicating an early return.
409
+ verify (rpm .getQueueStorage (), Mockito .times (1 )).hasData ();
410
+ }
411
+
412
+ @ Test
413
+ public void testGetDeletableFilesWhenAsyncClusterConnectionClosed () throws ReplicationException {
414
+ List <FileStatus > files = List .of (new FileStatus ());
415
+ assertFalse (services .getAsyncClusterConnection ().isClosed ());
416
+ verify (services .getAsyncClusterConnection (), Mockito .times (1 )).isClosed ();
417
+ cleaner .getDeletableFiles (files );
418
+ verify (services .getAsyncClusterConnection (), Mockito .times (2 )).isClosed ();
419
+ verify (rpm .getQueueStorage (), Mockito .times (1 )).hasData ();
420
+
421
+ when (services .getAsyncClusterConnection ().isClosed ()).thenReturn (true );
422
+ assertTrue (services .getAsyncClusterConnection ().isClosed ());
423
+ verify (services .getAsyncClusterConnection (), Mockito .times (3 )).isClosed ();
424
+ cleaner .getDeletableFiles (files );
425
+ verify (services .getAsyncClusterConnection (), Mockito .times (4 )).isClosed ();
426
+ // rpm.getQueueStorage().hasData() was not executed, indicating an early return.
427
+ verify (rpm .getQueueStorage (), Mockito .times (1 )).hasData ();
428
+ }
386
429
}
0 commit comments