public ProxyClientFactory(ApplicationState state) { super(state.tracing()); this.clientPool = new ClientPool(state.config().getClientPoolSize()); }
@Test public void releaseAndAcquireThreadSafety() throws Exception { // given a client pool ClientPool pool = new ClientPool(25); // when acquire and release occurs many times on multiple threads List<Completable> completables = IntStream.range(0, 1000) .mapToObj(index -> acquireAndReleaseAsync(pool)) .collect(Collectors.toList()); // then an exception is NOT thrown Completable.merge(completables).blockingAwait(); }
@Test public void sameInstance() throws Exception { ClientPool pool = new ClientPool(2); assertEquals(0, pool.countAvailable()); Client client = mockClient("localhost"); IntStream.range(0, 10).forEach(i -> pool.release(client)); assertEquals(1, pool.countAvailable()); Client client2 = mockClient("google"); IntStream.range(0, 10).forEach(i -> pool.release(client2)); assertEquals(2, pool.countAvailable()); }
@Test public void releaseAndAcquire() throws Exception { ClientPool pool = new ClientPool(2); assertEquals(0, pool.countAvailable()); IntStream.range(0, 10).forEach(i -> pool.release(mockClient("localhost"))); assertEquals(2, pool.countAvailable()); IntStream.range(0, 10).forEach(i -> pool.release(mockClient("google"))); assertEquals(4, pool.countAvailable()); IntStream.range(0, 2) .forEach( i -> pool.acquire( mockFrontContext(), mockConfig("localhost"), () -> mockClient("localhost"))); assertEquals(2, pool.countAvailable()); IntStream.range(0, 2) .forEach( i -> pool.acquire( mockFrontContext(), mockConfig("google"), () -> mockClient("localhost"))); assertEquals(0, pool.countAvailable()); }