Refine search
/** * see #testTruncateNotCalledIfSizeIsSameAsTargetSize */ @Test public void testTruncateIfSizeIsDifferentToTargetSize() throws IOException { FileChannel channelMock = mock(FileChannel.class); when(channelMock.size()).thenReturn(42L); when(channelMock.truncate(anyLong())).thenReturn(channelMock); FileRecords fileRecords = new FileRecords(tempFile(), channelMock, 0, Integer.MAX_VALUE, false); fileRecords.truncateTo(23); verify(channelMock, atLeastOnce()).size(); verify(channelMock).truncate(23); }
/** * Test that truncateTo only calls truncate on the FileChannel if the size of the * FileChannel is bigger than the target size. This is important because some JVMs * change the mtime of the file, even if truncate should do nothing. */ @Test public void testTruncateNotCalledIfSizeIsSameAsTargetSize() throws IOException { FileChannel channelMock = mock(FileChannel.class); when(channelMock.size()).thenReturn(42L); when(channelMock.position(42L)).thenReturn(null); FileRecords fileRecords = new FileRecords(tempFile(), channelMock, 0, Integer.MAX_VALUE, false); fileRecords.truncateTo(42); verify(channelMock, atLeastOnce()).size(); verify(channelMock, times(0)).truncate(anyLong()); }
@Test public void testReadFullyIfEofIsReached() throws IOException { final FileChannel channelMock = mock(FileChannel.class); final int bufferSize = 100; final String fileChannelContent = "abcdefghkl"; ByteBuffer buffer = ByteBuffer.allocate(bufferSize); when(channelMock.read(any(), anyLong())).then(invocation -> { ByteBuffer bufferArg = invocation.getArgument(0); bufferArg.put(fileChannelContent.getBytes()); return -1; }); Utils.readFully(channelMock, buffer, 0L); assertEquals("abcdefghkl", new String(buffer.array(), 0, buffer.position())); assertEquals(fileChannelContent.length(), buffer.position()); assertTrue(buffer.hasRemaining()); verify(channelMock, atLeastOnce()).read(any(), anyLong()); }
/** * Tests that `readFullyOrFail` behaves correctly if multiple `FileChannel.read` operations are required to fill * the destination buffer. */ @Test public void testReadFullyOrFailWithPartialFileChannelReads() throws IOException { FileChannel channelMock = mock(FileChannel.class); final int bufferSize = 100; ByteBuffer buffer = ByteBuffer.allocate(bufferSize); String expectedBufferContent = fileChannelMockExpectReadWithRandomBytes(channelMock, bufferSize); Utils.readFullyOrFail(channelMock, buffer, 0L, "test"); assertEquals("The buffer should be populated correctly", expectedBufferContent, new String(buffer.array())); assertFalse("The buffer should be filled", buffer.hasRemaining()); verify(channelMock, atLeastOnce()).read(any(), anyLong()); }
/** * Tests that `readFullyOrFail` behaves correctly if multiple `FileChannel.read` operations are required to fill * the destination buffer. */ @Test public void testReadFullyWithPartialFileChannelReads() throws IOException { FileChannel channelMock = mock(FileChannel.class); final int bufferSize = 100; String expectedBufferContent = fileChannelMockExpectReadWithRandomBytes(channelMock, bufferSize); ByteBuffer buffer = ByteBuffer.allocate(bufferSize); Utils.readFully(channelMock, buffer, 0L); assertEquals("The buffer should be populated correctly.", expectedBufferContent, new String(buffer.array())); assertFalse("The buffer should be filled", buffer.hasRemaining()); verify(channelMock, atLeastOnce()).read(any(), anyLong()); }
@Test @SuppressWarnings({ "deprecation", "unchecked" }) public void shouldRegInReportEveryIncomingRow() throws IOException { int iterationNumber = 999; RowCounter.RowCounterMapper mapper = new RowCounter.RowCounterMapper(); Reporter reporter = mock(Reporter.class); for (int i = 0; i < iterationNumber; i++) mapper.map(mock(ImmutableBytesWritable.class), mock(Result.class), mock(OutputCollector.class), reporter); Mockito.verify(reporter, times(iterationNumber)).incrCounter( any(), anyLong()); }
@Test public void awaitAndShutdownInterrupt() throws InterruptedException { ExecutorService executor = Mockito.mock(ExecutorService.class); doThrow(new InterruptedException("boom")) .when(executor).awaitTermination(anyLong(), any()); EventLoopProcessor<String> interruptingProcessor = initProcessor(executor); boolean result = interruptingProcessor.awaitAndShutdown(Duration.ofMillis(100)); assertThat(Thread.currentThread().isInterrupted()).as("interrupted").isTrue(); assertThat(result).as("await failed").isFalse(); }
@Test public void markForTerminationReturnsTrueWhenSuccessful() { KernelTransactionImplementation tx = mock( KernelTransactionImplementation.class ); when( tx.getReuseCount() ).thenReturn( 42 ); when( tx.markForTermination( anyLong(), any() ) ).thenReturn( true ); KernelTransactionImplementationHandle handle = new KernelTransactionImplementationHandle( tx, clock ); assertTrue( handle.markForTermination( Status.Transaction.Terminated ) ); }
@Test public void markForTerminationReturnsFalseWhenNotSuccessful() { KernelTransactionImplementation tx = mock( KernelTransactionImplementation.class ); when( tx.getReuseCount() ).thenReturn( 42 ); when( tx.markForTermination( anyLong(), any() ) ).thenReturn( false ); KernelTransactionImplementationHandle handle = new KernelTransactionImplementationHandle( tx, clock ); assertFalse( handle.markForTermination( Status.Transaction.Terminated ) ); }
@Test public void shouldResumeWhenWritableOnceAgain() throws Exception { // given TransportThrottle throttle = newThrottleAndInstall( channel ); when( channel.isWritable() ).thenReturn( false ).thenReturn( true ); // when throttle.acquire( channel ); // expect verify( lock, atLeast( 1 ) ).lock( any(), anyLong() ); verify( lock, never() ).unlock( any() ); }
@Test public void shouldNotWaitIfNoSequenceNumberSpecified() throws Exception { // Given: when(request.getCommandSequenceNumber()).thenReturn(Optional.empty()); // When: CommandStoreUtil.waitForCommandSequenceNumber(commandQueue, request, TIMEOUT); // Then: verify(commandQueue, never()).ensureConsumedPast(anyLong(), any()); }
@Test public void blockWhenPollConditionNotSatisfied() { long timeout = 4000L; NetworkClient mockNetworkClient = mock(NetworkClient.class); ConsumerNetworkClient consumerClient = new ConsumerNetworkClient(new LogContext(), mockNetworkClient, metadata, time, 100, 1000, Integer.MAX_VALUE); when(mockNetworkClient.inFlightRequestCount()).thenReturn(1); consumerClient.poll(time.timer(timeout), () -> true); verify(mockNetworkClient).poll(eq(timeout), anyLong()); }
@Test public void blockOnlyForRetryBackoffIfNoInflightRequests() { long retryBackoffMs = 100L; NetworkClient mockNetworkClient = mock(NetworkClient.class); ConsumerNetworkClient consumerClient = new ConsumerNetworkClient(new LogContext(), mockNetworkClient, metadata, time, retryBackoffMs, 1000, Integer.MAX_VALUE); when(mockNetworkClient.inFlightRequestCount()).thenReturn(0); consumerClient.poll(time.timer(Long.MAX_VALUE), () -> true); verify(mockNetworkClient).poll(eq(retryBackoffMs), anyLong()); }
@Test public void shouldNotifyMessageProcessingStartedAndCompleted() { BoltConnectionMetricsMonitor metricsMonitor = mock( BoltConnectionMetricsMonitor.class ); BoltConnection connection = newConnection( metricsMonitor ); connection.start(); connection.enqueue( machine -> { } ); connection.processNextBatch(); verify( metricsMonitor ).messageProcessingStarted( anyLong() ); verify( metricsMonitor ).messageProcessingCompleted( anyLong() ); }