Skip to content

Commit 84d12a2

Browse files
committed
GithubAppCredentialsAppInstallationTokenTest from jenkinsci#326 was flaking; use closeTo for actual times
1 parent cad8d3c commit 84d12a2

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

src/test/java/org/jenkinsci/plugins/github_branch_source/GithubAppCredentialsAppInstallationTokenTest.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package org.jenkinsci.plugins.github_branch_source;
22

33
import static org.hamcrest.MatcherAssert.assertThat;
4-
import static org.hamcrest.Matchers.*;
4+
import static org.hamcrest.Matchers.is;
55

66
import hudson.util.Secret;
7+
import java.math.BigDecimal;
78
import java.time.Duration;
89
import java.time.Instant;
10+
import org.hamcrest.Description;
11+
import org.hamcrest.Matcher;
12+
import org.hamcrest.TypeSafeMatcher;
13+
import org.hamcrest.number.BigDecimalCloseTo;
914
import org.junit.Test;
1015

1116
public class GithubAppCredentialsAppInstallationTokenTest {
@@ -22,7 +27,7 @@ public void testAppInstallationTokenStale() throws Exception {
2227
assertThat(token.isStale(), is(false));
2328
assertThat(
2429
token.getTokenStaleEpochSeconds(),
25-
equalTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS));
30+
closeTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS, 3));
2631

2732
now = Instant.now().getEpochSecond();
2833
token =
@@ -31,7 +36,7 @@ public void testAppInstallationTokenStale() throws Exception {
3136
assertThat(token.isStale(), is(false));
3237
assertThat(
3338
token.getTokenStaleEpochSeconds(),
34-
equalTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS));
39+
closeTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS, 3));
3540

3641
now = Instant.now().getEpochSecond();
3742
token =
@@ -41,7 +46,7 @@ public void testAppInstallationTokenStale() throws Exception {
4146
assertThat(token.isStale(), is(false));
4247
assertThat(
4348
token.getTokenStaleEpochSeconds(),
44-
equalTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS));
49+
closeTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS, 3));
4550

4651
now = Instant.now().getEpochSecond();
4752
token =
@@ -52,7 +57,7 @@ public void testAppInstallationTokenStale() throws Exception {
5257
+ Duration.ofMinutes(7).getSeconds());
5358
assertThat(token.isStale(), is(false));
5459
assertThat(
55-
token.getTokenStaleEpochSeconds(), equalTo(now + Duration.ofMinutes(7).getSeconds()));
60+
token.getTokenStaleEpochSeconds(), closeTo(now + Duration.ofMinutes(7).getSeconds(), 3));
5661

5762
now = Instant.now().getEpochSecond();
5863
token =
@@ -61,8 +66,9 @@ public void testAppInstallationTokenStale() throws Exception {
6166
assertThat(token.isStale(), is(false));
6267
assertThat(
6368
token.getTokenStaleEpochSeconds(),
64-
equalTo(now + GitHubAppCredentials.AppInstallationToken.STALE_AFTER_SECONDS + 1));
69+
closeTo(now + GitHubAppCredentials.AppInstallationToken.STALE_AFTER_SECONDS + 1, 3));
6570

71+
// TODO use FlagRule
6672
long notStaleSeconds = GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS;
6773
try {
6874
// Should revert to 1 second minimum
@@ -71,7 +77,7 @@ public void testAppInstallationTokenStale() throws Exception {
7177
now = Instant.now().getEpochSecond();
7278
token = new GitHubAppCredentials.AppInstallationToken(secret, now);
7379
assertThat(token.isStale(), is(false));
74-
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + 1));
80+
assertThat(token.getTokenStaleEpochSeconds(), closeTo(now + 1, 3));
7581

7682
// Verify goes stale
7783
Thread.sleep(1000);
@@ -80,4 +86,25 @@ public void testAppInstallationTokenStale() throws Exception {
8086
GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS = notStaleSeconds;
8187
}
8288
}
89+
90+
private static Matcher<Long> closeTo(long operand, long error) {
91+
BigDecimalCloseTo delegate =
92+
new BigDecimalCloseTo(new BigDecimal(operand), new BigDecimal(error));
93+
return new TypeSafeMatcher<Long>(Long.class) {
94+
@Override
95+
protected boolean matchesSafely(Long item) {
96+
return delegate.matches(new BigDecimal(item));
97+
}
98+
99+
@Override
100+
protected void describeMismatchSafely(Long item, Description mismatchDescription) {
101+
delegate.describeMismatchSafely(new BigDecimal(item), mismatchDescription);
102+
}
103+
104+
@Override
105+
public void describeTo(Description description) {
106+
delegate.describeTo(description);
107+
}
108+
};
109+
}
83110
}

0 commit comments

Comments
 (0)