@@ -290,6 +290,65 @@ public void sendMessage(Message message) {
290290 }
291291 }
292292
293+ @ Test
294+ public void testDisconnectBeforeReservationReleasesRequestForRetry () throws Exception {
295+ SyncService sync = new SyncService ();
296+ try {
297+ TronNetDelegate delegate = mock (TronNetDelegate .class );
298+ ReflectUtils .setFieldValue (sync , "tronNetDelegate" , delegate );
299+ PeerConnection closing = Mockito .spy (new PeerConnection ());
300+ PeerConnection survivor = Mockito .spy (new PeerConnection ());
301+ Channel channel = new Channel ();
302+ ReflectUtils .setFieldValue (channel , "inetSocketAddress" , inetSocketAddress );
303+ ReflectUtils .setFieldValue (channel , "inetAddress" , inetSocketAddress .getAddress ());
304+ closing .setChannel (channel );
305+ Channel otherChannel = mock (Channel .class );
306+ InetSocketAddress otherAddress = new InetSocketAddress ("127.0.0.3" , 10001 );
307+ Mockito .when (otherChannel .getInetSocketAddress ()).thenReturn (otherAddress );
308+ Mockito .when (otherChannel .getInetAddress ()).thenReturn (otherAddress .getAddress ());
309+ survivor .setChannel (otherChannel );
310+ closing .setFetchAble (true );
311+ survivor .setFetchAble (true );
312+ Mockito .doNothing ().when (closing ).sendMessage (Mockito .any (Message .class ));
313+ Mockito .doNothing ().when (survivor ).sendMessage (Mockito .any (Message .class ));
314+ BlockCapsule .BlockId blockId = new BlockCapsule .BlockId (Sha256Hash .ZERO_HASH , 1 );
315+ closing .getSyncBlockToFetch ().add (blockId );
316+ survivor .getSyncBlockToFetch ().add (blockId );
317+ Mockito .when (delegate .getActivePeer ()).thenReturn (Arrays .asList (closing , survivor ));
318+ Cache <BlockCapsule .BlockId , PeerConnection > requests = Mockito .spy (
319+ (Cache <BlockCapsule .BlockId , PeerConnection >)
320+ ReflectUtils .getFieldObject (sync , "requestBlockIds" ));
321+ ReflectUtils .setFieldValue (sync , "requestBlockIds" , requests );
322+ Mockito .doAnswer (call -> {
323+ // Finish disconnect cleanup after selection but before the reservation is written.
324+ channel .markDisconnected ();
325+ Mockito .when (delegate .getActivePeer ()).thenReturn (Collections .singletonList (survivor ));
326+ sync .onDisconnect (closing );
327+ closing .getSyncBlockToFetch ().clear ();
328+ closing .getSyncBlockRequested ().clear ();
329+ Assert .assertNull (requests .getIfPresent (blockId ));
330+ return call .callRealMethod ();
331+ }).when (requests ).put (blockId , closing );
332+
333+ ReflectUtils .invokeMethod (sync , "startFetchSyncBlock" );
334+
335+ Assert .assertTrue (closing .isDisconnect ());
336+ Assert .assertNull (requests .getIfPresent (blockId ));
337+ Assert .assertFalse (requests .asMap ().containsValue (closing ));
338+ Assert .assertTrue ((boolean ) ReflectUtils .getFieldObject (sync , "fetchFlag" ));
339+ Mockito .verify (closing , Mockito .never ()).sendMessage (Mockito .any (Message .class ));
340+ Mockito .verify (survivor , Mockito .never ()).sendMessage (Mockito .any (Message .class ));
341+
342+ ReflectUtils .invokeMethod (sync , "startFetchSyncBlock" );
343+
344+ Assert .assertSame (survivor , requests .getIfPresent (blockId ));
345+ Assert .assertTrue (survivor .getSyncBlockRequested ().containsKey (blockId ));
346+ Mockito .verify (survivor ).sendMessage (Mockito .any (FetchInvDataMessage .class ));
347+ } finally {
348+ sync .close ();
349+ }
350+ }
351+
293352 @ Test
294353 public void testStartFetchSyncBlock () throws Exception {
295354 Field field = PeerManager .class .getDeclaredField ("peers" );
0 commit comments