Skip to content

Commit 8f460fb

Browse files
committed
Fix and unmute org.elasticsearch.script.StatsSummaryTests:testEqualsAndHashCode (elastic#115922)
This commit fixes and unmutes org.elasticsearch.script.StatsSummaryTests:testEqualsAndHashCode. Previously, there was no guarantee that the doubles added to stats1 and stats2 will be different. In fact, the count may even be zero - which we seen in one particular failure. The simplest thing here, to avoid this potential situation, is to ensure that there is at least one value, and that the values added to each stats instance are different.
1 parent 794e922 commit 8f460fb

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

muted-tests.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,6 @@ tests:
151151
- class: org.elasticsearch.xpack.ml.integration.MlJobIT
152152
method: testGetJob_GivenNoSuchJob
153153
issue: https://github.com/elastic/elasticsearch/issues/112730
154-
- class: org.elasticsearch.script.StatsSummaryTests
155-
method: testEqualsAndHashCode
156-
issue: https://github.com/elastic/elasticsearch/issues/112439
157154
- class: org.elasticsearch.xpack.ml.integration.MlJobIT
158155
method: testDeleteJobAfterMissingAliases
159156
issue: https://github.com/elastic/elasticsearch/issues/112823

server/src/test/java/org/elasticsearch/script/StatsSummaryTests.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,21 @@ public void testEqualsAndHashCode() {
7575
assertThat(stats1, equalTo(stats2));
7676
assertThat(stats1.hashCode(), equalTo(stats2.hashCode()));
7777

78+
// Accumulators with same sum, but different counts are not equals
79+
stats1.accept(1);
80+
stats1.accept(1);
81+
stats2.accept(2);
82+
assertThat(stats1.getSum(), equalTo(stats2.getSum()));
83+
assertThat(stats1.getCount(), not(equalTo(stats2.getCount())));
84+
assertThat(stats1, not(equalTo(stats2)));
85+
assertThat(stats1.hashCode(), not(equalTo(stats2.hashCode())));
86+
7887
// Accumulators with different values are not equals
79-
randomDoubles(randomIntBetween(0, 20)).forEach(stats1);
80-
randomDoubles(randomIntBetween(0, 20)).forEach(stats2);
88+
stats1.reset();
89+
stats2.reset();
90+
randomDoubles(randomIntBetween(1, 20)).forEach(stats1.andThen(v -> stats2.accept(v + randomDoubleBetween(0.0, 1.0, false))));
91+
assertThat(stats1.getCount(), equalTo(stats2.getCount()));
92+
assertThat(stats1.getSum(), not(equalTo(stats2.getSum())));
8193
assertThat(stats1, not(equalTo(stats2)));
8294
assertThat(stats1.hashCode(), not(equalTo(stats2.hashCode())));
8395
}

0 commit comments

Comments
 (0)