Skip to content

Commit b1cf61a

Browse files
committed
fixing SamplingServiceTests
1 parent 60faa3c commit b1cf61a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

server/src/test/java/org/elasticsearch/ingest/SamplingServiceTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void testMaybeSample() {
5959
ProjectMetadata.Builder projectBuilder = ProjectMetadata.builder(ProjectId.DEFAULT);
6060
final ProjectId projectId = projectBuilder.getId();
6161
ProjectMetadata projectMetadata = projectBuilder.build();
62+
ClusterState originalClusterState = ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(projectMetadata).build();
6263
Map<String, Object> inputRawDocSource = randomMap(1, 100, () -> Tuple.tuple(randomAlphaOfLength(10), randomAlphaOfLength(10)));
6364
final IndexRequest indexRequest = new IndexRequest(indexName).id("_id").source(inputRawDocSource);
6465
samplingService.maybeSample(projectMetadata, indexRequest);
@@ -74,6 +75,26 @@ public void testMaybeSample() {
7475
)
7576
);
7677
projectMetadata = projectBuilder.build();
78+
{
79+
/*
80+
* First we ingest some docs without notifying samplingService of the cluster state change. It will have cached the fact that
81+
* there is no config for this index, and so it will not store any samples.
82+
*/
83+
int docsToSample = randomIntBetween(1, maxSize);
84+
for (int i = 0; i < docsToSample; i++) {
85+
samplingService.maybeSample(projectMetadata, indexRequest);
86+
}
87+
List<SamplingService.RawDocument> sample = samplingService.getLocalSample(projectId, indexName);
88+
assertThat(sample, empty());
89+
}
90+
// Now we notify samplingService that the cluster state has changed, and it will pick up the new sampling config
91+
samplingService.clusterChanged(
92+
new ClusterChangedEvent(
93+
"test",
94+
ClusterState.builder(ClusterState.EMPTY_STATE).putProjectMetadata(projectMetadata).build(),
95+
originalClusterState
96+
)
97+
);
7798
int docsToSample = randomIntBetween(1, maxSize);
7899
for (int i = 0; i < docsToSample; i++) {
79100
samplingService.maybeSample(projectMetadata, indexRequest);

0 commit comments

Comments
 (0)