Search in sources :

Example 6 with SegmentsMerged

use of io.pravega.shared.protocol.netty.WireCommands.SegmentsMerged in project pravega by pravega.

the class LargeEventWriterTest method testUnexpectedErrors.

@Test(timeout = 5000)
public void testUnexpectedErrors() throws ConnectionFailedException, NoSuchSegmentException, AuthenticationException, SegmentSealedException {
    Segment segment = Segment.fromScopedName("foo/bar/1");
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    MockController controller = new MockController("localhost", 0, connectionFactory, false);
    ClientConnection connection = Mockito.mock(ClientConnection.class);
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    connectionFactory.provideConnection(location, connection);
    EmptyTokenProviderImpl tokenProvider = new EmptyTokenProviderImpl();
    EventWriterConfig config = EventWriterConfig.builder().initialBackoffMillis(0).build();
    ArrayList<ByteBuffer> events = new ArrayList<>();
    events.add(ByteBuffer.allocate(1));
    AtomicBoolean failed = new AtomicBoolean(false);
    AtomicBoolean succeeded = new AtomicBoolean(false);
    answerRequest(connectionFactory, connection, location, CreateTransientSegment.class, r -> new SegmentCreated(r.getRequestId(), "transient-segment"));
    answerRequest(connectionFactory, connection, location, SetupAppend.class, r -> new AppendSetup(r.getRequestId(), segment.getScopedName(), r.getWriterId(), WireCommands.NULL_ATTRIBUTE_VALUE));
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ConditionalBlockEnd argument = (ConditionalBlockEnd) invocation.getArgument(0);
            failed.set(true);
            connectionFactory.getProcessor(location).process(new InvalidEventNumber(argument.getWriterId(), argument.getEventNumber(), "stacktrace"));
            return null;
        }
    }).doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ConditionalBlockEnd argument = (ConditionalBlockEnd) invocation.getArgument(0);
            ByteBuf data = argument.getData();
            succeeded.set(true);
            connectionFactory.getProcessor(location).process(new DataAppended(argument.getRequestId(), argument.getWriterId(), argument.getEventNumber(), argument.getEventNumber() - 1, argument.getExpectedOffset() + data.readableBytes()));
            return null;
        }
    }).when(connection).send(any(ConditionalBlockEnd.class));
    answerRequest(connectionFactory, connection, location, MergeSegments.class, r -> {
        return new SegmentsMerged(r.getRequestId(), r.getSource(), r.getTarget(), -1);
    });
    LargeEventWriter writer = new LargeEventWriter(writerId, controller, connectionFactory);
    writer.writeLargeEvent(segment, events, tokenProvider, EventWriterConfig.builder().build());
    assertTrue(failed.getAndSet(false));
    assertTrue(succeeded.getAndSet(false));
    answerRequest(connectionFactory, connection, location, CreateTransientSegment.class, r -> new SegmentCreated(r.getRequestId(), "transient-segment"));
    answerRequest(connectionFactory, connection, location, SetupAppend.class, r -> new AppendSetup(r.getRequestId(), segment.getScopedName(), r.getWriterId(), WireCommands.NULL_ATTRIBUTE_VALUE));
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ConditionalBlockEnd argument = (ConditionalBlockEnd) invocation.getArgument(0);
            failed.set(true);
            connectionFactory.getProcessor(location).process(new ConditionalCheckFailed(argument.getWriterId(), argument.getEventNumber(), argument.getRequestId()));
            return null;
        }
    }).doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ConditionalBlockEnd argument = (ConditionalBlockEnd) invocation.getArgument(0);
            ByteBuf data = argument.getData();
            succeeded.set(true);
            connectionFactory.getProcessor(location).process(new DataAppended(argument.getRequestId(), argument.getWriterId(), argument.getEventNumber(), argument.getEventNumber() - 1, argument.getExpectedOffset() + data.readableBytes()));
            return null;
        }
    }).when(connection).send(any(ConditionalBlockEnd.class));
    answerRequest(connectionFactory, connection, location, MergeSegments.class, r -> {
        return new SegmentsMerged(r.getRequestId(), r.getSource(), r.getTarget(), -1);
    });
    writer = new LargeEventWriter(writerId, controller, connectionFactory);
    writer.writeLargeEvent(segment, events, tokenProvider, EventWriterConfig.builder().build());
    assertTrue(failed.getAndSet(false));
    assertTrue(succeeded.getAndSet(false));
}
Also used : ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Segment(io.pravega.client.segment.impl.Segment) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) CreateTransientSegment(io.pravega.shared.protocol.netty.WireCommands.CreateTransientSegment) ConditionalCheckFailed(io.pravega.shared.protocol.netty.WireCommands.ConditionalCheckFailed) ConditionalBlockEnd(io.pravega.shared.protocol.netty.WireCommands.ConditionalBlockEnd) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvalidEventNumber(io.pravega.shared.protocol.netty.WireCommands.InvalidEventNumber) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) ClientConnection(io.pravega.client.connection.impl.ClientConnection) ByteBuffer(java.nio.ByteBuffer) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) EmptyTokenProviderImpl(io.pravega.client.security.auth.EmptyTokenProviderImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SegmentCreated(io.pravega.shared.protocol.netty.WireCommands.SegmentCreated) Answer(org.mockito.stubbing.Answer) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) MockController(io.pravega.client.stream.mock.MockController) SegmentsMerged(io.pravega.shared.protocol.netty.WireCommands.SegmentsMerged) Test(org.junit.Test)

Aggregations

ClientConnection (io.pravega.client.connection.impl.ClientConnection)6 Segment (io.pravega.client.segment.impl.Segment)6 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)6 MockController (io.pravega.client.stream.mock.MockController)6 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)6 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)6 CreateTransientSegment (io.pravega.shared.protocol.netty.WireCommands.CreateTransientSegment)6 DataAppended (io.pravega.shared.protocol.netty.WireCommands.DataAppended)6 NoSuchSegment (io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment)6 SegmentCreated (io.pravega.shared.protocol.netty.WireCommands.SegmentCreated)6 SegmentsMerged (io.pravega.shared.protocol.netty.WireCommands.SegmentsMerged)6 Test (org.junit.Test)6 ByteBuf (io.netty.buffer.ByteBuf)5 ArrayList (java.util.ArrayList)5 EmptyTokenProviderImpl (io.pravega.client.security.auth.EmptyTokenProviderImpl)4 ConditionalBlockEnd (io.pravega.shared.protocol.netty.WireCommands.ConditionalBlockEnd)4 ByteBuffer (java.nio.ByteBuffer)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3