Search in sources :

Example 1 with ListenableAsyncCloseable

use of io.servicetalk.concurrent.api.ListenableAsyncCloseable in project servicetalk by apple.

the class ConnectionFactoryFilterTest method testAppend.

@Test
void testAppend() throws Exception {
    Deque<Integer> createOrder = new ArrayDeque<>();
    Deque<Integer> connectOrder = new ArrayDeque<>();
    class FactoryOrder implements ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable> {

        final int order;

        ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable> original;

        FactoryOrder(int order, ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable> original) {
            this.order = order;
            this.original = original;
        }

        @Override
        public Single<ListenableAsyncCloseable> newConnection(final InetSocketAddress unused, @Nullable final TransportObserver observer) {
            connectOrder.add(order);
            return original.newConnection(unused, observer);
        }

        @Override
        public Completable closeAsync() {
            return Completable.completed();
        }

        @Override
        public Completable onClose() {
            return Completable.completed();
        }
    }
    class FilterOrder implements ConnectionFactoryFilter<InetSocketAddress, ListenableAsyncCloseable> {

        final int order;

        FilterOrder(int order) {
            this.order = order;
        }

        @Override
        public ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable> create(final ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable> original) {
            createOrder.add(order);
            return new FactoryOrder(order, original);
        }
    }
    FilterOrder first = new FilterOrder(1);
    FilterOrder second = new FilterOrder(2);
    ConnectionFactoryFilter<InetSocketAddress, ListenableAsyncCloseable> combined = first.append(second);
    ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable> root = new FactoryOrder(999, new ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable>() {

        @Override
        public Single<ListenableAsyncCloseable> newConnection(final InetSocketAddress unused, @Nullable final TransportObserver observer) {
            return Single.succeeded(DUMMY_CLOSABLE);
        }

        @Override
        public Completable onClose() {
            return Completable.completed();
        }

        @Override
        public Completable closeAsync() {
            return Completable.completed();
        }
    });
    ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable> factory = combined.create(root);
    ListenableAsyncCloseable connection = factory.newConnection(mock(InetSocketAddress.class), null).toFuture().get();
    assertThat(connection, is(sameInstance(DUMMY_CLOSABLE)));
    assertThat(createOrder, is(hasSize(2)));
    assertThat(createOrder, is(containsInRelativeOrder(2, 1)));
    assertThat(connectOrder, is(hasSize(3)));
    assertThat(connectOrder, is(containsInRelativeOrder(1, 2, 999)));
}
Also used : Completable(io.servicetalk.concurrent.api.Completable) InetSocketAddress(java.net.InetSocketAddress) TransportObserver(io.servicetalk.transport.api.TransportObserver) ArrayDeque(java.util.ArrayDeque) Single(io.servicetalk.concurrent.api.Single) ListenableAsyncCloseable(io.servicetalk.concurrent.api.ListenableAsyncCloseable) Nullable(javax.annotation.Nullable) Test(org.junit.jupiter.api.Test)

Example 2 with ListenableAsyncCloseable

use of io.servicetalk.concurrent.api.ListenableAsyncCloseable in project servicetalk by apple.

the class LimitingConnectionFactoryFilterTest method cancelReleasesPermit.

@Test
void cancelReleasesPermit() throws Exception {
    ConnectionFactory<String, ListenableAsyncCloseable> o = newMockConnectionFactory();
    when(o.newConnection(any(), any())).thenReturn(never());
    ConnectionFactory<String, ? extends ListenableAsyncCloseable> cf = makeCF(LimitingConnectionFactoryFilter.withMax(1), o);
    toSource(cf.newConnection("c1", null)).subscribe(connectlistener);
    assertThat(connectlistener.pollTerminal(10, MILLISECONDS), is(nullValue()));
    connectAndVerifyFailed(cf);
    connectlistener.awaitSubscription().cancel();
    ListenableAsyncCloseable c = mock(ListenableAsyncCloseable.class);
    when(c.onClose()).thenReturn(Completable.never());
    when(o.newConnection(any(), any())).thenReturn(succeeded(c));
    cf.newConnection("c2", null).toFuture().get();
}
Also used : ListenableAsyncCloseable(io.servicetalk.concurrent.api.ListenableAsyncCloseable) Test(org.junit.jupiter.api.Test)

Example 3 with ListenableAsyncCloseable

use of io.servicetalk.concurrent.api.ListenableAsyncCloseable in project servicetalk by apple.

the class LimitingConnectionFactoryFilterTest method setUp.

@BeforeEach
public void setUp() {
    original = newMockConnectionFactory();
    connectionOnClose = new LinkedBlockingQueue<>();
    when(original.newConnection(any(), any())).thenAnswer(invocation -> {
        ListenableAsyncCloseable conn = mock(ListenableAsyncCloseable.class);
        Processor onClose = newCompletableProcessor();
        connectionOnClose.add(onClose);
        when(conn.onClose()).thenReturn(fromSource(onClose));
        return succeeded(conn);
    });
}
Also used : Processors.newCompletableProcessor(io.servicetalk.concurrent.api.Processors.newCompletableProcessor) Processor(io.servicetalk.concurrent.CompletableSource.Processor) ListenableAsyncCloseable(io.servicetalk.concurrent.api.ListenableAsyncCloseable) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with ListenableAsyncCloseable

use of io.servicetalk.concurrent.api.ListenableAsyncCloseable in project servicetalk by apple.

the class PowerSetPartitionMapTest method testWildCardResolveTwoElements.

@Test
void testWildCardResolveTwoElements() {
    PowerSetPartitionMap<ListenableAsyncCloseable> map = oneTwoThreeMap();
    PartitionAttributesBuilder builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(DC_ID, 1);
    builder.add(SHARD_ID, 10);
    assertEquals(VALUE, map.get(builder.build()));
    builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(DC_ID, 1);
    builder.add(SHARD_ID, 9);
    assertEquals(VALUE, map.get(builder.build()));
    builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(DC_ID, 1);
    builder.add(APP_ID, "myapp");
    assertEquals(VALUE, map.get(builder.build()));
    builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(APP_ID, "myapp");
    builder.add(IS_MAIN, true);
    assertEquals(VALUE, map.get(builder.build()));
    builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(APP_ID, "myapp");
    builder.add(IS_MAIN, false);
    assertEquals(VALUE, map.get(builder.build()));
    builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(DC_ID, 1);
    builder.add(IS_MAIN, false);
    assertEquals(VALUE, map.get(builder.build()));
    builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(DC_ID, 1);
    builder.add(IS_MAIN, true);
    assertEquals(VALUE, map.get(builder.build()));
    builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(DC_ID, 2);
    builder.add(IS_MAIN, true);
    assertNull(map.get(builder.build()));
}
Also used : ListenableAsyncCloseable(io.servicetalk.concurrent.api.ListenableAsyncCloseable) PartitionAttributesBuilder(io.servicetalk.client.api.partition.PartitionAttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 5 with ListenableAsyncCloseable

use of io.servicetalk.concurrent.api.ListenableAsyncCloseable in project servicetalk by apple.

the class PowerSetPartitionMapTest method testAddDuplicationPartitions.

@Test
void testAddDuplicationPartitions() {
    PowerSetPartitionMap<ListenableAsyncCloseable> map = new PowerSetPartitionMap<>(address -> VALUE);
    assertTrue(map.isEmpty(), "New map is not empty.");
    PartitionAttributes partition = new DefaultPartitionAttributesBuilder(1).add(IS_MAIN, true).add(SHARD_ID, 1).build();
    List<ListenableAsyncCloseable> added1 = map.add(partition);
    List<ListenableAsyncCloseable> added2 = map.add(partition);
    assertEquals(added1, added2, "Added partitions are not equal.");
    assertEquals(1, map.size(), "Same partition added twice.");
    List<ListenableAsyncCloseable> removed = map.remove(partition);
    assertEquals(removed.size(), added1.size(), "Unexpected size of removed partitions.");
}
Also used : PartitionAttributes(io.servicetalk.client.api.partition.PartitionAttributes) ListenableAsyncCloseable(io.servicetalk.concurrent.api.ListenableAsyncCloseable) Test(org.junit.jupiter.api.Test)

Aggregations

ListenableAsyncCloseable (io.servicetalk.concurrent.api.ListenableAsyncCloseable)13 Test (org.junit.jupiter.api.Test)9 PartitionAttributesBuilder (io.servicetalk.client.api.partition.PartitionAttributesBuilder)5 PartitionAttributes (io.servicetalk.client.api.partition.PartitionAttributes)3 Completable (io.servicetalk.concurrent.api.Completable)2 Single (io.servicetalk.concurrent.api.Single)2 TransportObserver (io.servicetalk.transport.api.TransportObserver)2 Processor (io.servicetalk.concurrent.CompletableSource.Processor)1 Processors.newCompletableProcessor (io.servicetalk.concurrent.api.Processors.newCompletableProcessor)1 ConnectAndHttpExecutionStrategy (io.servicetalk.http.api.ConnectAndHttpExecutionStrategy)1 FilterableStreamingHttpConnection (io.servicetalk.http.api.FilterableStreamingHttpConnection)1 HttpClient (io.servicetalk.http.api.HttpClient)1 HttpResponse (io.servicetalk.http.api.HttpResponse)1 ServerContext (io.servicetalk.transport.api.ServerContext)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 ArrayDeque (java.util.ArrayDeque)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Nullable (javax.annotation.Nullable)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1