synchronized String sendWriteToStream(int streamId) { String stream = getStream(streamId); client.writeRecordSet(stream, recordSet) .addEventListener(this); return stream; }
@Test(timeout = 20000) public void testFlushWhenBufferIsFull() throws Exception { DistributedLogClient client = mock(DistributedLogClient.class); when(client.writeRecordSet((String) any(), (LogRecordSetBuffer) any())) .thenReturn(Future.value(new DLSN(1L, 1L, 999L))); ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); DistributedLogMultiStreamWriter writer = DistributedLogMultiStreamWriter.newBuilder() .streams(Lists.newArrayList("stream1", "stream2")) .client(client) .compressionCodec(CompressionCodec.Type.LZ4) .firstSpeculativeTimeoutMs(100000) .maxSpeculativeTimeoutMs(200000) .speculativeBackoffMultiplier(2) .requestTimeoutMs(500000) .flushIntervalMs(0) .bufferSize(0) .scheduler(executorService) .build(); ByteBuffer buffer = ByteBuffer.wrap("test".getBytes(UTF_8)); writer.write(buffer); verify(client, times(1)).writeRecordSet((String) any(), (LogRecordSetBuffer) any()); writer.close(); }
client.writeRecordSet(streamName, recordSetWriter) .addEventListener(new FutureEventListener<DLSN>() { @Override
throws Exception { DistributedLogClient client = mock(DistributedLogClient.class); when(client.writeRecordSet((String) any(), (LogRecordSetBuffer) any())) .thenReturn(Future.value(new DLSN(1L, 1L, 999L))); ScheduledExecutorService executorService = ByteBuffer buffer1 = ByteBuffer.wrap(data); writer.write(buffer1); verify(client, times(0)).writeRecordSet((String) any(), (LogRecordSetBuffer) any()); LogRecordSet.Writer recordSetWriter1 = writer.getLogRecordSetWriter(); assertEquals(1, recordSetWriter1.getNumRecords()); verify(client, times(1)).writeRecordSet((String) any(), (LogRecordSetBuffer) any()); LogRecordSet.Writer recordSetWriter2 = writer.getLogRecordSetWriter(); assertEquals(1, recordSetWriter2.getNumRecords());
@Test(timeout = 20000) public void testPeriodicalFlush() throws Exception { DistributedLogClient client = mock(DistributedLogClient.class); DistributedLogMultiStreamWriter writer = DistributedLogMultiStreamWriter.newBuilder() .streams(Lists.newArrayList("stream1", "stream2")) .client(client) .compressionCodec(CompressionCodec.Type.LZ4) .firstSpeculativeTimeoutMs(10) .maxSpeculativeTimeoutMs(20) .speculativeBackoffMultiplier(2) .requestTimeoutMs(5000000) .flushIntervalMs(10) .bufferSize(Integer.MAX_VALUE) .build(); final DLSN dlsn = new DLSN(99L, 88L, 0L); Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { return Future.value(dlsn); } }).when(client).writeRecordSet((String) any(), (LogRecordSetBuffer) any()); byte[] data = "test-test".getBytes(UTF_8); ByteBuffer buffer = ByteBuffer.wrap(data); Future<DLSN> writeFuture = writer.write(buffer); DLSN writeDLSN = Await.result(writeFuture); assertEquals(dlsn, writeDLSN); writer.close(); }
@Test(timeout = 20000) public void testFailRequestAfterRetriedAllStreams() throws Exception { DistributedLogClient client = mock(DistributedLogClient.class); when(client.writeRecordSet((String) any(), (LogRecordSetBuffer) any())) .thenReturn(new Promise<DLSN>()); DistributedLogMultiStreamWriter writer = DistributedLogMultiStreamWriter.newBuilder() .streams(Lists.newArrayList("stream1", "stream2")) .client(client) .compressionCodec(CompressionCodec.Type.LZ4) .firstSpeculativeTimeoutMs(10) .maxSpeculativeTimeoutMs(20) .speculativeBackoffMultiplier(2) .requestTimeoutMs(5000000) .flushIntervalMs(10) .bufferSize(Integer.MAX_VALUE) .build(); byte[] data = "test-test".getBytes(UTF_8); ByteBuffer buffer = ByteBuffer.wrap(data); Future<DLSN> writeFuture = writer.write(buffer); try { Await.result(writeFuture); fail("Should fail the request after retries all streams"); } catch (IndividualRequestTimeoutException e) { long timeoutMs = e.timeout().inMilliseconds(); assertTrue(timeoutMs >= (10 + 20) && timeoutMs < 5000000); } writer.close(); } }
}).when(client).writeRecordSet((String) any(), (LogRecordSetBuffer) any());
synchronized String sendWriteToStream(int streamId) { String stream = getStream(streamId); client.writeRecordSet(stream, recordSet) .addEventListener(this); return stream; }