Skip to content

Commit 7b5b9d0

Browse files
authored
[ML] Avoid 5s wait in AbstractNativeProcessTests (#74916)
Calling close() before counting down the latch was causing a 5 second wait and timeout in the tests
1 parent 49256c1 commit 7b5b9d0

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
@@ -87,19 +87,23 @@ public void terminateExecutorService() {
8787
public void testStart_DoNotDetectCrashWhenNoInputPipeProvided() throws Exception {
8888
when(processPipes.getProcessInStream()).thenReturn(Optional.empty());
8989
try (AbstractNativeProcess process = new TestNativeProcess()) {
90-
process.start(executorService);
91-
} finally {
92-
mockNativeProcessLoggingStreamEnds.countDown();
93-
// Not detecting a crash is confirmed in terminateExecutorService()
90+
try {
91+
process.start(executorService);
92+
} finally {
93+
mockNativeProcessLoggingStreamEnds.countDown();
94+
// Not detecting a crash is confirmed in terminateExecutorService()
95+
}
9496
}
9597
}
9698

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

@@ -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)