Skip to content

Commit 27fcf7b

Browse files
Hendrik Muhsbenwtrent
authored andcommitted
reuse mock client to avoid probles with thread context closed errors (#46398)
1 parent f336c74 commit 27fcf7b

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

x-pack/plugin/data-frame/src/test/java/org/elasticsearch/xpack/dataframe/checkpoint/DataFrameTransformCheckpointServiceNodeTests.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
import org.elasticsearch.xpack.dataframe.DataFrameSingleNodeTestCase;
5151
import org.elasticsearch.xpack.dataframe.notifications.DataFrameAuditor;
5252
import org.elasticsearch.xpack.dataframe.persistence.DataFrameTransformsConfigManager;
53-
import org.junit.After;
53+
import org.junit.AfterClass;
5454
import org.junit.Before;
5555

5656
import java.nio.file.Path;
@@ -67,8 +67,12 @@
6767

6868
public class DataFrameTransformCheckpointServiceNodeTests extends DataFrameSingleNodeTestCase {
6969

70+
// re-use the mock client for the whole test suite as the underlying thread pool and the
71+
// corresponding context if recreated cause unreliable test execution
72+
// see https://github.com/elastic/elasticsearch/issues/45238 and https://github.com/elastic/elasticsearch/issues/42577
73+
private static MockClientForCheckpointing mockClientForCheckpointing = null;
74+
7075
private DataFrameTransformsConfigManager transformsConfigManager;
71-
private MockClientForCheckpointing mockClientForCheckpointing;
7276
private DataFrameTransformsCheckpointService transformsCheckpointService;
7377

7478
private class MockClientForCheckpointing extends NoOpClient {
@@ -121,18 +125,22 @@ void doExecute(ActionType<Response> action, Request request, ActionListener<Resp
121125

122126
@Before
123127
public void createComponents() {
128+
// it's not possible to run it as @BeforeClass as clients aren't initialized
129+
if (mockClientForCheckpointing == null) {
130+
mockClientForCheckpointing = new MockClientForCheckpointing("DataFrameTransformCheckpointServiceNodeTests");
131+
}
132+
124133
transformsConfigManager = new DataFrameTransformsConfigManager(client(), xContentRegistry());
125134

126135
// use a mock for the checkpoint service
127-
mockClientForCheckpointing = new MockClientForCheckpointing(getTestName());
128136
DataFrameAuditor mockAuditor = mock(DataFrameAuditor.class);
129137
transformsCheckpointService = new DataFrameTransformsCheckpointService(mockClientForCheckpointing,
130138
transformsConfigManager,
131139
mockAuditor);
132140
}
133141

134-
@After
135-
public void tearDownClient() {
142+
@AfterClass
143+
public static void tearDownClient() {
136144
mockClientForCheckpointing.close();
137145
}
138146

0 commit comments

Comments
 (0)