Search in sources :

Example 1 with ByteBufferCollector

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));
}
Also used : ByteBufferCollector(com.alipay.sofa.jraft.util.ByteBufferCollector) FileNotFoundException(java.io.FileNotFoundException) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Example 2 with ByteBufferCollector

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());
}
Also used : Status(com.alipay.sofa.jraft.Status) ByteBufferCollector(com.alipay.sofa.jraft.util.ByteBufferCollector) Message(com.google.protobuf.Message) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) Test(org.junit.Test)

Example 3 with ByteBufferCollector

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();
}
Also used : ByteBufferCollector(com.alipay.sofa.jraft.util.ByteBufferCollector) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with ByteBufferCollector

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());
}
Also used : ByteBufferCollector(com.alipay.sofa.jraft.util.ByteBufferCollector) Message(com.google.protobuf.Message) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) Test(org.junit.Test)

Example 5 with ByteBufferCollector

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);
}
Also used : ByteBufferCollector(com.alipay.sofa.jraft.util.ByteBufferCollector) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Aggregations

ByteBufferCollector (com.alipay.sofa.jraft.util.ByteBufferCollector)14 ByteBuffer (java.nio.ByteBuffer)8 Test (org.junit.Test)8 BaseStorageTest (com.alipay.sofa.jraft.storage.BaseStorageTest)5 AppendEntriesRequest (com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesRequest)4 Message (com.google.protobuf.Message)3 Status (com.alipay.sofa.jraft.Status)2 FutureImpl (com.alipay.sofa.jraft.rpc.impl.FutureImpl)2 FileNotFoundException (java.io.FileNotFoundException)2 LocalFileMetaOutter (com.alipay.sofa.jraft.entity.LocalFileMetaOutter)1 RetryAgainException (com.alipay.sofa.jraft.error.RetryAgainException)1 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)1 GetFileResponse (com.alipay.sofa.jraft.rpc.RpcRequests.GetFileResponse)1 RpcResponseClosureAdapter (com.alipay.sofa.jraft.rpc.RpcResponseClosureAdapter)1 FileReader (com.alipay.sofa.jraft.storage.io.FileReader)1 Session (com.alipay.sofa.jraft.storage.snapshot.remote.Session)1 Recyclable (com.alipay.sofa.jraft.util.Recyclable)1 RecyclableByteBufferList (com.alipay.sofa.jraft.util.RecyclableByteBufferList)1 File (java.io.File)1 IOException (java.io.IOException)1