Skip to content

Conversation

@Demogorgon314
Copy link
Member

Motivation

Move assert equals and retry to a base class.

Documentation

Need to update docs?

  • doc-required
  • no-need-doc
  • doc

@github-actions github-actions bot added the doc-not-needed Your PR changes do not impact docs label Mar 23, 2022
@Demogorgon314
Copy link
Member Author

/pulsarbot rerun-failure-checks

1 similar comment
@Demogorgon314
Copy link
Member Author

/pulsarbot rerun-failure-checks

@codelipenghui codelipenghui added this to the 2.11.0 milestone Mar 24, 2022
@codelipenghui codelipenghui added area/test type/refactor Code or documentation refactors. e.g. refactor code structure or methods to improve code readability labels Mar 24, 2022
@codelipenghui codelipenghui merged commit d0d9aa5 into apache:master Mar 24, 2022
Comment on lines +108 to 139

public static void assertEqualsAndRetry(Supplier<Object> actual,
Object expected,
Object expectedAndRetry) throws Exception {
assertEqualsAndRetry(actual, expected, expectedAndRetry, 5, 100);
}

public static void assertEqualsAndRetry(Supplier<Object> actual,
Object expected,
Object expectedAndRetry,
int retryCount,
long intSleepTimeInMillis) throws Exception {
assertTrue(retryStrategically((__) -> {
if (actual.get().equals(expectedAndRetry)) {
return false;
}
assertEquals(actual.get(), expected);
return true;
}, retryCount, intSleepTimeInMillis));
}

public static boolean retryStrategically(Predicate<Void> predicate, int retryCount, long intSleepTimeInMillis)
throws Exception {
for (int i = 0; i < retryCount; i++) {
if (predicate.test(null)) {
return true;
}
Thread.sleep(intSleepTimeInMillis + (intSleepTimeInMillis * i));
}
return false;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Please don't expand the usage of these methods.
Awaitability should be used instead of this approach.

Copy link
Member

Choose a reason for hiding this comment

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

+1 - I think we should consolidate and only use Awaitility for these kinds of tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

If we use Awaitility, then we need to use Fail-Fast Conditions feature.

But we need to upgrade the awaitility dependency to 4.1.0 or higher first. (Current Pulsar are using 4.0.3)

Example:

Awaitility.await().failFast(() -> {
    Optional<Map<String, String>> cachedValue = objCache.getIfCached(key1);

    // Need ensure objCache.getIfCached(key1) don't return a wrong value.
    // Only retry when objCache.getIfCached(key1) return Optional.empty() or Optional.of(v)
    return cachedValue.isPresent() && !Optional.of(v).equals(cachedValue);
}).untilAsserted(() -> assertEquals(objCache.getIfCached(key1), Optional.of(v)));

@lhotari @michaeljmarshall Do you have a better idea when using the current version of awaitility? Thanks!

@lhotari
Copy link
Member

lhotari commented Mar 24, 2022

@Demogorgon314 A better approach would be to remove retryStrategically completely, as I did in #14518 (which didn't get merged since it was obsolete)

@lhotari
Copy link
Member

lhotari commented Mar 24, 2022

There was a similar discussion about the desire to replace retryStrategically with Availability in #14373 (comment) .

@Demogorgon314 Demogorgon314 deleted the refactor/move_assert_equals_and_retry_to_base_class branch March 25, 2022 01:39
Nicklee007 pushed a commit to Nicklee007/pulsar that referenced this pull request Apr 20, 2022
nicoloboschi pushed a commit to datastax/pulsar that referenced this pull request May 9, 2022
nicoloboschi pushed a commit to datastax/pulsar that referenced this pull request May 9, 2022
codelipenghui pushed a commit that referenced this pull request Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/test cherry-picked/branch-2.10 doc-not-needed Your PR changes do not impact docs release/2.10.1 type/refactor Code or documentation refactors. e.g. refactor code structure or methods to improve code readability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants