|
| 1 | +package io.split.client.impressions; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.time.ZoneId; |
| 6 | +import java.time.ZonedDateTime; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import static org.hamcrest.CoreMatchers.is; |
| 11 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 12 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 13 | + |
| 14 | +public class ImpressionCounterTest { |
| 15 | + |
| 16 | + private long makeTimestamp(int year, int month, int day, int hour, int minute, int second) { |
| 17 | + return ZonedDateTime.of(year, month, day, hour, minute, second, 0, ZoneId.of("UTC")).toInstant().toEpochMilli(); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testTruncateTimeFrame() { |
| 22 | + assertThat(ImpressionCounter.truncateTimeframe(makeTimestamp(2020, 9, 2, 10, 53, 12)), |
| 23 | + is(equalTo(makeTimestamp(2020, 9, 2, 10, 0, 0)))); |
| 24 | + assertThat(ImpressionCounter.truncateTimeframe(makeTimestamp(2020, 9, 2, 10, 0, 0)), |
| 25 | + is(equalTo(makeTimestamp(2020, 9, 2, 10, 0, 0)))); |
| 26 | + assertThat(ImpressionCounter.truncateTimeframe(makeTimestamp(2020, 9, 2, 10, 53, 0 )), |
| 27 | + is(equalTo(makeTimestamp(2020, 9, 2, 10, 0, 0)))); |
| 28 | + assertThat(ImpressionCounter.truncateTimeframe(makeTimestamp(2020, 9, 2, 10, 0, 12)), |
| 29 | + is(equalTo(makeTimestamp(2020, 9, 2, 10, 0, 0)))); |
| 30 | + assertThat(ImpressionCounter.truncateTimeframe(makeTimestamp(1970, 1, 1, 0, 0, 0)), |
| 31 | + is(equalTo(makeTimestamp(1970, 1, 1, 0, 0, 0)))); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testMakeKey() { |
| 36 | + long targetTZ = makeTimestamp(2020, 9, 2, 10, 0, 0); |
| 37 | + assertThat(ImpressionCounter.makeKey("someFeature", makeTimestamp(2020, 9, 2, 10, 5, 23)), |
| 38 | + is(equalTo("someFeature::" + targetTZ))); |
| 39 | + assertThat(ImpressionCounter.makeKey("", makeTimestamp(2020, 9, 2, 10, 5, 23)), |
| 40 | + is(equalTo("::" + targetTZ))); |
| 41 | + assertThat(ImpressionCounter.makeKey(null, makeTimestamp(2020, 9, 2, 10, 5, 23)), |
| 42 | + is(equalTo("null::" + targetTZ))); |
| 43 | + assertThat(ImpressionCounter.makeKey(null, 0L), is(equalTo("null::0"))); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testBasicUsage() { |
| 48 | + final ImpressionCounter counter = new ImpressionCounter(); |
| 49 | + final long timestamp = makeTimestamp(2020, 9, 2, 10, 10, 12); |
| 50 | + counter.inc("feature1", timestamp, 1); |
| 51 | + counter.inc("feature1", timestamp + 1, 1); |
| 52 | + counter.inc("feature1", timestamp + 2, 1); |
| 53 | + counter.inc("feature2", timestamp + 3, 2); |
| 54 | + counter.inc("feature2", timestamp + 4, 2); |
| 55 | + Map<String, Integer> counted = counter.popAll(); |
| 56 | + assertThat(counted.size(), is(equalTo(2))); |
| 57 | + assertThat(counted.get(ImpressionCounter.makeKey("feature1", timestamp)), is(equalTo(3))); |
| 58 | + assertThat(counted.get(ImpressionCounter.makeKey("feature2", timestamp)), is(equalTo(4))); |
| 59 | + assertThat(counter.popAll().size(), is(equalTo(0))); |
| 60 | + |
| 61 | + final long nextHourTimestamp = makeTimestamp(2020, 9, 2, 11, 10, 12); |
| 62 | + counter.inc("feature1", timestamp, 1); |
| 63 | + counter.inc("feature1", timestamp + 1, 1); |
| 64 | + counter.inc("feature1", timestamp + 2, 1); |
| 65 | + counter.inc("feature2", timestamp + 3, 2); |
| 66 | + counter.inc("feature2", timestamp + 4, 2); |
| 67 | + counter.inc("feature1", nextHourTimestamp, 1); |
| 68 | + counter.inc("feature1", nextHourTimestamp + 1, 1); |
| 69 | + counter.inc("feature1", nextHourTimestamp + 2, 1); |
| 70 | + counter.inc("feature2", nextHourTimestamp + 3, 2); |
| 71 | + counter.inc("feature2", nextHourTimestamp + 4, 2); |
| 72 | + counted = counter.popAll(); |
| 73 | + assertThat(counted.size(), is(equalTo(4))); |
| 74 | + assertThat(counted.get(ImpressionCounter.makeKey("feature1", timestamp)), is(equalTo(3))); |
| 75 | + assertThat(counted.get(ImpressionCounter.makeKey("feature2", timestamp)), is(equalTo(4))); |
| 76 | + assertThat(counted.get(ImpressionCounter.makeKey("feature1", nextHourTimestamp)), is(equalTo(3))); |
| 77 | + assertThat(counted.get(ImpressionCounter.makeKey("feature2", nextHourTimestamp)), is(equalTo(4))); |
| 78 | + assertThat(counter.popAll().size(), is(equalTo(0))); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void manyConcurrentCalls() throws InterruptedException { |
| 83 | + final int iterations = 10000000; |
| 84 | + final long timestamp = makeTimestamp(2020, 9, 2, 10, 10, 12); |
| 85 | + final long nextHourTimestamp = makeTimestamp(2020, 9, 2, 11, 10, 12); |
| 86 | + ImpressionCounter counter = new ImpressionCounter(); |
| 87 | + Thread t1 = new Thread(() -> { |
| 88 | + int times = iterations; |
| 89 | + while (times-- > 0) { |
| 90 | + counter.inc("feature1", timestamp, 1); |
| 91 | + counter.inc("feature2", timestamp, 1); |
| 92 | + counter.inc("feature1", nextHourTimestamp, 2); |
| 93 | + counter.inc("feature2", nextHourTimestamp, 2); |
| 94 | + } |
| 95 | + }); |
| 96 | + Thread t2 = new Thread(() -> { |
| 97 | + int times = iterations; |
| 98 | + while (times-- > 0) { |
| 99 | + counter.inc("feature1", timestamp, 2); |
| 100 | + counter.inc("feature2", timestamp, 2); |
| 101 | + counter.inc("feature1", nextHourTimestamp, 1); |
| 102 | + counter.inc("feature2", nextHourTimestamp, 1); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + t1.setDaemon(true); t2.setDaemon(true); |
| 107 | + t1.start(); t2.start(); |
| 108 | + t1.join(); t2.join(); |
| 109 | + |
| 110 | + HashMap<String, Integer> counted = counter.popAll(); |
| 111 | + assertThat(counted.size(), is(equalTo(4))); |
| 112 | + assertThat(counted.get(ImpressionCounter.makeKey("feature1", timestamp)), is(equalTo(iterations * 3))); |
| 113 | + assertThat(counted.get(ImpressionCounter.makeKey("feature2", timestamp)), is(equalTo(iterations * 3))); |
| 114 | + assertThat(counted.get(ImpressionCounter.makeKey("feature1", nextHourTimestamp)), is(equalTo(iterations * 3))); |
| 115 | + assertThat(counted.get(ImpressionCounter.makeKey("feature2", nextHourTimestamp)), is(equalTo(iterations * 3))); |
| 116 | + } |
| 117 | +} |
0 commit comments