Skip to content

Commit f0ce54c

Browse files
davidkyleelasticsearchmachine
authored and
elasticsearchmachine
committed
[ML] Avoid 5s wait in AbstractNativeProcessTests (elastic#74916)
Calling close() before counting down the latch was causing a 5 second wait and timeout in the tests
1 parent af364df commit f0ce54c

File tree

1 file changed

+52
-39
lines changed

1 file changed

+52
-39
lines changed

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/AbstractNativeProcessTests.java

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,23 @@ public void terminateExecutorService() {
8686
public void testStart_DoNotDetectCrashWhenNoInputPipeProvided() throws Exception {
8787
when(processPipes.getProcessInStream()).thenReturn(Optional.empty());
8888
try (AbstractNativeProcess process = new TestNativeProcess()) {
89-
process.start(executorService);
90-
} finally {
91-
mockNativeProcessLoggingStreamEnds.countDown();
92-
// Not detecting a crash is confirmed in terminateExecutorService()
89+
try {
90+
process.start(executorService);
91+
} finally {
92+
mockNativeProcessLoggingStreamEnds.countDown();
93+
// Not detecting a crash is confirmed in terminateExecutorService()
94+
}
9395
}
9496
}
9597

9698
public void testStart_DoNotDetectCrashWhenProcessIsBeingClosed() throws Exception {
9799
try (AbstractNativeProcess process = new TestNativeProcess()) {
98-
process.start(executorService);
99-
} finally {
100-
mockNativeProcessLoggingStreamEnds.countDown();
101-
// Not detecting a crash is confirmed in terminateExecutorService()
100+
try {
101+
process.start(executorService);
102+
} finally {
103+
mockNativeProcessLoggingStreamEnds.countDown();
104+
// Not detecting a crash is confirmed in terminateExecutorService()
105+
}
102106
}
103107
}
104108

@@ -142,65 +146,74 @@ public void testCrashReporting() throws Exception {
142146

143147
public void testWriteRecord() throws Exception {
144148
try (AbstractNativeProcess process = new TestNativeProcess()) {
145-
process.start(executorService);
146-
process.writeRecord(new String[] {"a", "b", "c"});
147-
process.flushStream();
148-
149-
verify(inputStream).write(any(), anyInt(), anyInt());
150-
151-
} finally {
152-
mockNativeProcessLoggingStreamEnds.countDown();
149+
try {
150+
process.start(executorService);
151+
process.writeRecord(new String[]{"a", "b", "c"});
152+
process.flushStream();
153+
verify(inputStream).write(any(), anyInt(), anyInt());
154+
} finally {
155+
mockNativeProcessLoggingStreamEnds.countDown();
156+
}
153157
}
154158
}
155159

156160
public void testWriteRecord_FailWhenNoInputPipeProvided() throws Exception {
157161
when(processPipes.getProcessInStream()).thenReturn(Optional.empty());
158162
try (AbstractNativeProcess process = new TestNativeProcess()) {
159-
process.start(executorService);
160-
expectThrows(NullPointerException.class, () -> process.writeRecord(new String[] {"a", "b", "c"}));
161-
} finally {
162-
mockNativeProcessLoggingStreamEnds.countDown();
163+
try {
164+
process.start(executorService);
165+
expectThrows(NullPointerException.class, () -> process.writeRecord(new String[]{"a", "b", "c"}));
166+
} finally {
167+
mockNativeProcessLoggingStreamEnds.countDown();
168+
}
163169
}
164170
}
165171

166172
public void testFlush() throws Exception {
167173
try (AbstractNativeProcess process = new TestNativeProcess()) {
168-
process.start(executorService);
169-
process.flushStream();
170-
171-
verify(inputStream).flush();
172-
} finally {
173-
mockNativeProcessLoggingStreamEnds.countDown();
174+
try {
175+
process.start(executorService);
176+
process.flushStream();
177+
verify(inputStream).flush();
178+
} finally {
179+
mockNativeProcessLoggingStreamEnds.countDown();
180+
}
174181
}
175182
}
176183

177184
public void testFlush_FailWhenNoInputPipeProvided() throws Exception {
178185
when(processPipes.getProcessInStream()).thenReturn(Optional.empty());
179186
try (AbstractNativeProcess process = new TestNativeProcess()) {
180-
process.start(executorService);
181-
expectThrows(NullPointerException.class, process::flushStream);
182-
} finally {
183-
mockNativeProcessLoggingStreamEnds.countDown();
187+
try {
188+
process.start(executorService);
189+
expectThrows(NullPointerException.class, process::flushStream);
190+
} finally {
191+
mockNativeProcessLoggingStreamEnds.countDown();
192+
}
184193
}
185194
}
186195

187196
public void testIsReady() throws Exception {
188197
try (AbstractNativeProcess process = new TestNativeProcess()) {
189-
process.start(executorService);
190-
assertThat(process.isReady(), is(false));
191-
process.setReady();
192-
assertThat(process.isReady(), is(true));
193-
} finally {
194-
mockNativeProcessLoggingStreamEnds.countDown();
198+
try {
199+
process.start(executorService);
200+
assertThat(process.isReady(), is(false));
201+
process.setReady();
202+
assertThat(process.isReady(), is(true));
203+
} finally {
204+
mockNativeProcessLoggingStreamEnds.countDown();
205+
}
195206
}
196207
}
197208

198209
public void testConsumeAndCloseOutputStream_GivenNoOutputStream() throws Exception {
199210
when(processPipes.getProcessOutStream()).thenReturn(Optional.empty());
200211
try (AbstractNativeProcess process = new TestNativeProcess()) {
201-
process.consumeAndCloseOutputStream();
202-
} finally {
203-
mockNativeProcessLoggingStreamEnds.countDown();
212+
try {
213+
process.consumeAndCloseOutputStream();
214+
} finally {
215+
mockNativeProcessLoggingStreamEnds.countDown();
216+
}
204217
}
205218
}
206219

0 commit comments

Comments
 (0)