|
3 | 3 | import static org.hamcrest.MatcherAssert.assertThat;
|
4 | 4 | import static org.hamcrest.Matchers.is;
|
5 | 5 | import static org.hamcrest.Matchers.nullValue;
|
6 |
| -import static org.jenkinsci.plugins.github.admin.GitHubDuplicateEventsMonitor.DuplicateEventsSubscriber.getEventCountsTracker; |
7 |
| -import static org.jenkinsci.plugins.github.admin.GitHubDuplicateEventsMonitor.DuplicateEventsSubscriber.getLastDuplicate; |
8 |
| -import static org.jenkinsci.plugins.github.admin.GitHubDuplicateEventsMonitor.DuplicateEventsSubscriber.isDuplicateEventSeen; |
9 | 6 |
|
10 | 7 | import java.time.Duration;
|
11 | 8 | import java.time.Instant;
|
|
21 | 18 | @For(GitHubDuplicateEventsMonitor.DuplicateEventsSubscriber.class)
|
22 | 19 | public class GitHubDuplicateEventsMonitorUnitTest {
|
23 | 20 |
|
24 |
| - private final GitHubDuplicateEventsMonitor.DuplicateEventsSubscriber subscriber |
25 |
| - = new GitHubDuplicateEventsMonitor.DuplicateEventsSubscriber(); |
26 |
| - |
27 | 21 | @Test
|
28 | 22 | public void onEventShouldTrackEventAndKeepTrackOfLastDuplicate() {
|
| 23 | + var subscriber = new GitHubDuplicateEventsMonitor.DuplicateEventsSubscriber(); |
| 24 | + |
29 | 25 | var now = Instant.parse("2025-02-05T03:00:00Z");
|
30 | 26 | var after1Sec = Instant.parse("2025-02-05T03:00:01Z");
|
31 | 27 | var after2Sec = Instant.parse("2025-02-05T03:00:02Z");
|
32 | 28 | FakeTicker fakeTicker = new FakeTicker(now);
|
33 |
| - GitHubDuplicateEventsMonitor.DuplicateEventsSubscriber.setTicker(fakeTicker); |
| 29 | + subscriber.setTicker(fakeTicker); |
34 | 30 |
|
35 |
| - assertThat("lastDuplicate is null at first", getLastDuplicate(), is(nullValue())); |
36 |
| - assertThat("should not throw NPE", isDuplicateEventSeen(), is(false)); |
| 31 | + assertThat("lastDuplicate is null at first", subscriber.getLastDuplicate(), is(nullValue())); |
| 32 | + assertThat("should not throw NPE", subscriber.isDuplicateEventSeen(), is(false)); |
37 | 33 | // send a null event
|
38 | 34 | subscriber.onEvent(new GHSubscriberEvent(null, "origin", GHEvent.PUSH, "payload"));
|
39 |
| - assertThat("null event is not tracked", getEventCountsTracker().size(), is(0)); |
40 |
| - assertThat("lastDuplicate is still null", getLastDuplicate(), is(nullValue())); |
| 35 | + assertThat("null event is not tracked", subscriber.getEventCountsTracker().size(), is(0)); |
| 36 | + assertThat("lastDuplicate is still null", subscriber.getLastDuplicate(), is(nullValue())); |
41 | 37 |
|
42 | 38 | // at present
|
43 | 39 | subscriber.onEvent(new GHSubscriberEvent("1", "origin", GHEvent.PUSH, "payload"));
|
44 |
| - assertThat(getEventCountsTracker(), is(Set.of("1"))); |
45 |
| - assertThat(getLastDuplicate(), is(nullValue())); |
46 |
| - assertThat(isDuplicateEventSeen(), is(false)); |
| 40 | + assertThat(subscriber.getEventCountsTracker(), is(Set.of("1"))); |
| 41 | + assertThat(subscriber.getLastDuplicate(), is(nullValue())); |
| 42 | + assertThat(subscriber.isDuplicateEventSeen(), is(false)); |
47 | 43 | subscriber.onEvent(new GHSubscriberEvent("2", "origin", GHEvent.PUSH, "payload"));
|
48 |
| - assertThat(getLastDuplicate(), is(nullValue())); |
49 |
| - assertThat(isDuplicateEventSeen(), is(false)); |
50 |
| - assertThat(getEventCountsTracker(), is(Set.of("1", "2"))); |
| 44 | + assertThat(subscriber.getLastDuplicate(), is(nullValue())); |
| 45 | + assertThat(subscriber.isDuplicateEventSeen(), is(false)); |
| 46 | + assertThat(subscriber.getEventCountsTracker(), is(Set.of("1", "2"))); |
51 | 47 | subscriber.onEvent(new GHSubscriberEvent(null, "origin", GHEvent.PUSH, "payload"));
|
52 |
| - assertThat(getLastDuplicate(), is(nullValue())); |
53 |
| - assertThat(getEventCountsTracker(), is(Set.of("1", "2"))); |
54 |
| - assertThat(isDuplicateEventSeen(), is(false)); |
| 48 | + assertThat(subscriber.getLastDuplicate(), is(nullValue())); |
| 49 | + assertThat(subscriber.getEventCountsTracker(), is(Set.of("1", "2"))); |
| 50 | + assertThat(subscriber.isDuplicateEventSeen(), is(false)); |
55 | 51 |
|
56 | 52 | // after a second
|
57 | 53 | fakeTicker.advance(Duration.ofSeconds(1));
|
58 | 54 | subscriber.onEvent(new GHSubscriberEvent("1", "origin", GHEvent.PUSH, "payload"));
|
59 |
| - assertThat(getLastDuplicate().eventGuid(), is("1")); |
60 |
| - assertThat(getLastDuplicate().lastUpdated(), is(after1Sec)); |
61 |
| - assertThat(getEventCountsTracker(), is(Set.of("1", "2"))); |
62 |
| - assertThat(isDuplicateEventSeen(), is(true)); |
| 55 | + assertThat(subscriber.getLastDuplicate().eventGuid(), is("1")); |
| 56 | + assertThat(subscriber.getLastDuplicate().lastUpdated(), is(after1Sec)); |
| 57 | + assertThat(subscriber.getEventCountsTracker(), is(Set.of("1", "2"))); |
| 58 | + assertThat(subscriber.isDuplicateEventSeen(), is(true)); |
63 | 59 |
|
64 | 60 | // second occurrence for another event after 2 seconds
|
65 | 61 | fakeTicker.advance(Duration.ofSeconds(1));
|
66 | 62 | subscriber.onEvent(new GHSubscriberEvent("2", "origin", GHEvent.PUSH, "payload"));
|
67 |
| - assertThat(getLastDuplicate().eventGuid(), is("2")); |
68 |
| - assertThat(getLastDuplicate().lastUpdated(), is(after2Sec)); |
69 |
| - assertThat(getEventCountsTracker(), is(Set.of("1", "2"))); |
70 |
| - assertThat(isDuplicateEventSeen(), is(true)); |
| 63 | + assertThat(subscriber.getLastDuplicate().eventGuid(), is("2")); |
| 64 | + assertThat(subscriber.getLastDuplicate().lastUpdated(), is(after2Sec)); |
| 65 | + assertThat(subscriber.getEventCountsTracker(), is(Set.of("1", "2"))); |
| 66 | + assertThat(subscriber.isDuplicateEventSeen(), is(true)); |
71 | 67 |
|
72 | 68 | // 24 hours has passed; note we already added 2 seconds/ so effectively 24h 2sec now.
|
73 | 69 | fakeTicker.advance(Duration.ofHours(24));
|
74 |
| - assertThat(isDuplicateEventSeen(), is(false)); |
| 70 | + assertThat(subscriber.isDuplicateEventSeen(), is(false)); |
75 | 71 | }
|
76 | 72 |
|
77 | 73 | @Test
|
78 | 74 | public void checkOldEntriesAreExpiredAfter10Minutes() {
|
| 75 | + var subscriber = new GitHubDuplicateEventsMonitor.DuplicateEventsSubscriber(); |
| 76 | + |
79 | 77 | var now = Instant.parse("2025-02-05T03:00:00Z");
|
80 | 78 | FakeTicker fakeTicker = new FakeTicker(now);
|
81 |
| - GitHubDuplicateEventsMonitor.DuplicateEventsSubscriber.setTicker(fakeTicker); |
| 79 | + subscriber.setTicker(fakeTicker); |
82 | 80 |
|
83 | 81 | // at present
|
84 | 82 | subscriber.onEvent(new GHSubscriberEvent("1", "origin", GHEvent.PUSH, "payload"));
|
85 | 83 | subscriber.onEvent(new GHSubscriberEvent("2", "origin", GHEvent.PUSH, "payload"));
|
86 |
| - assertThat(getEventCountsTracker(), is(Set.of("1", "2"))); |
| 84 | + assertThat(subscriber.getEventCountsTracker(), is(Set.of("1", "2"))); |
87 | 85 |
|
88 | 86 | // after 2 minutes
|
89 | 87 | fakeTicker.advance(Duration.ofMinutes(2));
|
90 | 88 | subscriber.onEvent(new GHSubscriberEvent("3", "origin", GHEvent.PUSH, "payload"));
|
91 | 89 | subscriber.onEvent(new GHSubscriberEvent("4", "origin", GHEvent.PUSH, "payload"));
|
92 |
| - assertThat(getEventCountsTracker(), is(Set.of("1", "2", "3", "4"))); |
93 |
| - assertThat(getEventCountsTracker().size(), is(4)); |
| 90 | + assertThat(subscriber.getEventCountsTracker(), is(Set.of("1", "2", "3", "4"))); |
| 91 | + assertThat(subscriber.getEventCountsTracker().size(), is(4)); |
94 | 92 |
|
95 | 93 | // 10 minutes 1 second later
|
96 | 94 | fakeTicker.advance(Duration.ofMinutes(8).plusSeconds(1));
|
97 |
| - assertThat(getEventCountsTracker(), is(Set.of("3", "4"))); |
98 |
| - assertThat(getEventCountsTracker().size(), is(2)); |
| 95 | + assertThat(subscriber.getEventCountsTracker(), is(Set.of("3", "4"))); |
| 96 | + assertThat(subscriber.getEventCountsTracker().size(), is(2)); |
99 | 97 | }
|
100 | 98 |
|
101 | 99 | private static class FakeTicker implements Ticker {
|
|
0 commit comments