Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -685,9 +685,6 @@ private void offerAvailable(final Segment seg, final PoolEntry<R, C> p) {
}

private PoolEntry<R, C> pollAvailable(final Segment seg, final Object neededState) {
if (neededState == null) {
return seg.available.pollFirst();
}
for (final PoolEntry<R, C> p : seg.available) {
if (compatible(neededState, p.getState())) {
if (seg.available.remove(p)) {
Expand All @@ -699,7 +696,7 @@ private PoolEntry<R, C> pollAvailable(final Segment seg, final Object neededStat
}

private boolean compatible(final Object needed, final Object have) {
return needed == null || Objects.equals(needed, have);
return Objects.equals(needed, have);
}

private boolean handOffToCompatibleWaiter(final PoolEntry<R, C> entry, final Segment seg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -112,20 +113,21 @@ void perRouteAndTotalLimits() throws Exception {
}

@Test
void stateCompatibilityNullMatchesAnything() throws Exception {
void stateCompatibilityNullOnlyMatchesNull() throws Exception {
final RouteSegmentedConnPool<String, FakeConnection> pool =
newPool(1, 1, TimeValue.NEG_ONE_MILLISECOND, PoolReusePolicy.LIFO, FakeConnection::close);
newPool(2, 2, TimeValue.NEG_ONE_MILLISECOND, PoolReusePolicy.LIFO, FakeConnection::close);

final PoolEntry<String, FakeConnection> e = pool.lease("r", null, Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS);
e.assignConnection(new FakeConnection());
e.updateState("X");
e.updateExpiry(TimeValue.ofSeconds(30));
pool.release(e, true);

// waiter with null state must match
// a lease with null state must not be handed an entry bound to state "X"
final PoolEntry<String, FakeConnection> got =
pool.lease("r", null, Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS);
assertSame(e, got);
assertNotSame(e, got);
assertFalse(got.hasConnection());
pool.release(got, false);
pool.close(CloseMode.IMMEDIATE);
}
Expand Down Expand Up @@ -519,4 +521,53 @@ void cancelledAndReenqueuedLiveSegmentDoesNotBlockAnotherPendingRoute() throws E
pool.close(CloseMode.IMMEDIATE);
}

@Test
void statelessLeaseMustNotReuseAnotherUsersStatefulEntry() throws Exception {
final RouteSegmentedConnPool<String, FakeConnection> pool =
newPool(2, 2, TimeValue.NEG_ONE_MILLISECOND, PoolReusePolicy.LIFO, FakeConnection::close);

final PoolEntry<String, FakeConnection> aliceEntry =
pool.lease("route", "alice", Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS);
final FakeConnection aliceConnection = new FakeConnection();
aliceEntry.assignConnection(aliceConnection);
aliceEntry.updateState("alice");
pool.release(aliceEntry, true);

final PoolEntry<String, FakeConnection> anonEntry =
pool.lease("route", null, Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS);

assertFalse(anonEntry.hasConnection(),
"A lease with no state must not be handed alice's authenticated connection");

pool.release(anonEntry, false);
pool.close(CloseMode.IMMEDIATE);
}

@Test
void strictAndLaxPoolsDoNotReuseAnotherUsersStatefulEntry() throws Exception {
final StrictConnPool<String, FakeConnection> strict =
new StrictConnPool<>(2, 2, TimeValue.NEG_ONE_MILLISECOND, PoolReusePolicy.LIFO, FakeConnection::close, null);
final PoolEntry<String, FakeConnection> strictAlice =
strict.lease("route", "alice", Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS);
strictAlice.assignConnection(new FakeConnection());
strictAlice.updateState("alice");
strict.release(strictAlice, true);
final PoolEntry<String, FakeConnection> strictAnon =
strict.lease("route", null, Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS);
assertFalse(strictAnon.hasConnection(), "StrictConnPool must not reuse alice's entry for a stateless lease");
strict.close(CloseMode.IMMEDIATE);

final LaxConnPool<String, FakeConnection> lax =
new LaxConnPool<>(2, TimeValue.NEG_ONE_MILLISECOND, PoolReusePolicy.LIFO, FakeConnection::close, null);
final PoolEntry<String, FakeConnection> laxAlice =
lax.lease("route", "alice", Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS);
laxAlice.assignConnection(new FakeConnection());
laxAlice.updateState("alice");
lax.release(laxAlice, true);
final PoolEntry<String, FakeConnection> laxAnon =
lax.lease("route", null, Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS);
assertFalse(laxAnon.hasConnection(), "LaxConnPool must not reuse alice's entry for a stateless lease");
lax.close(CloseMode.IMMEDIATE);
}

}
Loading