|
| 1 | +package org.kohsuke.github; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.time.Instant; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.List; |
| 9 | +import java.util.concurrent.CountDownLatch; |
| 10 | +import java.util.concurrent.atomic.AtomicInteger; |
| 11 | + |
| 12 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 13 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 14 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 15 | + |
| 16 | +public class GitHubSanityCachedValueTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + public void cachesWithinSameSecond() throws Exception { |
| 20 | + alignToStartOfSecond(); |
| 21 | + GitHubSanityCachedValue<String> cachedValue = new GitHubSanityCachedValue<>(); |
| 22 | + AtomicInteger calls = new AtomicInteger(); |
| 23 | + |
| 24 | + String first = cachedValue.get(() -> { |
| 25 | + calls.incrementAndGet(); |
| 26 | + return "value"; |
| 27 | + }); |
| 28 | + String second = cachedValue.get(() -> { |
| 29 | + calls.incrementAndGet(); |
| 30 | + return "value"; |
| 31 | + }); |
| 32 | + |
| 33 | + assertThat(first, equalTo("value")); |
| 34 | + assertThat(second, equalTo("value")); |
| 35 | + assertThat(calls.get(), equalTo(1)); |
| 36 | + } |
| 37 | + |
| 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 | + |
| 60 | + @Test |
| 61 | + public void concurrentCallersOnlyRefreshOnce() throws Exception { |
| 62 | + alignToStartOfSecond(); |
| 63 | + GitHubSanityCachedValue<String> cachedValue = new GitHubSanityCachedValue<>(); |
| 64 | + AtomicInteger calls = new AtomicInteger(); |
| 65 | + List<String> results = Collections.synchronizedList(new ArrayList<>()); |
| 66 | + CountDownLatch ready = new CountDownLatch(5); |
| 67 | + CountDownLatch start = new CountDownLatch(1); |
| 68 | + CountDownLatch finished = new CountDownLatch(5); |
| 69 | + |
| 70 | + for (int i = 0; i < 5; i++) { |
| 71 | + Thread thread = new Thread(() -> { |
| 72 | + try { |
| 73 | + ready.countDown(); |
| 74 | + start.await(); |
| 75 | + String value = cachedValue.get((result) -> result == null, () -> { |
| 76 | + calls.incrementAndGet(); |
| 77 | + return "value"; |
| 78 | + }); |
| 79 | + results.add(value); |
| 80 | + } catch (Exception ignored) { |
| 81 | + results.add(null); |
| 82 | + } finally { |
| 83 | + finished.countDown(); |
| 84 | + } |
| 85 | + }); |
| 86 | + thread.start(); |
| 87 | + } |
| 88 | + |
| 89 | + ready.await(); |
| 90 | + start.countDown(); |
| 91 | + finished.await(); |
| 92 | + |
| 93 | + assertThat(calls.get(), equalTo(1)); |
| 94 | + assertThat(results.size(), equalTo(5)); |
| 95 | + for (String result : results) { |
| 96 | + assertThat(result, notNullValue()); |
| 97 | + assertThat(result, equalTo("value")); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static void alignToStartOfSecond() { |
| 102 | + while (Instant.now().getNano() > 100_000_000) { |
| 103 | + Thread.yield(); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
0 commit comments