Skip to content

Commit df2c06f

Browse files
committed
[TEST] Fix ClusterApplierServiceTests.testClusterStateUpdateLogging
This changes the test to not use a `CountDownlatch`, instead adding an assertion for the final logging message and waiting until the `MockAppender` has seen it before proceeding. Resolves #23739
1 parent ee9a271 commit df2c06f

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

server/src/test/java/org/elasticsearch/cluster/service/ClusterApplierServiceTests.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,22 @@ public void testClusterStateUpdateLogging() throws Exception {
128128
clusterApplierService.getClass().getCanonicalName(),
129129
Level.TRACE,
130130
"*failed to execute cluster state applier in [2s]*"));
131+
mockAppender.addExpectation(
132+
new MockLogAppender.SeenEventExpectation(
133+
"test3",
134+
clusterApplierService.getClass().getCanonicalName(),
135+
Level.DEBUG,
136+
"*processing [test3]: took [0s] no change in cluster state*"));
131137

132138
Logger clusterLogger = LogManager.getLogger("org.elasticsearch.cluster.service");
133139
Loggers.addAppender(clusterLogger, mockAppender);
134140
try {
135-
final CountDownLatch latch = new CountDownLatch(3);
136141
clusterApplierService.currentTimeOverride = System.nanoTime();
137142
clusterApplierService.runOnApplierThread("test1",
138143
currentState -> clusterApplierService.currentTimeOverride += TimeValue.timeValueSeconds(1).nanos(),
139144
new ClusterApplyListener() {
140145
@Override
141-
public void onSuccess(String source) {
142-
latch.countDown();
143-
}
146+
public void onSuccess(String source) { }
144147

145148
@Override
146149
public void onFailure(String source, Exception e) {
@@ -159,31 +162,25 @@ public void onSuccess(String source) {
159162
}
160163

161164
@Override
162-
public void onFailure(String source, Exception e) {
163-
latch.countDown();
164-
}
165+
public void onFailure(String source, Exception e) { }
165166
});
166167
// Additional update task to make sure all previous logging made it to the loggerName
167-
// We don't check logging for this on since there is no guarantee that it will occur before our check
168168
clusterApplierService.runOnApplierThread("test3",
169169
currentState -> {},
170170
new ClusterApplyListener() {
171171
@Override
172-
public void onSuccess(String source) {
173-
latch.countDown();
174-
}
172+
public void onSuccess(String source) { }
175173

176174
@Override
177175
public void onFailure(String source, Exception e) {
178176
fail();
179177
}
180178
});
181-
latch.await();
179+
assertBusy(mockAppender::assertAllExpectationsMatched);
182180
} finally {
183181
Loggers.removeAppender(clusterLogger, mockAppender);
184182
mockAppender.stop();
185183
}
186-
mockAppender.assertAllExpectationsMatched();
187184
}
188185

189186
@TestLogging("org.elasticsearch.cluster.service:WARN") // To ensure that we log cluster state events on WARN level

test/framework/src/main/java/org/elasticsearch/test/MockLogAppender.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public UnseenEventExpectation(String name, String logger, Level level, String me
112112

113113
@Override
114114
public void assertMatched() {
115-
assertThat(name, saw, equalTo(false));
115+
assertThat("expected to see " + name + " but did not", saw, equalTo(false));
116116
}
117117
}
118118

@@ -124,7 +124,7 @@ public SeenEventExpectation(String name, String logger, Level level, String mess
124124

125125
@Override
126126
public void assertMatched() {
127-
assertThat(name, saw, equalTo(true));
127+
assertThat("expected to see " + name + " but did not", saw, equalTo(true));
128128
}
129129
}
130130

0 commit comments

Comments
 (0)