|
28 | 28 | import static org.mockito.Mockito.doNothing; |
29 | 29 | import static org.mockito.Mockito.doReturn; |
30 | 30 | import static org.mockito.Mockito.mock; |
| 31 | +import static org.mockito.Mockito.mockStatic; |
31 | 32 | import static org.mockito.Mockito.never; |
32 | 33 | import static org.mockito.Mockito.spy; |
33 | 34 | import static org.mockito.Mockito.times; |
|
93 | 94 | import org.apache.pulsar.common.protocol.Commands; |
94 | 95 | import org.apache.pulsar.common.util.Backoff; |
95 | 96 | import org.awaitility.Awaitility; |
| 97 | +import org.mockito.MockedStatic; |
96 | 98 | import org.testng.Assert; |
97 | 99 | import org.testng.annotations.AfterMethod; |
98 | 100 | import org.testng.annotations.BeforeMethod; |
@@ -1050,6 +1052,88 @@ public void testSeekAsyncInternal() { |
1050 | 1052 | verify(cnx, times(1)).sendRequestWithId(any(ByteBuf.class), anyLong()); |
1051 | 1053 | } |
1052 | 1054 |
|
| 1055 | + @Test |
| 1056 | + public void testSeekAsyncReleasesBufferOnInProgressConflict() { |
| 1057 | + ClientCnx cnx = mock(ClientCnx.class); |
| 1058 | + CompletableFuture<ProducerResponse> clientReq = new CompletableFuture<>(); |
| 1059 | + when(cnx.sendRequestWithId(any(ByteBuf.class), anyLong())).thenAnswer(inv -> { |
| 1060 | + inv.<ByteBuf>getArgument(0).release(); |
| 1061 | + return clientReq; |
| 1062 | + }); |
| 1063 | + consumer.setClientCnx(cnx); |
| 1064 | + consumer.setState(HandlerState.State.Ready); |
| 1065 | + |
| 1066 | + List<ByteBuf> capturedBuffers = new ArrayList<>(); |
| 1067 | + try (MockedStatic<Commands> commands = mockStatic(Commands.class, inv -> { |
| 1068 | + Object result = inv.callRealMethod(); |
| 1069 | + if ("newSeek".equals(inv.getMethod().getName())) { |
| 1070 | + capturedBuffers.add((ByteBuf) result); |
| 1071 | + } |
| 1072 | + return result; |
| 1073 | + })) { |
| 1074 | + CompletableFuture<Void> firstResult = consumer.seekAsync(1L); |
| 1075 | + CompletableFuture<Void> secondResult = consumer.seekAsync(1L); |
| 1076 | + |
| 1077 | + assertThat(capturedBuffers).hasSize(2); |
| 1078 | + assertThat(capturedBuffers.get(1).refCnt()).isZero(); |
| 1079 | + assertTrue(secondResult.isCompletedExceptionally()); |
| 1080 | + } |
| 1081 | + } |
| 1082 | + |
| 1083 | + @Test |
| 1084 | + public void testSeekAsyncReleasesBufferOnTimeout() { |
| 1085 | + ScheduledExecutorProvider provider = mock(ScheduledExecutorProvider.class); |
| 1086 | + ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class); |
| 1087 | + when(provider.getExecutor()).thenReturn(scheduledExecutorService); |
| 1088 | + when(consumer.getClient().getScheduledExecutorProvider()).thenReturn(provider); |
| 1089 | + |
| 1090 | + doAnswer(invocation -> { |
| 1091 | + ((Runnable) invocation.getArgument(0)).run(); |
| 1092 | + return null; |
| 1093 | + }).when(scheduledExecutorService).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class)); |
| 1094 | + |
| 1095 | + List<ByteBuf> capturedBuffers = new ArrayList<>(); |
| 1096 | + try (MockedStatic<Commands> commands = mockStatic(Commands.class, inv -> { |
| 1097 | + Object result = inv.callRealMethod(); |
| 1098 | + if ("newSeek".equals(inv.getMethod().getName())) { |
| 1099 | + capturedBuffers.add((ByteBuf) result); |
| 1100 | + } |
| 1101 | + return result; |
| 1102 | + })) { |
| 1103 | + CompletableFuture<Void> result = consumer.seekAsync(1L); |
| 1104 | + |
| 1105 | + assertThat(capturedBuffers).hasSize(1); |
| 1106 | + assertThat(capturedBuffers.get(0).refCnt()).isZero(); |
| 1107 | + assertTrue(result.isCompletedExceptionally()); |
| 1108 | + } |
| 1109 | + } |
| 1110 | + |
| 1111 | + @Test |
| 1112 | + public void testSeekAsyncReleasesBufferOnRejectedRetry() { |
| 1113 | + ScheduledExecutorProvider provider = mock(ScheduledExecutorProvider.class); |
| 1114 | + ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class); |
| 1115 | + when(provider.getExecutor()).thenReturn(scheduledExecutorService); |
| 1116 | + when(consumer.getClient().getScheduledExecutorProvider()).thenReturn(provider); |
| 1117 | + |
| 1118 | + when(scheduledExecutorService.schedule(any(Runnable.class), anyLong(), any(TimeUnit.class))) |
| 1119 | + .thenThrow(new java.util.concurrent.RejectedExecutionException("shutdown")); |
| 1120 | + |
| 1121 | + List<ByteBuf> capturedBuffers = new ArrayList<>(); |
| 1122 | + try (MockedStatic<Commands> commands = mockStatic(Commands.class, inv -> { |
| 1123 | + Object result = inv.callRealMethod(); |
| 1124 | + if ("newSeek".equals(inv.getMethod().getName())) { |
| 1125 | + capturedBuffers.add((ByteBuf) result); |
| 1126 | + } |
| 1127 | + return result; |
| 1128 | + })) { |
| 1129 | + CompletableFuture<Void> result = consumer.seekAsync(1L); |
| 1130 | + |
| 1131 | + assertThat(capturedBuffers).hasSize(1); |
| 1132 | + assertThat(capturedBuffers.get(0).refCnt()).isZero(); |
| 1133 | + assertTrue(result.isCompletedExceptionally()); |
| 1134 | + } |
| 1135 | + } |
| 1136 | + |
1053 | 1137 | @Test(invocationTimeOut = 1000) |
1054 | 1138 | public void testAutoGenerateConsumerName() { |
1055 | 1139 | Pattern consumerNamePattern = Pattern.compile("[a-zA-Z0-9]{5}"); |
|