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