Search in sources :

Example 51 with SimpleMessage

use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.

the class MessageStreamApiTests method runTestStreamAsync.

void runTestStreamAsync(CLUSTER cluster) throws Exception {
    RaftTestUtil.waitForLeader(cluster);
    ByteString bytes = ByteString.EMPTY;
    for (int i = 0; i < 10; ) {
        final String s = (char) ('A' + i) + "1234567";
        LOG.info("s=" + s);
        final ByteString b = ByteString.copyFrom(s, StandardCharsets.UTF_8);
        Assert.assertEquals(8, b.size());
        for (int j = 0; j < 128; j++) {
            bytes = bytes.concat(b);
        }
        i++;
        Assert.assertEquals(i * SUBMESSAGE_SIZE.getSizeInt(), bytes.size());
    }
    try (RaftClient client = cluster.createClient()) {
        final RaftClientReply reply = client.getMessageStreamApi().streamAsync(Message.valueOf(bytes)).get();
        Assert.assertTrue(reply.isSuccess());
    }
    // check if all the parts are streamed as a single message.
    try (RaftClient client = cluster.createClient()) {
        final RaftClientReply reply = client.io().sendReadOnly(new SimpleMessage(bytes.toString(StandardCharsets.UTF_8)));
        Assert.assertTrue(reply.isSuccess());
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) RaftClient(org.apache.ratis.client.RaftClient)

Example 52 with SimpleMessage

use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.

the class MessageStreamApiTests method runTestStream.

void runTestStream(CLUSTER cluster) throws Exception {
    RaftTestUtil.waitForLeader(cluster);
    // stream multiple parts
    final int numParts = 9;
    final int endOfRequest = 6;
    final StringBuilder key = new StringBuilder();
    try (RaftClient client = cluster.createClient();
        MessageOutputStream out = client.getMessageStreamApi().stream()) {
        for (int i = 1; i <= numParts; i++) {
            key.append(i);
            out.sendAsync(new SimpleMessage(i + ""), i == endOfRequest);
        }
    }
    // check if all the parts are streamed as a single message.
    final String k = key.toString();
    try (RaftClient client = cluster.createClient()) {
        final String k1 = k.substring(0, endOfRequest);
        final RaftClientReply r1 = client.io().sendReadOnly(new SimpleMessage(k1));
        Assert.assertTrue(r1.isSuccess());
        final String k2 = k.substring(endOfRequest);
        final RaftClientReply r2 = client.io().sendReadOnly(new SimpleMessage(k2));
        Assert.assertTrue(r2.isSuccess());
    }
}
Also used : MessageOutputStream(org.apache.ratis.client.api.MessageOutputStream) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)52 RaftClient (org.apache.ratis.client.RaftClient)46 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)27 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)20 Test (org.junit.Test)20 RaftServer (org.apache.ratis.server.RaftServer)19 IOException (java.io.IOException)15 RaftClientRpc (org.apache.ratis.client.RaftClientRpc)12 CompletableFuture (java.util.concurrent.CompletableFuture)11 RaftLog (org.apache.ratis.server.raftlog.RaftLog)10 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)9 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ArrayList (java.util.ArrayList)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 BaseTest (org.apache.ratis.BaseTest)7 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)6 File (java.io.File)5