|
1 | 1 | package io.split.engine.segments;
|
2 | 2 |
|
3 | 3 | import com.google.common.collect.Sets;
|
| 4 | +import io.split.cache.InMemoryCacheImp; |
4 | 5 | import io.split.cache.SegmentCache;
|
5 | 6 | import io.split.cache.SegmentCacheInMemoryImpl;
|
| 7 | +import io.split.cache.SplitCache; |
6 | 8 | import io.split.client.dtos.SegmentChange;
|
| 9 | +import io.split.client.dtos.SplitChange; |
7 | 10 | import io.split.engine.SDKReadinessGates;
|
| 11 | +import io.split.engine.common.FetchOptions; |
| 12 | +import io.split.engine.experiments.SplitChangeFetcher; |
| 13 | +import io.split.engine.experiments.SplitFetcherImp; |
| 14 | +import io.split.engine.experiments.SplitParser; |
| 15 | +import org.junit.Assert; |
8 | 16 | import org.junit.Test;
|
| 17 | +import org.mockito.ArgumentCaptor; |
9 | 18 | import org.mockito.Mockito;
|
10 | 19 | import org.slf4j.Logger;
|
11 | 20 | import org.slf4j.LoggerFactory;
|
|
21 | 30 | import static org.junit.Assert.assertNotNull;
|
22 | 31 | import static org.junit.Assert.assertEquals;
|
23 | 32 | import static org.junit.Assert.assertThat;
|
| 33 | +import static org.mockito.Mockito.when; |
24 | 34 |
|
25 | 35 | /**
|
26 | 36 | * Tests for RefreshableSegmentFetcher.
|
@@ -138,6 +148,51 @@ public void does_not_work_if_sdk_readiness_gates_are_null() {
|
138 | 148 | SegmentFetcher fetcher = new SegmentFetcherImp(SEGMENT_NAME, segmentChangeFetcher, null, segmentCache);
|
139 | 149 | }
|
140 | 150 |
|
| 151 | + @Test |
| 152 | + public void testBypassCdnClearedAfterFirstHit() { |
| 153 | + SegmentChangeFetcher mockFetcher = Mockito.mock(SegmentChangeFetcher.class); |
| 154 | + SegmentSynchronizationTask segmentSynchronizationTaskMock = Mockito.mock(SegmentSynchronizationTask.class); |
| 155 | + SegmentCache segmentCacheMock = new SegmentCacheInMemoryImpl(); |
| 156 | + SDKReadinessGates mockGates = Mockito.mock(SDKReadinessGates.class); |
| 157 | + SegmentFetcher fetcher = new SegmentFetcherImp("someSegment", mockFetcher, mockGates, segmentCacheMock); |
| 158 | + |
| 159 | + |
| 160 | + SegmentChange response1 = new SegmentChange(); |
| 161 | + response1.name = "someSegment"; |
| 162 | + response1.added = new ArrayList<>(); |
| 163 | + response1.removed = new ArrayList<>(); |
| 164 | + response1.since = -1; |
| 165 | + response1.till = 1; |
| 166 | + |
| 167 | + SegmentChange response2 = new SegmentChange(); |
| 168 | + response2.name = "someSegment"; |
| 169 | + response2.added = new ArrayList<>(); |
| 170 | + response2.removed = new ArrayList<>(); |
| 171 | + response2.since = 1; |
| 172 | + response1.till = 1; |
| 173 | + |
| 174 | + ArgumentCaptor<FetchOptions> optionsCaptor = ArgumentCaptor.forClass(FetchOptions.class); |
| 175 | + ArgumentCaptor<Long> cnCaptor = ArgumentCaptor.forClass(Long.class); |
| 176 | + when(mockFetcher.fetch(Mockito.eq("someSegment"), cnCaptor.capture(), optionsCaptor.capture())).thenReturn(response1, response2); |
| 177 | + |
| 178 | + FetchOptions originalOptions = new FetchOptions.Builder().targetChangeNumber(123).build(); |
| 179 | + fetcher.fetch(originalOptions); |
| 180 | + List<Long> capturedCNs = cnCaptor.getAllValues(); |
| 181 | + List<FetchOptions> capturedOptions = optionsCaptor.getAllValues(); |
| 182 | + |
| 183 | + Assert.assertEquals(capturedOptions.size(), 2); |
| 184 | + Assert.assertEquals(capturedCNs.size(), 2); |
| 185 | + |
| 186 | + Assert.assertEquals(capturedCNs.get(0), Long.valueOf(-1)); |
| 187 | + Assert.assertEquals(capturedCNs.get(1), Long.valueOf(1)); |
| 188 | + |
| 189 | + Assert.assertEquals(capturedOptions.get(0).targetCN(), 123); |
| 190 | + Assert.assertEquals(capturedOptions.get(1).targetCN(), -1); |
| 191 | + |
| 192 | + // Ensure that the original value hasn't been modified |
| 193 | + Assert.assertEquals(originalOptions.targetCN(), 123); |
| 194 | + } |
| 195 | + |
141 | 196 | private SegmentChange getSegmentChange(long since, long till){
|
142 | 197 | SegmentChange segmentChange = new SegmentChange();
|
143 | 198 | segmentChange.name = SEGMENT_NAME;
|
|
0 commit comments