Search in sources :

Example 36 with MockController

use of io.pravega.client.stream.mock.MockController in project pravega by pravega.

the class SegmentOutputStreamTest method testOverSizedWriteFails.

@Test
public void testOverSizedWriteFails() throws ConnectionFailedException, SegmentSealedException {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    @Cleanup("shutdown") InlineExecutor inlineExecutor = new InlineExecutor();
    cf.setExecutor(inlineExecutor);
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf);
    ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(uri, connection);
    @Cleanup SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, "");
    output.reconnect();
    verify(connection).send(new SetupAppend(1, cid, SEGMENT, ""));
    cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
    ByteBuffer data = ByteBuffer.allocate(PendingEvent.MAX_WRITE_SIZE + 1);
    CompletableFuture<Boolean> acked = new CompletableFuture<>();
    try {
        output.write(new PendingEvent("routingKey", data, acked));
        fail("Did not throw");
    } catch (IllegalArgumentException e) {
    // expected
    }
    assertEquals(false, acked.isDone());
    verifyNoMoreInteractions(connection);
}
Also used : Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) CompletableFuture(java.util.concurrent.CompletableFuture) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) PendingEvent(io.pravega.client.stream.impl.PendingEvent) InlineExecutor(io.pravega.test.common.InlineExecutor) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) UUID(java.util.UUID) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 37 with MockController

use of io.pravega.client.stream.mock.MockController in project pravega by pravega.

the class SegmentOutputStreamTest method testConnectionFailure.

@Test
public void testConnectionFailure() throws ConnectionFailedException {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    @Cleanup("shutdown") InlineExecutor inlineExecutor = new InlineExecutor();
    cf.setExecutor(inlineExecutor);
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf);
    ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(uri, connection);
    SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, "");
    output.reconnect();
    InOrder inOrder = Mockito.inOrder(connection);
    inOrder.verify(connection).send(new SetupAppend(1, cid, SEGMENT, ""));
    cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
    ByteBuffer data = getBuffer("test");
    CompletableFuture<Boolean> acked = new CompletableFuture<>();
    Append append = new Append(SEGMENT, cid, 1, Unpooled.wrappedBuffer(data), null);
    CompletableFuture<Boolean> acked2 = new CompletableFuture<>();
    Append append2 = new Append(SEGMENT, cid, 2, Unpooled.wrappedBuffer(data), null);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            cf.getProcessor(uri).connectionDropped();
            throw new ConnectionFailedException();
        }
    }).when(connection).send(append);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            CompletedCallback callback = (CompletedCallback) invocation.getArgument(1);
            callback.complete(null);
            return null;
        }
    }).when(connection).sendAsync(Mockito.eq(Collections.singletonList(append)), Mockito.any());
    Async.testBlocking(() -> {
        output.write(new PendingEvent(null, data, acked, null));
        output.write(new PendingEvent(null, data, acked2, null));
    }, () -> {
        cf.getProcessor(uri).appendSetup(new AppendSetup(2, SEGMENT, cid, 0));
    });
    inOrder.verify(connection).send(append);
    inOrder.verify(connection).send(new SetupAppend(2, cid, SEGMENT, ""));
    inOrder.verify(connection).sendAsync(Mockito.eq(Collections.singletonList(append)), Mockito.any());
    inOrder.verify(connection).send(append2);
    assertEquals(false, acked.isDone());
    assertEquals(false, acked2.isDone());
    inOrder.verifyNoMoreInteractions();
}
Also used : CompletedCallback(io.pravega.client.netty.impl.ClientConnection.CompletedCallback) InOrder(org.mockito.InOrder) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) CompletableFuture(java.util.concurrent.CompletableFuture) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) PendingEvent(io.pravega.client.stream.impl.PendingEvent) InlineExecutor(io.pravega.test.common.InlineExecutor) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) UUID(java.util.UUID) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Test(org.junit.Test)

Example 38 with MockController

use of io.pravega.client.stream.mock.MockController in project pravega by pravega.

the class SegmentOutputStreamTest method testFailureWhileRetransmittingInflight.

@Test(timeout = 10000)
public void testFailureWhileRetransmittingInflight() throws ConnectionFailedException {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
    // Ensure task submitted to executor is run inline.
    implementAsDirectExecutor(executor);
    cf.setExecutor(executor);
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf);
    ClientConnection connection = mock(ClientConnection.class);
    InOrder inOrder = inOrder(connection);
    cf.provideConnection(uri, connection);
    @SuppressWarnings("resource") SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, "");
    output.reconnect();
    cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
    output.write(new PendingEvent(null, getBuffer("test1"), new CompletableFuture<>()));
    output.write(new PendingEvent(null, getBuffer("test2"), new CompletableFuture<>()));
    doAnswer(new Answer<Void>() {

        boolean failed = false;

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            CompletedCallback callback = (CompletedCallback) invocation.getArgument(1);
            if (failed) {
                callback.complete(null);
            } else {
                failed = true;
                callback.complete(new ConnectionFailedException("Injected"));
            }
            return null;
        }
    }).when(connection).sendAsync(Mockito.any(), Mockito.any());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            cf.getProcessor(uri).appendSetup(new AppendSetup(2, SEGMENT, cid, 0));
            return null;
        }
    }).when(connection).send(new SetupAppend(2, cid, SEGMENT, ""));
    cf.getProcessor(uri).connectionDropped();
    Async.testBlocking(() -> output.write(new PendingEvent(null, getBuffer("test3"), new CompletableFuture<>())), () -> {
        cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
    });
    output.write(new PendingEvent(null, getBuffer("test4"), new CompletableFuture<>()));
    Append append1 = new Append(SEGMENT, cid, 1, Unpooled.wrappedBuffer(getBuffer("test1")), null);
    Append append2 = new Append(SEGMENT, cid, 2, Unpooled.wrappedBuffer(getBuffer("test2")), null);
    Append append3 = new Append(SEGMENT, cid, 3, Unpooled.wrappedBuffer(getBuffer("test3")), null);
    Append append4 = new Append(SEGMENT, cid, 4, Unpooled.wrappedBuffer(getBuffer("test4")), null);
    inOrder.verify(connection).send(new SetupAppend(1, cid, SEGMENT, ""));
    inOrder.verify(connection).send(append1);
    inOrder.verify(connection).send(append2);
    inOrder.verify(connection).close();
    inOrder.verify(connection).send(new SetupAppend(2, cid, SEGMENT, ""));
    inOrder.verify(connection).sendAsync(eq(ImmutableList.of(append1, append2)), any());
    inOrder.verify(connection).close();
    inOrder.verify(connection).send(new SetupAppend(3, cid, SEGMENT, ""));
    inOrder.verify(connection).sendAsync(eq(ImmutableList.of(append1, append2)), any());
    inOrder.verify(connection).send(append3);
    inOrder.verify(connection).send(append4);
    verifyNoMoreInteractions(connection);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CompletedCallback(io.pravega.client.netty.impl.ClientConnection.CompletedCallback) InOrder(org.mockito.InOrder) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) CompletableFuture(java.util.concurrent.CompletableFuture) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) PendingEvent(io.pravega.client.stream.impl.PendingEvent) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) UUID(java.util.UUID) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Test(org.junit.Test)

Example 39 with MockController

use of io.pravega.client.stream.mock.MockController in project pravega by pravega.

the class SegmentOutputStreamTest method testNewEventsGoAfterInflight.

@Test(timeout = 20000)
public void testNewEventsGoAfterInflight() throws ConnectionFailedException {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    @Cleanup("shutdown") InlineExecutor inlineExecutor = new InlineExecutor();
    cf.setExecutor(inlineExecutor);
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf);
    ClientConnection connection = mock(ClientConnection.class);
    InOrder inOrder = inOrder(connection);
    cf.provideConnection(uri, connection);
    @SuppressWarnings("resource") SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, "");
    output.reconnect();
    cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
    output.write(new PendingEvent(null, getBuffer("test1"), new CompletableFuture<>()));
    output.write(new PendingEvent(null, getBuffer("test2"), new CompletableFuture<>()));
    answerSuccess(connection);
    cf.getProcessor(uri).connectionDropped();
    Async.testBlocking(() -> output.write(new PendingEvent(null, getBuffer("test3"), new CompletableFuture<>())), () -> cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0)));
    output.write(new PendingEvent(null, getBuffer("test4"), new CompletableFuture<>()));
    Append append1 = new Append(SEGMENT, cid, 1, Unpooled.wrappedBuffer(getBuffer("test1")), null);
    Append append2 = new Append(SEGMENT, cid, 2, Unpooled.wrappedBuffer(getBuffer("test2")), null);
    Append append3 = new Append(SEGMENT, cid, 3, Unpooled.wrappedBuffer(getBuffer("test3")), null);
    Append append4 = new Append(SEGMENT, cid, 4, Unpooled.wrappedBuffer(getBuffer("test4")), null);
    inOrder.verify(connection).send(new SetupAppend(1, cid, SEGMENT, ""));
    inOrder.verify(connection).send(append1);
    inOrder.verify(connection).send(append2);
    inOrder.verify(connection).close();
    inOrder.verify(connection).send(new SetupAppend(2, cid, SEGMENT, ""));
    inOrder.verify(connection).sendAsync(eq(ImmutableList.of(append1, append2)), any());
    inOrder.verify(connection).send(append3);
    inOrder.verify(connection).send(append4);
    verifyNoMoreInteractions(connection);
}
Also used : InOrder(org.mockito.InOrder) Cleanup(lombok.Cleanup) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) CompletableFuture(java.util.concurrent.CompletableFuture) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) PendingEvent(io.pravega.client.stream.impl.PendingEvent) InlineExecutor(io.pravega.test.common.InlineExecutor) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) UUID(java.util.UUID) Test(org.junit.Test)

Example 40 with MockController

use of io.pravega.client.stream.mock.MockController in project pravega by pravega.

the class SegmentOutputStreamTest method testExceptionSealedCallback.

@Test(timeout = 10000)
public void testExceptionSealedCallback() throws ConnectionFailedException {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
    // Ensure task submitted to executor is run inline.
    implementAsDirectExecutor(executor);
    cf.setExecutor(executor);
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf);
    ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(uri, connection);
    AtomicBoolean shouldThrow = new AtomicBoolean(true);
    // call back which throws an exception.
    Consumer<Segment> exceptionCallback = s -> {
        if (shouldThrow.getAndSet(false)) {
            throw new IllegalStateException();
        }
    };
    SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, exceptionCallback, RETRY_SCHEDULE, "");
    output.reconnect();
    verify(connection).send(new SetupAppend(1, cid, SEGMENT, ""));
    cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
    ByteBuffer data = getBuffer("test");
    CompletableFuture<Boolean> ack = new CompletableFuture<>();
    output.write(new PendingEvent(null, data, ack));
    assertEquals(false, ack.isDone());
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            cf.getProcessor(uri).appendSetup(new AppendSetup(3, SEGMENT, cid, 0));
            return null;
        }
    }).when(connection).send(new SetupAppend(3, cid, SEGMENT, ""));
    Async.testBlocking(() -> {
        AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.flush());
    }, () -> {
        cf.getProcessor(uri).segmentIsSealed(new WireCommands.SegmentIsSealed(1, SEGMENT));
        output.getUnackedEventsOnSeal();
    });
    verify(connection).send(new WireCommands.KeepAlive());
    verify(connection).send(new Append(SEGMENT, cid, 1, Unpooled.wrappedBuffer(data), null));
    assertEquals(false, ack.isDone());
}
Also used : Retry(io.pravega.common.util.Retry) AssertExtensions(io.pravega.test.common.AssertExtensions) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Cleanup(lombok.Cleanup) Async(io.pravega.test.common.Async) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) Mockito.doThrow(org.mockito.Mockito.doThrow) MockController(io.pravega.client.stream.mock.MockController) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Mockito.doAnswer(org.mockito.Mockito.doAnswer) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) Assert.fail(org.junit.Assert.fail) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) UUID(java.util.UUID) Assert.assertFalse(org.junit.Assert.assertFalse) Mockito.inOrder(org.mockito.Mockito.inOrder) Futures(io.pravega.common.concurrent.Futures) Mockito.mock(org.mockito.Mockito.mock) ClientConnection(io.pravega.client.netty.impl.ClientConnection) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) PendingEvent(io.pravega.client.stream.impl.PendingEvent) Append(io.pravega.shared.protocol.netty.Append) Answer(org.mockito.stubbing.Answer) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ImmutableList(com.google.common.collect.ImmutableList) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CompletedCallback(io.pravega.client.netty.impl.ClientConnection.CompletedCallback) RetryWithBackoff(io.pravega.common.util.Retry.RetryWithBackoff) InOrder(org.mockito.InOrder) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) InlineExecutor(io.pravega.test.common.InlineExecutor) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) CompletableFuture(java.util.concurrent.CompletableFuture) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) ClientConnection(io.pravega.client.netty.impl.ClientConnection) UUID(java.util.UUID) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ByteBuffer(java.nio.ByteBuffer) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) PendingEvent(io.pravega.client.stream.impl.PendingEvent) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockController(io.pravega.client.stream.mock.MockController) Test(org.junit.Test)

Aggregations

MockController (io.pravega.client.stream.mock.MockController)50 Test (org.junit.Test)49 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)48 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)47 Cleanup (lombok.Cleanup)38 ClientConnection (io.pravega.client.netty.impl.ClientConnection)30 UUID (java.util.UUID)22 WireCommands (io.pravega.shared.protocol.netty.WireCommands)20 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)19 CompletableFuture (java.util.concurrent.CompletableFuture)17 PendingEvent (io.pravega.client.stream.impl.PendingEvent)16 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)16 ClientFactory (io.pravega.client.ClientFactory)15 Segment (io.pravega.client.segment.impl.Segment)15 SynchronizerConfig (io.pravega.client.state.SynchronizerConfig)15 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)15 InOrder (org.mockito.InOrder)14 InvocationOnMock (org.mockito.invocation.InvocationOnMock)13 Append (io.pravega.shared.protocol.netty.Append)12 ByteBuffer (java.nio.ByteBuffer)12