Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Hamcrest matchers and assertThat() in ReindexRenamedSettingTests to improve readability #2503

Merged

Conversation

tlfeng
Copy link
Collaborator

@tlfeng tlfeng commented Mar 18, 2022

Description

  • Use Hamcrest matchers and assertThat() in ReindexRenamedSettingTests to improve the readability.

Reason:
The order of the assertEquals() arguments in ReindexRenamedSettingTests class is reversed.

The unit test class ReindexRenamedSettingTests were added in PR #2221 / commit 63c75d1

According to the comment in #2463 (comment):

The first argument of the assert is the expected value, and the second is the actual value.

Issues Resolved

Related to issue #1547

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@tlfeng tlfeng added :test Adding or fixing a test v2.0.0 Version 2.0.0 non-issue bugs / unexpected behaviors that end up non issues; audit trail simple changes that aren't issues labels Mar 18, 2022
@tlfeng tlfeng requested a review from a team as a code owner March 18, 2022 06:07
@opensearch-ci-bot
Copy link
Collaborator

❌   Gradle Check failure 29ffc42
Log 3509

Reports 3509

@tlfeng
Copy link
Collaborator Author

tlfeng commented Mar 18, 2022

in log 3509:

> Task :server:internalClusterTest

REPRODUCE WITH: ./gradlew ':server:internalClusterTest' --tests "org.opensearch.search.SearchCancellationIT.testMSearchChildRequestCancellationWithClusterLevelTimeout" -Dtests.seed=E7E590B85679E426 -Dtests.security.manager=true -Dtests.jvm.argline="-XX:TieredStopAtLevel=1 -XX:ReservedCodeCacheSize=64m" -Dtests.locale=no -Dtests.timezone=Australia/South -Druntime.java=17

org.opensearch.search.SearchCancellationIT > testMSearchChildRequestCancellationWithClusterLevelTimeout FAILED
    java.lang.AssertionError: Actual child request with cancellation failure is different that expected expected:<[0, 1]> but was:<[]>
        at __randomizedtesting.SeedInfo.seed([E7E590B85679E426:59585B5DC76F0451]:0)
        at org.junit.Assert.fail(Assert.java:89)
        at org.junit.Assert.failNotEquals(Assert.java:835)
        at org.junit.Assert.assertEquals(Assert.java:120)
        at org.opensearch.search.SearchCancellationIT.ensureMSearchWasCancelled(SearchCancellationIT.java:194)
        at org.opensearch.search.SearchCancellationIT.testMSearchChildRequestCancellationWithClusterLevelTimeout(SearchCancellationIT.java:537)

It's reported in issue #2242

@opensearch-ci-bot
Copy link
Collaborator

❌   Gradle Check failure 4b0abf6
Log 3518

Reports 3518

Copy link
Member

@andrross andrross left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first argument of the assert is the expected value, and the second is the actual value.

FYI, this is one reason to prefer the Hamcrest matcher style of assertThat(actualValue, equalTo(expectedValue)) because it reads more naturally and you don't have know the ordering.

@tlfeng
Copy link
Collaborator Author

tlfeng commented Mar 18, 2022

@andrross Thank you for sharing the knowledge! Now I know assertThat() should be preferred to use.

Hamcrest strives to make your tests as readable as possible.

p.s. the advantages of assertThat() are shown in the release notes of junit 4.4: http://junit.sourceforge.net/doc/ReleaseNotes4.4.html

@tlfeng
Copy link
Collaborator Author

tlfeng commented Mar 18, 2022

In Log 3518:

> Task :qa:remote-clusters:integTest FAILED

REPRODUCE WITH: ./gradlew ':qa:remote-clusters:integTest' --tests "org.opensearch.cluster.remote.test.RemoteClustersIT.testHAProxyModeConnectionWorks" -Dtests.seed=DB46B9E87253F055 -Dtests.security.manager=true -Dtests.jvm.argline="-XX:TieredStopAtLevel=1 -XX:ReservedCodeCacheSize=64m" -Dtests.locale=no -Dtests.timezone=America/Argentina/San_Juan -Druntime.java=17

WARNING: A terminally deprecated method in java.lang.System has been called
WARNING: System::setSecurityManager has been called by org.gradle.api.internal.tasks.testing.worker.TestWorker (file:/home/ubuntu/.gradle/wrapper/dists/gradle-7.3.3-all/4295vidhdd9hd3gbjyw1xqxpo/gradle-7.3.3/lib/plugins/gradle-testing-base-7.3.3.jar)
WARNING: Please consider reporting this to the maintainers of org.gradle.api.internal.tasks.testing.worker.TestWorker
WARNING: System::setSecurityManager will be removed in a future release
org.opensearch.cluster.remote.test.RemoteClustersIT > testHAProxyModeConnectionWorks FAILED
    java.lang.AssertionError
        at __randomizedtesting.SeedInfo.seed([DB46B9E87253F055:DC306C2349D58608]:0)
        at org.junit.Assert.fail(Assert.java:87)
        at org.junit.Assert.assertTrue(Assert.java:42)
        at org.junit.Assert.assertTrue(Assert.java:53)
        at org.opensearch.cluster.remote.test.RemoteClustersIT.testHAProxyModeConnectionWorks(RemoteClustersIT.java:125)

It's reported in #1703, this one fails very often recently.

@tlfeng tlfeng force-pushed the flip-assertequal-allowlist-setting branch from 755f677 to eccb012 Compare March 18, 2022 18:40
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Settings;
import org.opensearch.test.OpenSearchTestCase;

import java.util.Arrays;
import java.util.List;

import static org.hamcrest.Matchers.equalTo;
Copy link
Collaborator Author

@tlfeng tlfeng Mar 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to suggestion from @andrross , I updated all the assertions to use assertThat() and Matchers API from Hamcrest, in this file.

@opensearch-ci-bot
Copy link
Collaborator

❌   Gradle Check failure 755f677ecc3d9dd91a0a4b8aa57f74fa528b5c8e
Log 3522

Reports 3522

@opensearch-ci-bot
Copy link
Collaborator

✅   Gradle Check success eccb012
Log 3523

Reports 3523

@tlfeng
Copy link
Collaborator Author

tlfeng commented Mar 18, 2022

In log 3522:

REPRODUCE WITH: ./gradlew ':server:internalClusterTest' --tests "org.opensearch.gateway.RecoveryFromGatewayIT.testReuseInFileBasedPeerRecovery" -Dtests.seed=678B15424FD7EBE5 -Dtests.security.manager=true -Dtests.jvm.argline="-XX:TieredStopAtLevel=1 -XX:ReservedCodeCacheSize=64m" -Dtests.locale=sr-RS -Dtests.timezone=Pacific/Midway -Druntime.java=17

org.opensearch.gateway.RecoveryFromGatewayIT > testReuseInFileBasedPeerRecovery FAILED
    java.lang.AssertionError: shard [test][0] on node [node_t1] has pending operations:
     --> RetentionLeaseBackgroundSyncAction.Request{retentionLeases=RetentionLeases{primaryTerm=1, version=1480, leases={peer_recovery/awjm15faRpeBMJiOvn_QMQ=RetentionLease{id='peer_recovery/awjm15faRpeBMJiOvn_QMQ', retainingSequenceNumber=786, timestamp=1647629686105, source='peer recovery'}, peer_recovery/7LnHvaeSRV6f3UsOaIJARg=RetentionLease{id='peer_recovery/7LnHvaeSRV6f3UsOaIJARg', retainingSequenceNumber=786, timestamp=1647629686105, source='peer recovery'}}}, shardId=[test][0], timeout=1m, index='test', waitForActiveShards=0}
    	at org.opensearch.index.shard.IndexShardOperationPermits.acquire(IndexShardOperationPermits.java:248)
    	at org.opensearch.index.shard.IndexShard.acquirePrimaryOperationPermit(IndexShard.java:3146)

It's reported in issue #1746, this one also fails frequently recently.

@tlfeng tlfeng changed the title Flip assertEquals() arguments in ReindexRenamedSettingTests Use Hamcrest matchers and assertThat() in ReindexRenamedSettingTests Mar 18, 2022
@opensearch-ci-bot
Copy link
Collaborator

❌   Gradle Check failure ce4adbb
Log 3527

Reports 3527

@opensearch-ci-bot
Copy link
Collaborator

❌   Gradle Check failure 5d77f8d
Log 3529

Reports 3529

@opensearch-ci-bot
Copy link
Collaborator

❌   Gradle Check failure 37d6fe4
Log 3535

Reports 3535

@tlfeng
Copy link
Collaborator Author

tlfeng commented Mar 19, 2022

In report 3535:
failed test:
org.opensearch.index.ShardIndexingPressureConcurrentExecutionTests.html#testCoordinatingPrimaryThreadedUpdateToShardLimitsAndRejections

start gradle check

@opensearch-ci-bot
Copy link
Collaborator

✅   Gradle Check success 37d6fe4
Log 3540

Reports 3540

@tlfeng tlfeng merged commit a853253 into opensearch-project:main Mar 19, 2022
@tlfeng tlfeng changed the title Use Hamcrest matchers and assertThat() in ReindexRenamedSettingTests Use Hamcrest matchers and assertThat() in ReindexRenamedSettingTests to improve readability Mar 19, 2022
@tlfeng tlfeng deleted the flip-assertequal-allowlist-setting branch March 19, 2022 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
non-issue bugs / unexpected behaviors that end up non issues; audit trail simple changes that aren't issues :test Adding or fixing a test v2.0.0 Version 2.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants