Skip to content

Commit ac5066b

Browse files
Add some tests
1 parent 5ad9c60 commit ac5066b

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright © 2024 Apple Inc. and the ServiceTalk project authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.servicetalk.loadbalancer;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import java.util.ArrayList;
21+
import java.util.HashSet;
22+
import java.util.List;
23+
import java.util.Set;
24+
import java.util.function.Predicate;
25+
26+
import static org.hamcrest.MatcherAssert.assertThat;
27+
import static org.hamcrest.Matchers.greaterThan;
28+
import static org.hamcrest.Matchers.hasItem;
29+
import static org.hamcrest.Matchers.hasSize;
30+
import static org.hamcrest.Matchers.not;
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertNotNull;
33+
34+
class CorePoolConnectionPoolStrategyTest {
35+
36+
List<TestLoadBalancedConnection> makeConnections(int size) {
37+
List<TestLoadBalancedConnection> result = new ArrayList<>(size);
38+
for (int i = 0; i < size; i++) {
39+
result.add(TestLoadBalancedConnection.mockConnection("address-" + i));
40+
}
41+
return result;
42+
}
43+
44+
@Test
45+
void selectsHosts() {
46+
List<TestLoadBalancedConnection> connections = makeConnections(1);
47+
ConnectionPoolStrategy<TestLoadBalancedConnection> strategy =
48+
new CorePoolConnectionPoolStrategy<>(1, false);
49+
assertNotNull(strategy.select(connections, c -> true, null));
50+
}
51+
52+
@Test
53+
void prefersCorePool() {
54+
List<TestLoadBalancedConnection> connections = makeConnections(10);
55+
ConnectionPoolStrategy<TestLoadBalancedConnection> strategy =
56+
new CorePoolConnectionPoolStrategy<>(5, false);
57+
Set<TestLoadBalancedConnection> selected = new HashSet<>();
58+
for (int i = 0; i < 100; i++) {
59+
selected.add(strategy.select(connections, c -> true, null));
60+
}
61+
// Commonly we should have more than one element in strategy, although we can expect it to contain a single
62+
// element with a probability of 0.2^99 or ~6e-70.
63+
assertThat(selected, hasSize(greaterThan(1)));
64+
65+
// We should not have selected any of the non-core pool connections.
66+
for (int i = 5; i < 10; i++) {
67+
assertThat(selected, not(hasItem(connections.get(i))));
68+
}
69+
}
70+
71+
@Test
72+
void spillsIntoOverflow() {
73+
List<TestLoadBalancedConnection> connections = makeConnections(6);
74+
ConnectionPoolStrategy<TestLoadBalancedConnection> strategy =
75+
new CorePoolConnectionPoolStrategy<>(5, false);
76+
Set<TestLoadBalancedConnection> corePoolCxns = new HashSet<>();
77+
for (int i = 0; i < 5; i++) {
78+
corePoolCxns.add(connections.get(i));
79+
}
80+
Predicate<TestLoadBalancedConnection> selector = (TestLoadBalancedConnection c) -> !corePoolCxns.contains(c);
81+
assertEquals(connections.get(5), strategy.select(connections, selector, null));
82+
}
83+
}

0 commit comments

Comments
 (0)