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)));
}
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();
}
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);
});
}
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()));
}
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.");
}
Aggregations