2323import static org .junit .Assert .assertSame ;
2424import static org .junit .Assert .assertTrue ;
2525import static org .mockito .Mockito .mock ;
26+ import static org .mockito .Mockito .verify ;
2627import static org .mockito .Mockito .when ;
2728
2829import java .util .ArrayList ;
3940import org .apache .hadoop .hbase .HBaseClassTestRule ;
4041import org .apache .hadoop .hbase .HBaseConfiguration ;
4142import org .apache .hadoop .hbase .ServerName ;
43+ import org .apache .hadoop .hbase .client .AsyncClusterConnection ;
4244import org .apache .hadoop .hbase .master .HMaster ;
4345import org .apache .hadoop .hbase .master .MasterServices ;
4446import org .apache .hadoop .hbase .master .ServerManager ;
6163import org .junit .ClassRule ;
6264import org .junit .Test ;
6365import org .junit .experimental .categories .Category ;
66+ import org .mockito .Mockito ;
6467
6568import org .apache .hbase .thirdparty .com .google .common .collect .ImmutableMap ;
6669
@@ -77,16 +80,21 @@ public class TestReplicationLogCleaner {
7780
7881 private ReplicationLogCleaner cleaner ;
7982
83+ private ReplicationPeerManager rpm ;
84+
8085 @ Before
8186 public void setUp () throws ReplicationException {
8287 services = mock (MasterServices .class );
8388 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 );
8593 when (services .getReplicationPeerManager ()).thenReturn (rpm );
8694 when (rpm .listPeers (null )).thenReturn (new ArrayList <>());
8795 ReplicationQueueStorage rqs = mock (ReplicationQueueStorage .class );
8896 when (rpm .getQueueStorage ()).thenReturn (rqs );
89- when (rpm . getQueueStorage () .hasData ()).thenReturn (true );
97+ when (rqs .hasData ()).thenReturn (true );
9098 when (rqs .listAllQueues ()).thenReturn (new ArrayList <>());
9199 ServerManager sm = mock (ServerManager .class );
92100 when (services .getServerManager ()).thenReturn (sm );
@@ -383,4 +391,39 @@ public void testDeadRegionServerShouldDeleteTwoPeers() throws ReplicationExcepti
383391 assertSame (file , iter .next ());
384392 assertFalse (iter .hasNext ());
385393 }
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+ }
386429}
0 commit comments