use of com.alipay.sofa.jraft.util.ByteBufferCollector in project sofa-jraft by sofastack.
the class SnapshotFileReaderTest method testReadFile.
@Test
public void testReadFile() throws Exception {
final ByteBufferCollector bufRef = ByteBufferCollector.allocate();
try {
this.reader.readFile(bufRef, "unfound", 0, 1024);
fail();
} catch (final FileNotFoundException e) {
}
final String data = writeData();
addDataMeta();
final int read = this.reader.readFile(bufRef, "data", 0, 1024);
assertEquals(-1, read);
final ByteBuffer buf = bufRef.getBuffer();
buf.flip();
assertEquals(data.length(), buf.remaining());
final byte[] bs = new byte[data.length()];
buf.get(bs);
assertEquals(data, new String(bs));
}
use of com.alipay.sofa.jraft.util.ByteBufferCollector in project sofa-jraft by sofastack.
the class CopySessionTest method testOnRpcReturnedRetry.
@Test
public void testOnRpcReturnedRetry() throws Exception {
assertNull(this.session.getTimer());
assertNull(this.session.getRpcCall());
final ByteBufferCollector bufRef = ByteBufferCollector.allocate(0);
this.session.setDestBuf(bufRef);
final FutureImpl<Message> future = new FutureImpl<>();
final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename("data").setCount(Integer.MAX_VALUE).setOffset(0).setReadPartly(true);
Mockito.when(this.rpcService.getFile(this.address, rb.build(), this.copyOpts.getTimeoutMs(), session.getDone())).thenReturn(future);
this.session.onRpcReturned(new Status(RaftError.EINTR, "test"), null);
assertNotNull(this.session.getTimer());
Thread.sleep(this.copyOpts.getRetryIntervalMs() + 100);
assertNotNull(this.session.getRpcCall());
assertSame(future, this.session.getRpcCall());
assertNull(this.session.getTimer());
}
use of com.alipay.sofa.jraft.util.ByteBufferCollector in project sofa-jraft by sofastack.
the class CopySessionTest method testOnRpcReturnedEOF.
@Test
public void testOnRpcReturnedEOF() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
new Thread() {
@Override
public void run() {
try {
// test join, should return
session.join();
latch.countDown();
} catch (final InterruptedException e) {
}
}
}.start();
assertNull(this.session.getRpcCall());
final ByteBufferCollector bufRef = ByteBufferCollector.allocate(0);
this.session.setDestBuf(bufRef);
this.session.onRpcReturned(Status.OK(), RpcRequests.GetFileResponse.newBuilder().setReadSize(100).setEof(true).setData(ByteString.copyFrom(new byte[100])).build());
assertEquals(100, bufRef.capacity());
// should be flip
assertEquals(0, bufRef.getBuffer().position());
assertEquals(100, bufRef.getBuffer().remaining());
assertNull(this.session.getRpcCall());
latch.await();
}
use of com.alipay.sofa.jraft.util.ByteBufferCollector in project sofa-jraft by sofastack.
the class CopySessionTest method testOnRpcReturnedOK.
@Test
public void testOnRpcReturnedOK() {
assertNull(this.session.getRpcCall());
final ByteBufferCollector bufRef = ByteBufferCollector.allocate(0);
this.session.setDestBuf(bufRef);
final FutureImpl<Message> future = new FutureImpl<>();
final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename("data").setCount(Integer.MAX_VALUE).setOffset(100).setReadPartly(true);
Mockito.when(this.rpcService.getFile(this.address, rb.build(), this.copyOpts.getTimeoutMs(), session.getDone())).thenReturn(future);
this.session.onRpcReturned(Status.OK(), RpcRequests.GetFileResponse.newBuilder().setReadSize(100).setEof(false).setData(ByteString.copyFrom(new byte[100])).build());
assertEquals(100, bufRef.capacity());
assertEquals(100, bufRef.getBuffer().position());
assertNotNull(this.session.getRpcCall());
// send next request
assertSame(future, this.session.getRpcCall());
}
use of com.alipay.sofa.jraft.util.ByteBufferCollector in project sofa-jraft by sofastack.
the class LocalFileReaderTest method testReadFile.
@Test
public void testReadFile() throws Exception {
final ByteBufferCollector bufRef = ByteBufferCollector.allocate();
try {
this.fileReader.readFile(bufRef, "unfound", 0, 1024);
fail();
} catch (final FileNotFoundException e) {
}
final String data = writeData();
assertReadResult(bufRef, data);
}
Aggregations