Skip to content

Commit a0c4fd0

Browse files
Fixing PR comments
1 parent 9d8dd47 commit a0c4fd0

File tree

7 files changed

+69
-13
lines changed

7 files changed

+69
-13
lines changed

client/src/main/java/io/split/engine/common/SyncManagerImp.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public void start() {
104104
try {
105105
Thread.currentThread().sleep(1000);
106106
} catch (InterruptedException e) {
107+
_log.warn("Sdk Initializer thread interrupted");
107108
e.printStackTrace();
108109
Thread.currentThread().interrupt();
109110
}

client/src/main/java/io/split/engine/common/SynchronizerImp.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,7 @@ public SynchronizerImp(SplitSynchronizationTask splitSynchronizationTask,
5353

5454
@Override
5555
public boolean syncAll() {
56-
AtomicBoolean syncStatus = new AtomicBoolean(false);
57-
if(_splitFetcher.fetchAll(true) &&
58-
_segmentSynchronizationTaskImp.fetchAllSynchronous()) {
59-
syncStatus.set(true);
60-
}
61-
return syncStatus.get();
56+
return _splitFetcher.fetchAll(true) && _segmentSynchronizationTaskImp.fetchAllSynchronous();
6257
}
6358

6459
@Override

client/src/main/java/io/split/engine/experiments/SplitFetcherImp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public boolean fetchAll(boolean addCacheHeader) {
149149
long start = _splitCache.getChangeNumber();
150150
try {
151151
runWithoutExceptionHandling(addCacheHeader);
152+
return true;
152153
} catch (InterruptedException e) {
153154
_log.warn("Interrupting split fetcher task");
154155
Thread.currentThread().interrupt();
@@ -164,6 +165,5 @@ public boolean fetchAll(boolean addCacheHeader) {
164165
_log.debug("split fetch before: " + start + ", after: " + _splitCache.getChangeNumber());
165166
}
166167
}
167-
return true;
168168
}
169169
}

client/src/main/java/io/split/engine/segments/SegmentFetcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface SegmentFetcher {
99
*/
1010
void fetch(boolean addCacheHeader);
1111

12-
void runWhitCacheHeader();
12+
boolean runWhitCacheHeader();
1313

1414
void fetchAll();
1515
}

client/src/main/java/io/split/engine/segments/SegmentFetcherImp.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.split.engine.segments;
22

3+
import com.google.common.annotations.VisibleForTesting;
34
import io.split.cache.SegmentCache;
45
import io.split.client.dtos.SegmentChange;
56
import io.split.engine.SDKReadinessGates;
@@ -116,7 +117,8 @@ private String summarize(List<String> changes) {
116117
return bldr.toString();
117118
}
118119

119-
private void callLoopRun(boolean isFetch, boolean addCacheHeader){
120+
@VisibleForTesting
121+
void callLoopRun(boolean isFetch, boolean addCacheHeader){
120122
while (true) {
121123
long start = _segmentCache.getChangeNumber(_segmentName);
122124
runWithoutExceptionHandling(addCacheHeader);
@@ -131,23 +133,26 @@ private void callLoopRun(boolean isFetch, boolean addCacheHeader){
131133
}
132134

133135
@Override
134-
public void runWhitCacheHeader(){
135-
this.fetchAndUpdate(true);
136+
public boolean runWhitCacheHeader(){
137+
return this.fetchAndUpdate(true);
136138
}
137139

138140
/**
139141
* Calls callLoopRun and after fetchs segment.
140142
* @param addCacheHeader indicates if CacheHeader is required
141143
*/
142-
private void fetchAndUpdate(boolean addCacheHeader) {
144+
@VisibleForTesting
145+
boolean fetchAndUpdate(boolean addCacheHeader) {
143146
try {
144147
// Do this again in case the previous call errored out.
145148
callLoopRun(true, addCacheHeader);
149+
return true;
146150
} catch (Throwable t) {
147151
_log.error("RefreshableSegmentFetcher failed: " + t.getMessage());
148152
if (_log.isDebugEnabled()) {
149153
_log.debug("Reason:", t);
150154
}
155+
return false;
151156
}
152157
}
153158

client/src/main/java/io/split/engine/segments/SegmentSynchronizationTaskImp.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ public boolean fetchAllSynchronous() {
167167
.collect(Collectors.toList())
168168
.stream().forEach(future -> {
169169
try {
170-
future.get();
170+
if(!future.get()) {
171+
fetchAllStatus.set(false);
172+
};
171173
} catch (Exception ex) {
172174
fetchAllStatus.set(false);
173175
_log.error(ex.getMessage());

client/src/test/java/io/split/engine/segments/SegmentSynchronizationTaskImpTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package io.split.engine.segments;
22

3+
import com.google.common.collect.Maps;
34
import io.split.engine.SDKReadinessGates;
45
import io.split.cache.SegmentCache;
56
import io.split.telemetry.storage.InMemoryTelemetryStorage;
67
import io.split.telemetry.storage.TelemetryStorage;
8+
import org.junit.Assert;
79
import org.junit.Before;
810
import org.junit.Test;
911
import org.mockito.Mockito;
1012
import org.slf4j.Logger;
1113
import org.slf4j.LoggerFactory;
1214

15+
import java.lang.reflect.Field;
16+
import java.lang.reflect.Modifier;
1317
import java.util.List;
18+
import java.util.concurrent.ConcurrentMap;
1419
import java.util.concurrent.ExecutorService;
1520
import java.util.concurrent.Executors;
1621
import java.util.concurrent.TimeUnit;
@@ -79,5 +84,53 @@ public void run() {
7984
assertThat(fetcher1.get(), is(sameInstance(fetcher2.get())));
8085
}
8186

87+
@Test
88+
public void testFetchAllAsynchronousAndGetFalse() throws NoSuchFieldException, IllegalAccessException {
89+
SDKReadinessGates gates = new SDKReadinessGates();
90+
SegmentCache segmentCache = Mockito.mock(SegmentCache.class);
91+
ConcurrentMap<String, SegmentFetcher> _segmentFetchers = Maps.newConcurrentMap();
92+
93+
SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class);
94+
SegmentFetcherImp segmentFetcher = Mockito.mock(SegmentFetcherImp.class);
95+
_segmentFetchers.put("SF", segmentFetcher);
96+
final SegmentSynchronizationTaskImp fetchers = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1L, 1, gates, segmentCache, TELEMETRY_STORAGE);
97+
Mockito.doNothing().when(segmentFetcher).callLoopRun(Mockito.anyBoolean(),Mockito.anyBoolean());
98+
Mockito.when(segmentFetcher.runWhitCacheHeader()).thenReturn(false);
99+
Mockito.when(segmentFetcher.fetchAndUpdate(Mockito.anyBoolean())).thenReturn(false);
100+
Mockito.doNothing().when(segmentFetcher).callLoopRun(Mockito.anyBoolean(),Mockito.anyBoolean());
101+
102+
// Before executing, we'll update the map of segmentFecthers via reflection.
103+
Field backoffBase = SegmentSynchronizationTaskImp.class.getDeclaredField("_segmentFetchers");
104+
backoffBase.setAccessible(true);
105+
Field modifiersField = Field.class.getDeclaredField("modifiers");
106+
modifiersField.setAccessible(true);
107+
modifiersField.setInt(backoffBase, backoffBase.getModifiers() & ~Modifier.FINAL);
108+
109+
backoffBase.set(fetchers, _segmentFetchers); // 1ms
110+
fetcher1.set(segmentFetcher);
111+
boolean fetch = fetchers.fetchAllSynchronous();
112+
Assert.assertEquals(false, fetch);
113+
}
82114

115+
@Test
116+
public void testFetchAllAsynchronousAndGetTrue() throws NoSuchFieldException, IllegalAccessException {
117+
SDKReadinessGates gates = new SDKReadinessGates();
118+
SegmentCache segmentCache = Mockito.mock(SegmentCache.class);
119+
120+
SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class);
121+
SegmentFetcherImp segmentFetcher = Mockito.mock(SegmentFetcherImp.class);
122+
final SegmentSynchronizationTaskImp fetchers = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1L, 1, gates, segmentCache, TELEMETRY_STORAGE);
123+
124+
// Before executing, we'll update the map of segmentFecthers via reflection.
125+
Field backoffBase = SegmentSynchronizationTaskImp.class.getDeclaredField("_segmentFetchers");
126+
backoffBase.setAccessible(true);
127+
Field modifiersField = Field.class.getDeclaredField("modifiers");
128+
modifiersField.setAccessible(true);
129+
modifiersField.setInt(backoffBase, backoffBase.getModifiers() & ~Modifier.FINAL);
130+
Mockito.doNothing().when(segmentFetcher).callLoopRun(Mockito.anyBoolean(),Mockito.anyBoolean());
131+
Mockito.when(segmentFetcher.runWhitCacheHeader()).thenReturn(true);
132+
Mockito.when(segmentFetcher.fetchAndUpdate(Mockito.anyBoolean())).thenReturn(true);
133+
boolean fetch = fetchers.fetchAllSynchronous();
134+
Assert.assertEquals(true, fetch);
135+
}
83136
}

0 commit comments

Comments
 (0)