1
1
package org .jenkinsci .plugins .github_branch_source ;
2
2
3
3
import static org .hamcrest .MatcherAssert .assertThat ;
4
- import static org .hamcrest .Matchers .* ;
4
+ import static org .hamcrest .Matchers .is ;
5
5
6
6
import hudson .util .Secret ;
7
+ import java .math .BigDecimal ;
7
8
import java .time .Duration ;
8
9
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 ;
9
14
import org .junit .Test ;
10
15
11
16
public class GithubAppCredentialsAppInstallationTokenTest {
@@ -22,7 +27,7 @@ public void testAppInstallationTokenStale() throws Exception {
22
27
assertThat (token .isStale (), is (false ));
23
28
assertThat (
24
29
token .getTokenStaleEpochSeconds (),
25
- equalTo (now + GitHubAppCredentials .AppInstallationToken .NOT_STALE_MINIMUM_SECONDS ));
30
+ closeTo (now + GitHubAppCredentials .AppInstallationToken .NOT_STALE_MINIMUM_SECONDS , 3 ));
26
31
27
32
now = Instant .now ().getEpochSecond ();
28
33
token =
@@ -31,7 +36,7 @@ public void testAppInstallationTokenStale() throws Exception {
31
36
assertThat (token .isStale (), is (false ));
32
37
assertThat (
33
38
token .getTokenStaleEpochSeconds (),
34
- equalTo (now + GitHubAppCredentials .AppInstallationToken .NOT_STALE_MINIMUM_SECONDS ));
39
+ closeTo (now + GitHubAppCredentials .AppInstallationToken .NOT_STALE_MINIMUM_SECONDS , 3 ));
35
40
36
41
now = Instant .now ().getEpochSecond ();
37
42
token =
@@ -41,7 +46,7 @@ public void testAppInstallationTokenStale() throws Exception {
41
46
assertThat (token .isStale (), is (false ));
42
47
assertThat (
43
48
token .getTokenStaleEpochSeconds (),
44
- equalTo (now + GitHubAppCredentials .AppInstallationToken .NOT_STALE_MINIMUM_SECONDS ));
49
+ closeTo (now + GitHubAppCredentials .AppInstallationToken .NOT_STALE_MINIMUM_SECONDS , 3 ));
45
50
46
51
now = Instant .now ().getEpochSecond ();
47
52
token =
@@ -52,7 +57,7 @@ public void testAppInstallationTokenStale() throws Exception {
52
57
+ Duration .ofMinutes (7 ).getSeconds ());
53
58
assertThat (token .isStale (), is (false ));
54
59
assertThat (
55
- token .getTokenStaleEpochSeconds (), equalTo (now + Duration .ofMinutes (7 ).getSeconds ()));
60
+ token .getTokenStaleEpochSeconds (), closeTo (now + Duration .ofMinutes (7 ).getSeconds (), 3 ));
56
61
57
62
now = Instant .now ().getEpochSecond ();
58
63
token =
@@ -61,8 +66,9 @@ public void testAppInstallationTokenStale() throws Exception {
61
66
assertThat (token .isStale (), is (false ));
62
67
assertThat (
63
68
token .getTokenStaleEpochSeconds (),
64
- equalTo (now + GitHubAppCredentials .AppInstallationToken .STALE_AFTER_SECONDS + 1 ));
69
+ closeTo (now + GitHubAppCredentials .AppInstallationToken .STALE_AFTER_SECONDS + 1 , 3 ));
65
70
71
+ // TODO use FlagRule
66
72
long notStaleSeconds = GitHubAppCredentials .AppInstallationToken .NOT_STALE_MINIMUM_SECONDS ;
67
73
try {
68
74
// Should revert to 1 second minimum
@@ -71,7 +77,7 @@ public void testAppInstallationTokenStale() throws Exception {
71
77
now = Instant .now ().getEpochSecond ();
72
78
token = new GitHubAppCredentials .AppInstallationToken (secret , now );
73
79
assertThat (token .isStale (), is (false ));
74
- assertThat (token .getTokenStaleEpochSeconds (), equalTo (now + 1 ));
80
+ assertThat (token .getTokenStaleEpochSeconds (), closeTo (now + 1 , 3 ));
75
81
76
82
// Verify goes stale
77
83
Thread .sleep (1000 );
@@ -80,4 +86,25 @@ public void testAppInstallationTokenStale() throws Exception {
80
86
GitHubAppCredentials .AppInstallationToken .NOT_STALE_MINIMUM_SECONDS = notStaleSeconds ;
81
87
}
82
88
}
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
+ }
83
110
}
0 commit comments