Skip to content

Commit cc772f4

Browse files
committed
feat Enabled read/write lock for GitHubSanityCachedValue [2172](hub4j#2172)
1 parent de55c0a commit cc772f4

2 files changed

Lines changed: 26 additions & 28 deletions

File tree

src/main/java/org/kohsuke/github/GitHubSanityCachedValue.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ <E extends Throwable> T get(Function<T, Boolean> isExpired, SupplierThrows<T, E>
4040
} finally {
4141
readLock.unlock();
4242
}
43-
4443
writeLock.lock();
4544
try {
4645
boolean stillExpired = Instant.now().getEpochSecond() > lastQueriedAtEpochSeconds || isExpired.apply(lastResult);

src/test/java/org/kohsuke/github/GitHubSanityCachedValueTest.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515

1616
public class GitHubSanityCachedValueTest {
1717

18+
private static void alignToStartOfSecond() {
19+
while (Instant.now().getNano() > 100_000_000) {
20+
Thread.yield();
21+
}
22+
}
23+
1824
@Test
1925
public void cachesWithinSameSecond() throws Exception {
2026
alignToStartOfSecond();
@@ -35,28 +41,6 @@ public void cachesWithinSameSecond() throws Exception {
3541
assertThat(calls.get(), equalTo(1));
3642
}
3743

38-
@Test
39-
public void refreshesAfterOneSecond() throws Exception {
40-
GitHubSanityCachedValue<String> cachedValue = new GitHubSanityCachedValue<>();
41-
AtomicInteger calls = new AtomicInteger();
42-
43-
String first = cachedValue.get(() -> {
44-
calls.incrementAndGet();
45-
return "value";
46-
});
47-
48-
Thread.sleep(1100);
49-
50-
String second = cachedValue.get(() -> {
51-
calls.incrementAndGet();
52-
return "value";
53-
});
54-
55-
assertThat(first, equalTo("value"));
56-
assertThat(second, equalTo("value"));
57-
assertThat(calls.get(), equalTo(2));
58-
}
59-
6044
@Test
6145
public void concurrentCallersOnlyRefreshOnce() throws Exception {
6246
alignToStartOfSecond();
@@ -98,10 +82,25 @@ public void concurrentCallersOnlyRefreshOnce() throws Exception {
9882
}
9983
}
10084

101-
private static void alignToStartOfSecond() {
102-
while (Instant.now().getNano() > 100_000_000) {
103-
Thread.yield();
104-
}
85+
@Test
86+
public void refreshesAfterOneSecond() throws Exception {
87+
GitHubSanityCachedValue<String> cachedValue = new GitHubSanityCachedValue<>();
88+
AtomicInteger calls = new AtomicInteger();
89+
90+
String first = cachedValue.get(() -> {
91+
calls.incrementAndGet();
92+
return "value";
93+
});
94+
95+
Thread.sleep(1100);
96+
97+
String second = cachedValue.get(() -> {
98+
calls.incrementAndGet();
99+
return "value";
100+
});
101+
102+
assertThat(first, equalTo("value"));
103+
assertThat(second, equalTo("value"));
104+
assertThat(calls.get(), equalTo(2));
105105
}
106106
}
107-

0 commit comments

Comments
 (0)