Skip to content

Commit fabcf70

Browse files
Switching evictor tests to use a deterministic queue (#105151)
1 parent 43362d5 commit fabcf70

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/external/http/IdleConnectionEvictorTests.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@
1010
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
1111
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
1212
import org.apache.http.nio.reactor.IOReactorException;
13+
import org.elasticsearch.common.util.concurrent.DeterministicTaskQueue;
1314
import org.elasticsearch.core.TimeValue;
1415
import org.elasticsearch.test.ESTestCase;
1516
import org.elasticsearch.threadpool.Scheduler;
1617
import org.elasticsearch.threadpool.ThreadPool;
17-
import org.junit.After;
1818
import org.junit.Before;
1919

2020
import java.util.concurrent.CountDownLatch;
2121
import java.util.concurrent.TimeUnit;
2222

23-
import static org.elasticsearch.xpack.inference.Utils.inferenceUtilityPool;
2423
import static org.mockito.ArgumentMatchers.any;
2524
import static org.mockito.ArgumentMatchers.anyLong;
2625
import static org.mockito.Mockito.doAnswer;
@@ -32,16 +31,11 @@
3231
public class IdleConnectionEvictorTests extends ESTestCase {
3332

3433
private static final TimeValue TIMEOUT = new TimeValue(30, TimeUnit.SECONDS);
35-
private ThreadPool threadPool;
34+
private DeterministicTaskQueue taskQueue;
3635

3736
@Before
3837
public void init() {
39-
threadPool = createThreadPool(inferenceUtilityPool());
40-
}
41-
42-
@After
43-
public void shutdown() {
44-
terminate(threadPool);
38+
taskQueue = new DeterministicTaskQueue();
4539
}
4640

4741
public void testStart_CallsExecutorSubmit() throws IOReactorException {
@@ -87,7 +81,7 @@ public void testCloseExpiredConnections_IsCalled() throws InterruptedException {
8781
var manager = mock(PoolingNHttpClientConnectionManager.class);
8882

8983
var evictor = new IdleConnectionEvictor(
90-
threadPool,
84+
taskQueue.getThreadPool(),
9185
manager,
9286
new TimeValue(1, TimeUnit.NANOSECONDS),
9387
new TimeValue(1, TimeUnit.NANOSECONDS)
@@ -100,7 +94,8 @@ public void testCloseExpiredConnections_IsCalled() throws InterruptedException {
10094
return Void.TYPE;
10195
}).when(manager).closeExpiredConnections();
10296

103-
evictor.start();
97+
startEvictor(evictor);
98+
10499
runLatch.await(TIMEOUT.getSeconds(), TimeUnit.SECONDS);
105100

106101
verify(manager, times(1)).closeExpiredConnections();
@@ -110,7 +105,7 @@ public void testCloseIdleConnections_IsCalled() throws InterruptedException {
110105
var manager = mock(PoolingNHttpClientConnectionManager.class);
111106

112107
var evictor = new IdleConnectionEvictor(
113-
threadPool,
108+
taskQueue.getThreadPool(),
114109
manager,
115110
new TimeValue(1, TimeUnit.NANOSECONDS),
116111
new TimeValue(1, TimeUnit.NANOSECONDS)
@@ -123,40 +118,47 @@ public void testCloseIdleConnections_IsCalled() throws InterruptedException {
123118
return Void.TYPE;
124119
}).when(manager).closeIdleConnections(anyLong(), any());
125120

126-
evictor.start();
121+
startEvictor(evictor);
122+
127123
runLatch.await(TIMEOUT.getSeconds(), TimeUnit.SECONDS);
128124

129125
verify(manager, times(1)).closeIdleConnections(anyLong(), any());
130126
}
131127

132128
public void testIsRunning_ReturnsTrue() throws IOReactorException {
133129
var evictor = new IdleConnectionEvictor(
134-
threadPool,
130+
taskQueue.getThreadPool(),
135131
createConnectionManager(),
136132
new TimeValue(1, TimeUnit.SECONDS),
137133
new TimeValue(1, TimeUnit.SECONDS)
138134
);
139135

140-
evictor.start();
136+
startEvictor(evictor);
137+
141138
assertTrue(evictor.isRunning());
142139
evictor.close();
143140
}
144141

145142
public void testIsRunning_ReturnsFalse() throws IOReactorException {
146143
var evictor = new IdleConnectionEvictor(
147-
threadPool,
144+
taskQueue.getThreadPool(),
148145
createConnectionManager(),
149146
new TimeValue(1, TimeUnit.SECONDS),
150147
new TimeValue(1, TimeUnit.SECONDS)
151148
);
152149

153-
evictor.start();
150+
startEvictor(evictor);
154151
assertTrue(evictor.isRunning());
155152

156153
evictor.close();
157154
assertFalse(evictor.isRunning());
158155
}
159156

157+
private void startEvictor(IdleConnectionEvictor evictor) {
158+
taskQueue.scheduleNow(evictor::start);
159+
taskQueue.runAllRunnableTasks();
160+
}
161+
160162
private static PoolingNHttpClientConnectionManager createConnectionManager() throws IOReactorException {
161163
return new PoolingNHttpClientConnectionManager(new DefaultConnectingIOReactor());
162164
}

0 commit comments

Comments
 (0)