Search in sources :

Example 91 with MockConnectionFactoryImpl

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

the class RevisionedStreamClientTest method testConditionalWrite.

@Test
public void testConditionalWrite() {
    String scope = "scope";
    String stream = "stream";
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, false);
    createScopeAndStream(scope, stream, controller);
    MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
    @Cleanup SynchronizerClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory, streamFactory);
    SynchronizerConfig config = SynchronizerConfig.builder().build();
    @Cleanup RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, new JavaSerializer<>(), config);
    client.writeUnconditionally("a");
    Revision revision = client.fetchLatestRevision();
    Revision newRevision = client.writeConditionally(revision, "b");
    assertNotNull(newRevision);
    assertTrue(newRevision.compareTo(revision) > 0);
    assertEquals(newRevision, client.fetchLatestRevision());
    Revision failed = client.writeConditionally(revision, "fail");
    assertNull(failed);
    assertEquals(newRevision, client.fetchLatestRevision());
    Iterator<Entry<Revision, String>> iter = client.readFrom(revision);
    assertTrue(iter.hasNext());
    Entry<Revision, String> entry = iter.next();
    assertEquals(newRevision, entry.getKey());
    assertEquals("b", entry.getValue());
    assertFalse(iter.hasNext());
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) SynchronizerClientFactory(io.pravega.client.SynchronizerClientFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) Entry(java.util.Map.Entry) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) Revision(io.pravega.client.state.Revision) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Test(org.junit.Test)

Example 92 with MockConnectionFactoryImpl

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

the class ConditionalOutputStreamTest method testSegmentSealed.

@Test(timeout = 10000)
public void testSegmentSealed() throws ConnectionFailedException {
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController("localhost", 0, connectionFactory, true);
    ConditionalOutputStreamFactory factory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory);
    Segment segment = new Segment("scope", "testWrite", 1);
    @Cleanup ConditionalOutputStream cOut = factory.createConditionalOutputStream(segment, DelegationTokenProviderFactory.create("token", controller, segment, AccessOperation.ANY), EventWriterConfig.builder().build());
    ByteBuffer data = ByteBuffer.allocate(10);
    String mockClientReplyStackTrace = "SomeException";
    ClientConnection mock = Mockito.mock(ClientConnection.class);
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    connectionFactory.provideConnection(location, mock);
    setupAppend(connectionFactory, segment, mock, location);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ConditionalAppend argument = (ConditionalAppend) invocation.getArgument(0);
            ReplyProcessor processor = connectionFactory.getProcessor(location);
            processor.process(new WireCommands.SegmentIsSealed(argument.getRequestId(), segment.getScopedName(), mockClientReplyStackTrace, argument.getEventNumber()));
            return null;
        }
    }).when(mock).send(any(ConditionalAppend.class));
    AssertExtensions.assertThrows(SegmentSealedException.class, () -> cOut.write(data, 0));
}
Also used : ConditionalAppend(io.pravega.shared.protocol.netty.WireCommands.ConditionalAppend) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 93 with MockConnectionFactoryImpl

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

the class ConditionalOutputStreamTest method testRetriesOnInvalidEventNumber.

@SneakyThrows
@Test(timeout = 10000)
public void testRetriesOnInvalidEventNumber() {
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController("localhost", 0, connectionFactory, true);
    ConditionalOutputStreamFactory factory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory);
    Segment segment = new Segment("scope", "testWrite", 1);
    @Cleanup ConditionalOutputStream objectUnderTest = factory.createConditionalOutputStream(segment, DelegationTokenProviderFactory.create("token", controller, segment, AccessOperation.ANY), EventWriterConfig.builder().build());
    ByteBuffer data = ByteBuffer.allocate(10);
    ClientConnection clientConnection = Mockito.mock(ClientConnection.class);
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    connectionFactory.provideConnection(location, clientConnection);
    setupAppend(connectionFactory, segment, clientConnection, location);
    final AtomicLong retryCounter = new AtomicLong(0);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ConditionalAppend argument = (ConditionalAppend) invocation.getArgument(0);
            ReplyProcessor processor = connectionFactory.getProcessor(location);
            if (retryCounter.getAndIncrement() < 2) {
                processor.process(new WireCommands.InvalidEventNumber(argument.getWriterId(), argument.getRequestId(), ""));
            } else {
                processor.process(new WireCommands.DataAppended(argument.getRequestId(), argument.getWriterId(), argument.getEventNumber(), 0, -1));
            }
            return null;
        }
    }).when(clientConnection).send(any(ConditionalAppend.class));
    assertTrue(objectUnderTest.write(data, 0));
    assertEquals(3, retryCounter.get());
}
Also used : ConditionalAppend(io.pravega.shared.protocol.netty.WireCommands.ConditionalAppend) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) AtomicLong(java.util.concurrent.atomic.AtomicLong) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test) SneakyThrows(lombok.SneakyThrows)

Example 94 with MockConnectionFactoryImpl

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

the class ConditionalOutputStreamTest method handleUnexpectedReplythrowsAppropriateTokenExceptions.

@Test
public void handleUnexpectedReplythrowsAppropriateTokenExceptions() {
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController("localhost", 0, connectionFactory, true);
    ConditionalOutputStreamFactory factory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory);
    Segment segment = new Segment("scope", "testWrite", 1);
    @Cleanup ConditionalOutputStreamImpl objectUnderTest = (ConditionalOutputStreamImpl) factory.createConditionalOutputStream(segment, DelegationTokenProviderFactory.create("token", controller, segment, AccessOperation.ANY), EventWriterConfig.builder().build());
    AssertExtensions.assertThrows("AuthenticationException wasn't thrown", () -> objectUnderTest.handleUnexpectedReply(new WireCommands.AuthTokenCheckFailed(1L, "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_CHECK_FAILED), "test"), e -> e instanceof AuthenticationException);
    AssertExtensions.assertThrows("AuthenticationException wasn't thrown", () -> objectUnderTest.handleUnexpectedReply(new WireCommands.AuthTokenCheckFailed(1L, "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.UNSPECIFIED), "test"), e -> e instanceof AuthenticationException);
    AssertExtensions.assertThrows("TokenExpiredException wasn't thrown", () -> objectUnderTest.handleUnexpectedReply(new WireCommands.AuthTokenCheckFailed(1L, "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_EXPIRED), "test"), e -> e instanceof TokenExpiredException);
    AssertExtensions.assertThrows("InvalidEventNumber wasn't treated as a connection failure", () -> objectUnderTest.handleUnexpectedReply(new WireCommands.InvalidEventNumber(UUID.randomUUID(), 1, "SomeException"), "test"), e -> e instanceof ConnectionFailedException);
    AssertExtensions.assertThrows("Hello wasn't treated as a connection failure", () -> objectUnderTest.handleUnexpectedReply(new WireCommands.Hello(1, 1), "test"), e -> e instanceof ConnectionFailedException);
}
Also used : AuthenticationException(io.pravega.auth.AuthenticationException) Cleanup(lombok.Cleanup) TokenExpiredException(io.pravega.auth.TokenExpiredException) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Test(org.junit.Test)

Example 95 with MockConnectionFactoryImpl

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

the class ConditionalOutputStreamTest method testClose.

@Test(timeout = 10000)
public void testClose() {
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController("localhost", 0, connectionFactory, true);
    ConditionalOutputStreamFactory factory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory);
    Segment segment = new Segment("scope", "testWrite", 1);
    @Cleanup ConditionalOutputStream cOut = factory.createConditionalOutputStream(segment, DelegationTokenProviderFactory.create("token", controller, segment, AccessOperation.ANY), EventWriterConfig.builder().build());
    cOut.close();
    AssertExtensions.assertThrows(IllegalStateException.class, () -> cOut.write(ByteBufferUtils.EMPTY, 0));
}
Also used : MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Aggregations

MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)121 MockController (io.pravega.client.stream.mock.MockController)118 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)114 Test (org.junit.Test)114 Cleanup (lombok.Cleanup)86 ClientConnection (io.pravega.client.connection.impl.ClientConnection)73 Segment (io.pravega.client.segment.impl.Segment)41 WireCommands (io.pravega.shared.protocol.netty.WireCommands)37 UUID (java.util.UUID)37 InvocationOnMock (org.mockito.invocation.InvocationOnMock)37 ReplyProcessor (io.pravega.shared.protocol.netty.ReplyProcessor)36 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)34 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)34 ByteBuffer (java.nio.ByteBuffer)32 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)31 AtomicLong (java.util.concurrent.atomic.AtomicLong)29 CompletableFuture (java.util.concurrent.CompletableFuture)27 InOrder (org.mockito.InOrder)27 SynchronizerClientFactory (io.pravega.client.SynchronizerClientFactory)26 SynchronizerConfig (io.pravega.client.state.SynchronizerConfig)22