@@ -826,16 +826,130 @@ void testOnNext_WithEndOfStream() {
826
826
connectionSpy .onNext (response );
827
827
828
828
// Assert connection restarts after the last verified block number
829
- verify (connectionSpy ).endStreamAndRestartAtBlock (endOfStream .blockNumber () + 1L );
829
+ verify (connectionSpy , times (1 )).endStreamAndRestartAtBlock (endOfStream .blockNumber () + 1L );
830
+ verify (connectionSpy , times (1 )).close ();
831
+ verify (connectionSpy , times (1 )).setCurrentBlockNumber (endOfStream .blockNumber () + 1L );
832
+ verify (connectionSpy , times (1 )).establishStream ();
833
+
834
+ assertEquals (endOfStream .blockNumber () + 1L , connection .getCurrentBlockNumber ());
835
+ assertEquals (0 , connection .getCurrentRequestIndex ());
830
836
831
837
// Verify log messages for end of stream
832
- final String expectedLog = "Received EndOfStream from block node" ;
838
+ final String expectedLog =
839
+ "Received EndOfStream from block node " + TEST_CONNECTION_DESCRIPTOR + " at block " + TEST_BLOCK_NUMBER ;
833
840
assertTrue (
834
841
logCaptor .debugLogs ().stream ().anyMatch (log -> log .contains (expectedLog ))
835
842
|| logCaptor .errorLogs ().stream ().anyMatch (log -> log .contains (expectedLog )),
836
843
"Expected log message not found: " + expectedLog );
837
844
}
838
845
846
+ /**
847
+ * Tests the onNext method handling a PublishStreamResponse
848
+ * with a ResendBlock for the next block after the last verified one.
849
+ */
850
+ @ Test
851
+ void testOnNext_WithResendBlock () {
852
+ // Arrange
853
+ final BlockNodeConnection connectionSpy = spy (connection );
854
+ final PublishStreamResponse .ResendBlock resendBlock = PublishStreamResponse .ResendBlock .newBuilder ()
855
+ .blockNumber (TEST_BLOCK_NUMBER )
856
+ .build ();
857
+ final PublishStreamResponse response =
858
+ PublishStreamResponse .newBuilder ().resendBlock (resendBlock ).build ();
859
+
860
+ when (connectionManager .getLastVerifiedBlock (blockNodeConfig )).thenReturn (TEST_BLOCK_NUMBER - 1L );
861
+ // Act
862
+ connectionSpy .onNext (response );
863
+
864
+ // Assert connection restarts after the last verified block number
865
+ verify (connectionSpy , times (1 )).endStreamAndRestartAtBlock (resendBlock .blockNumber ());
866
+ verify (connectionSpy , times (1 )).close ();
867
+ verify (connectionSpy , times (1 )).setCurrentBlockNumber (resendBlock .blockNumber ());
868
+ verify (connectionSpy , times (1 )).establishStream ();
869
+
870
+ assertEquals (resendBlock .blockNumber (), connection .getCurrentBlockNumber ());
871
+ assertEquals (0 , connection .getCurrentRequestIndex ());
872
+
873
+ // Verify log messages for resend block
874
+ final String expectedLog = "Received ResendBlock from block node " + TEST_CONNECTION_DESCRIPTOR + " for block "
875
+ + TEST_BLOCK_NUMBER ;
876
+ assertTrue (
877
+ logCaptor .debugLogs ().stream ().anyMatch (log -> log .contains (expectedLog )),
878
+ "Expected log message not found: " + expectedLog );
879
+ final String expectedLogForCorrectResendBlock = "Restarting stream at the next block " + TEST_BLOCK_NUMBER
880
+ + " after the last verified one for block node " + TEST_CONNECTION_DESCRIPTOR ;
881
+ assertTrue (
882
+ logCaptor .debugLogs ().stream ().anyMatch (log -> log .contains (expectedLogForCorrectResendBlock )),
883
+ "Expected log message not found: " + expectedLogForCorrectResendBlock );
884
+ }
885
+
886
+ /**
887
+ * Tests the onNext method handling a PublishStreamResponse
888
+ * with a ResendBlock for the next block after the last verified one.
889
+ */
890
+ @ Test
891
+ void testOnNext_WithResendBlockDifferentThanExpected () {
892
+ final var lastVerifiedBlockNumber = TEST_BLOCK_NUMBER * 2L ;
893
+
894
+ // Arrange
895
+ final BlockNodeConnection connectionSpy = spy (connection );
896
+ final PublishStreamResponse .ResendBlock resendBlock = PublishStreamResponse .ResendBlock .newBuilder ()
897
+ .blockNumber (TEST_BLOCK_NUMBER )
898
+ .build ();
899
+ final PublishStreamResponse response =
900
+ PublishStreamResponse .newBuilder ().resendBlock (resendBlock ).build ();
901
+
902
+ when (connectionManager .getLastVerifiedBlock (blockNodeConfig )).thenReturn (lastVerifiedBlockNumber );
903
+
904
+ // Act
905
+ connectionSpy .onNext (response );
906
+
907
+ // Verify log messages for resend block
908
+ final String expectedLog = "Received ResendBlock from block node " + TEST_CONNECTION_DESCRIPTOR + " for block "
909
+ + TEST_BLOCK_NUMBER ;
910
+ assertTrue (
911
+ logCaptor .debugLogs ().stream ().anyMatch (log -> log .contains (expectedLog )),
912
+ "Expected log message not found: " + expectedLog );
913
+ final String expectedLogForDifferentResendBlock = "Received ResendBlock for block " + TEST_BLOCK_NUMBER
914
+ + " but last verified block is " + lastVerifiedBlockNumber ;
915
+ assertTrue (
916
+ logCaptor .warnLogs ().stream ().anyMatch (log -> log .contains (expectedLogForDifferentResendBlock )),
917
+ "Expected log message not found: " + expectedLogForDifferentResendBlock );
918
+ }
919
+
920
+ /**
921
+ * Tests the onNext method handling a PublishStreamResponse
922
+ * with a ResendBlock for already acknowledged block.
923
+ */
924
+ @ Test
925
+ void testOnNext_WithResendBlockForAlreadyAcknowledgedBlock () {
926
+ // Arrange
927
+ final BlockNodeConnection connectionSpy = spy (connection );
928
+ final PublishStreamResponse .ResendBlock resendBlock = PublishStreamResponse .ResendBlock .newBuilder ()
929
+ .blockNumber (TEST_BLOCK_NUMBER )
930
+ .build ();
931
+ final PublishStreamResponse response =
932
+ PublishStreamResponse .newBuilder ().resendBlock (resendBlock ).build ();
933
+
934
+ when (connectionManager .isBlockAlreadyAcknowledged (TEST_BLOCK_NUMBER )).thenReturn (true );
935
+
936
+ // Act
937
+ connectionSpy .onNext (response );
938
+
939
+ // Verify log messages for resend block
940
+ final String expectedLog = "Received ResendBlock from block node " + TEST_CONNECTION_DESCRIPTOR + " for block "
941
+ + TEST_BLOCK_NUMBER ;
942
+ assertTrue (
943
+ logCaptor .debugLogs ().stream ().anyMatch (log -> log .contains (expectedLog )),
944
+ "Expected log message not found: " + expectedLog );
945
+ final String expectedLogForResendBlockAlreadyAcknowledged = "Block " + TEST_BLOCK_NUMBER
946
+ + " already acknowledged, skipping resend for block node " + TEST_CONNECTION_DESCRIPTOR ;
947
+ assertTrue (
948
+ logCaptor .debugLogs ().stream ()
949
+ .anyMatch (log -> log .contains (expectedLogForResendBlockAlreadyAcknowledged )),
950
+ "Expected log message not found: " + expectedLogForResendBlockAlreadyAcknowledged );
951
+ }
952
+
839
953
/**
840
954
* Tests the onError method when an error is received from the stream.
841
955
*/
0 commit comments