Skip to content

Commit 7f88a85

Browse files
committed
Make testClosePendingCreateChannelFutureDuringPoolClosure less time-based.
1 parent bf466c5 commit 7f88a85

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

pushy/src/main/java/com/eatthepath/pushy/apns/ApnsChannelPool.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ private void discardChannel(final Channel channel) {
235235
* @return a {@code Future} that will be completed when all resources held by this pool have been released
236236
*/
237237
public Future<Void> close() {
238-
final Promise<Void> closeDonePromise = new DefaultPromise<>(this.executor);
238+
final Promise<Void> closePromise = new DefaultPromise<>(this.executor);
239+
239240
this.allChannels.close().addListener(allCloseFuture -> {
240241
ApnsChannelPool.this.isClosed = true;
241242

@@ -257,9 +258,10 @@ public Future<Void> close() {
257258
acquisitionPromise.tryFailure(POOL_CLOSED_EXCEPTION);
258259
}
259260

260-
closeDonePromise.setSuccess(null);
261+
closePromise.setSuccess(null);
261262
});
262263
});
263-
return closeDonePromise;
264+
265+
return closePromise;
264266
}
265267
}

pushy/src/test/java/com/eatthepath/pushy/apns/ApnsChannelPoolTest.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import org.junit.jupiter.api.BeforeEach;
3434
import org.junit.jupiter.api.Test;
3535

36+
import java.util.ArrayList;
37+
import java.util.List;
3638
import java.util.concurrent.TimeUnit;
3739
import java.util.concurrent.atomic.AtomicInteger;
3840

@@ -240,15 +242,13 @@ void testPendingAcquisitionsDuringPoolClosure() throws Exception {
240242

241243
@SuppressWarnings("AnonymousInnerClassMayBeStatic")
242244
@Test
243-
void testPendingCreateChannelFutureDuringPoolClosure() throws Exception {
244-
final Promise<Void> createSuccess = new DefaultPromise<>(EVENT_EXECUTOR);
245+
void testClosePendingCreateChannelFutureDuringPoolClosure() throws Exception {
246+
final List<Promise<Channel>> createPromises = new ArrayList<>();
247+
245248
final PooledObjectFactory<Channel> factory = new PooledObjectFactory<Channel>() {
246249
@Override
247250
public Future<Channel> create(final Promise<Channel> promise) {
248-
EVENT_EXECUTOR.schedule(() -> {
249-
promise.trySuccess(new TestChannel(true));
250-
createSuccess.setSuccess(null);
251-
}, 1, TimeUnit.SECONDS);
251+
createPromises.add(promise);
252252
return promise;
253253
}
254254

@@ -261,8 +261,22 @@ public Future<Void> destroy(final Channel channel, final Promise<Void> promise)
261261

262262
final ApnsChannelPool pool = new ApnsChannelPool(factory, 1, EVENT_EXECUTOR, this.metricsListener);
263263

264-
pool.acquire();
265-
pool.close().await();
266-
assertTrue(createSuccess.isSuccess());
264+
final Future<Channel> acquireNewChannelFuture = pool.acquire();
265+
final Future<Channel> acquireReturnedChannelFuture = pool.acquire();
266+
267+
final Future<Void> closeFuture = pool.close();
268+
269+
EVENT_EXECUTOR.submit(() -> {
270+
final TestChannel channel = new TestChannel(true);
271+
createPromises.forEach(channelPromise -> channelPromise.trySuccess(channel));
272+
});
273+
274+
closeFuture.await();
275+
276+
assertTrue(acquireNewChannelFuture.await().isSuccess(),
277+
"Futures waiting for new connections at pool closure should succeed.");
278+
279+
assertFalse(acquireReturnedChannelFuture.await().isSuccess(),
280+
"Futures waiting for existing connections at pool closure should fail.");
267281
}
268282
}

0 commit comments

Comments
 (0)