Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -59,6 +60,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static io.servicetalk.client.api.ServiceDiscovererEvent.Status.AVAILABLE;
import static io.servicetalk.client.api.ServiceDiscovererEvent.Status.EXPIRED;
Expand Down Expand Up @@ -88,9 +90,11 @@
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -701,7 +705,11 @@ void resubscribeToEventsWhenAllHostsAreUnhealthy() throws Exception {

// Events for the new Subscriber change the state
sendServiceDiscoveryEvents(upEvent("address-2"), upEvent("address-3"), upEvent("address-4"));
assertAddresses(lb.usedAddresses(), "address-2", "address-3", "address-4");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory this will affect all uses of assertAddresses: can we fix assertAddresses directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will investigate a way to fix assertAddress and assertConnectionCount directly!

Copy link
Contributor Author

@lawrencewang49 lawrencewang49 Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hello @bryce-anderson, i have made changes to edit assertAddresses and assertConnectionCount! I have changed assertConnectionCount to use a containsInAnyOrder instead of a contains during the assertion, and used a list to iterate through the addresses for assertAddresses. the tests should now pass with both ./gradlew :servicetalk-loadbalancer:test --tests <qualified_test_path> and ./gradlew :servicetalk-loadbalancer:nondexTest --tests <qualified_test_path>

I removed ordering assumptions and avoided Hamcrest reflection matchers, comparing actual extracted keys instead. Because p2c didn't have deterministic selection, I used containsInAnyOrder to match this behavior

assertThat(lb.usedAddresses(), containsInAnyOrder(
hasProperty("key", is("address-2")),
hasProperty("key", is("address-3")),
hasProperty("key", is("address-4"))
));

// Verify the LB is recovered
Map<String, Matcher<? super String>> expected = new HashMap<>();
Expand All @@ -712,11 +720,14 @@ void resubscribeToEventsWhenAllHostsAreUnhealthy() throws Exception {
assertThat(selected1, is(anyOf(expected.values())));

if (isRoundRobin()) {
// These asserts are flaky for p2c because we don't have deterministic selection.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is still valuable.

expected.remove(selected1);
assertThat(lb.selectConnection(any(), null).toFuture().get().address(), is(anyOf(expected.values())));
assertConnectionCount(lb.usedAddresses(), connectionsCount("address-2", 0),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a similar problem with getConnectionCount: can we take the same approach as above and fix the assertion helper method?

connectionsCount("address-3", 1), connectionsCount("address-4", 1));
Map<String, Integer> connectionCounts = lb.usedAddresses().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().size()));

assertEquals(0, connectionCounts.getOrDefault("address-2", -1).intValue(), "address-2 count mismatch");
assertEquals(1, connectionCounts.getOrDefault("address-3", -1).intValue(), "address-3 count mismatch");
assertEquals(1, connectionCounts.getOrDefault("address-4", -1).intValue(), "address-4 count mismatch");
}
}

Expand Down