-
Notifications
You must be signed in to change notification settings - Fork 38
fix: update to latest build tools. slight refactor of datafile service. #335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fe953f7
retool the datafile service.
thomaszurkan-optimizely 7665539
fix tests
thomaszurkan-optimizely 38dcfa4
update travis build tools
thomaszurkan-optimizely be4affd
change api as well as build tools
thomaszurkan-optimizely 030b70c
cleanup gradle files
thomaszurkan-optimizely 1672dd9
remove unused import
thomaszurkan-optimizely 31268df
refactor to not allow more than one datafile download within a minute
thomaszurkan-optimizely 0d98dbb
update to add another test for double download
thomaszurkan-optimizely a41cba4
added a test for when two downloads come in longer than minimum. cha…
thomaszurkan-optimizely 0a53320
add a unit test for threading in datafile download
thomaszurkan-optimizely bfff4a0
remove unused imports
thomaszurkan-optimizely File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -36,6 +36,8 @@ | |||||||
import org.slf4j.Logger; | ||||||||
|
||||||||
import java.io.IOException; | ||||||||
import java.lang.reflect.Field; | ||||||||
import java.lang.reflect.Modifier; | ||||||||
import java.net.MalformedURLException; | ||||||||
import java.util.concurrent.TimeUnit; | ||||||||
|
||||||||
|
@@ -45,8 +47,10 @@ | |||||||
import static junit.framework.Assert.fail; | ||||||||
import static org.mockito.Matchers.any; | ||||||||
import static org.mockito.Matchers.anyInt; | ||||||||
import static org.mockito.Mockito.atLeast; | ||||||||
import static org.mockito.Mockito.atMost; | ||||||||
import static org.mockito.Mockito.mock; | ||||||||
import static org.mockito.Mockito.never; | ||||||||
import static org.mockito.Mockito.verify; | ||||||||
import static org.mockito.Mockito.when; | ||||||||
|
||||||||
|
@@ -150,7 +154,7 @@ public void noCacheAndLoadFromCDNFails() { | |||||||
public void warningsAreLogged() throws IOException { | ||||||||
final ListeningExecutorService executor = MoreExecutors.newDirectExecutorService(); | ||||||||
Cache cache = mock(Cache.class); | ||||||||
datafileCache = new DatafileCache("1", cache, logger); | ||||||||
datafileCache = new DatafileCache("warningsAreLogged", cache, logger); | ||||||||
DatafileLoader datafileLoader = | ||||||||
new DatafileLoader(datafileService, datafileClient, datafileCache, executor, logger); | ||||||||
|
||||||||
|
@@ -159,7 +163,7 @@ public void warningsAreLogged() throws IOException { | |||||||
when(cache.delete(datafileCache.getFileName())).thenReturn(false); | ||||||||
when(cache.save(datafileCache.getFileName(), "{}")).thenReturn(false); | ||||||||
|
||||||||
datafileLoader.getDatafile("1", datafileLoadedListener); | ||||||||
datafileLoader.getDatafile("warningsAreLogged", datafileLoadedListener); | ||||||||
try { | ||||||||
executor.awaitTermination(5, TimeUnit.SECONDS); | ||||||||
} catch (InterruptedException e) { | ||||||||
|
@@ -170,4 +174,118 @@ public void warningsAreLogged() throws IOException { | |||||||
verify(logger).warn("Unable to save new datafile"); | ||||||||
verify(datafileLoadedListener, atMost(1)).onDatafileLoaded("{}"); | ||||||||
} | ||||||||
|
||||||||
@Test | ||||||||
public void debugLogged() throws IOException { | ||||||||
final ListeningExecutorService executor = MoreExecutors.newDirectExecutorService(); | ||||||||
Cache cache = mock(Cache.class); | ||||||||
datafileCache = new DatafileCache("debugLogged", cache, logger); | ||||||||
DatafileLoader datafileLoader = | ||||||||
new DatafileLoader(datafileService, datafileClient, datafileCache, executor, logger); | ||||||||
|
||||||||
when(client.execute(any(Client.Request.class), anyInt(), anyInt())).thenReturn("{}"); | ||||||||
when(cache.exists(datafileCache.getFileName())).thenReturn(true); | ||||||||
when(cache.delete(datafileCache.getFileName())).thenReturn(false); | ||||||||
when(cache.save(datafileCache.getFileName(), "{}")).thenReturn(false); | ||||||||
|
||||||||
datafileLoader.getDatafile("debugLogged", datafileLoadedListener); | ||||||||
datafileLoader.getDatafile("debugLogged", datafileLoadedListener); | ||||||||
try { | ||||||||
executor.awaitTermination(5, TimeUnit.SECONDS); | ||||||||
} catch (InterruptedException e) { | ||||||||
fail(); | ||||||||
} | ||||||||
|
||||||||
verify(logger).debug("Last download happened under 1 minute ago. Throttled to be at least 1 minute apart."); | ||||||||
verify(datafileLoadedListener, atMost(2)).onDatafileLoaded("{}"); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also check if it's called at least once
Suggested change
|
||||||||
verify(datafileLoadedListener, atLeast(1)).onDatafileLoaded("{}"); | ||||||||
} | ||||||||
|
||||||||
@Test | ||||||||
public void debugLoggedMultiThreaded() throws IOException { | ||||||||
final ListeningExecutorService executor = MoreExecutors.newDirectExecutorService(); | ||||||||
Cache cache = mock(Cache.class); | ||||||||
datafileCache = new DatafileCache("debugLoggedMultiThreaded", cache, logger); | ||||||||
DatafileLoader datafileLoader = | ||||||||
new DatafileLoader(datafileService, datafileClient, datafileCache, executor, logger); | ||||||||
|
||||||||
when(client.execute(any(Client.Request.class), anyInt(), anyInt())).thenReturn("{}"); | ||||||||
when(cache.exists(datafileCache.getFileName())).thenReturn(true); | ||||||||
when(cache.delete(datafileCache.getFileName())).thenReturn(true); | ||||||||
when(cache.load(datafileCache.getFileName())).thenReturn("{}"); | ||||||||
when(cache.save(datafileCache.getFileName(), "{}")).thenReturn(true); | ||||||||
|
||||||||
Runnable r = () -> datafileLoader.getDatafile("debugLoggedMultiThreaded", datafileLoadedListener); | ||||||||
|
||||||||
new Thread(r).start(); | ||||||||
new Thread(r).start(); | ||||||||
new Thread(r).start(); | ||||||||
new Thread(r).start(); | ||||||||
|
||||||||
datafileLoader.getDatafile("debugLoggedMultiThreaded", datafileLoadedListener); | ||||||||
|
||||||||
try { | ||||||||
executor.awaitTermination(5, TimeUnit.SECONDS); | ||||||||
} catch (InterruptedException e) { | ||||||||
fail(); | ||||||||
} | ||||||||
|
||||||||
verify(logger, atLeast(1)).debug("Last download happened under 1 minute ago. Throttled to be at least 1 minute apart."); | ||||||||
verify(datafileLoadedListener, atMost(4)).onDatafileLoaded("{}"); | ||||||||
verify(datafileLoadedListener, atLeast(1)).onDatafileLoaded("{}"); | ||||||||
} | ||||||||
|
||||||||
|
||||||||
private void setTestDownloadFrequency(DatafileLoader datafileLoader, long value) { | ||||||||
try { | ||||||||
Field betweenDownloadsMilli = DatafileLoader.class.getDeclaredField("minTimeBetweenDownloadsMilli"); | ||||||||
betweenDownloadsMilli.setAccessible(true); | ||||||||
|
||||||||
//Field modifiersField; | ||||||||
//modifiersField = Field.class.getDeclaredField("modifiers"); | ||||||||
//modifiersField.setAccessible(true); | ||||||||
//modifiersField.setInt(betweenDownloadsMilli, betweenDownloadsMilli.getModifiers() & ~Modifier.FINAL); | ||||||||
betweenDownloadsMilli.set(null, value); | ||||||||
|
||||||||
} catch (NoSuchFieldException e) { | ||||||||
e.printStackTrace(); | ||||||||
} | ||||||||
catch (IllegalAccessException e) { | ||||||||
e.printStackTrace(); | ||||||||
} | ||||||||
|
||||||||
} | ||||||||
|
||||||||
@Test | ||||||||
public void allowDoubleDownload() throws IOException { | ||||||||
final ListeningExecutorService executor = MoreExecutors.newDirectExecutorService(); | ||||||||
Cache cache = mock(Cache.class); | ||||||||
datafileCache = new DatafileCache("allowDoubleDownload", cache, logger); | ||||||||
DatafileLoader datafileLoader = | ||||||||
new DatafileLoader(datafileService, datafileClient, datafileCache, executor, logger); | ||||||||
|
||||||||
// set download time to 1 second | ||||||||
setTestDownloadFrequency(datafileLoader, 1000L); | ||||||||
|
||||||||
when(client.execute(any(Client.Request.class), anyInt(), anyInt())).thenReturn("{}"); | ||||||||
when(cache.exists(datafileCache.getFileName())).thenReturn(true); | ||||||||
when(cache.delete(datafileCache.getFileName())).thenReturn(false); | ||||||||
when(cache.save(datafileCache.getFileName(), "{}")).thenReturn(false); | ||||||||
|
||||||||
datafileLoader.getDatafile("allowDoubleDownload", datafileLoadedListener); | ||||||||
try { | ||||||||
executor.awaitTermination(2, TimeUnit.SECONDS); | ||||||||
} catch (InterruptedException e) { | ||||||||
fail(); | ||||||||
} | ||||||||
|
||||||||
datafileLoader.getDatafile("allowDoubleDownload", datafileLoadedListener); | ||||||||
|
||||||||
// reset back to normal. | ||||||||
setTestDownloadFrequency(datafileLoader, 60 * 1000L); | ||||||||
|
||||||||
verify(logger, never()).debug("Last download happened under 1 minute ago. Throttled to be at least 1 minute apart."); | ||||||||
verify(datafileLoadedListener, atMost(2)).onDatafileLoaded("{}"); | ||||||||
verify(datafileLoadedListener, atLeast(1)).onDatafileLoaded("{}"); | ||||||||
} | ||||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add one more test to check if back-to-back download works fine for both requests when they're apart 61secs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to add another an extra minute to the tests to prove this point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep it's not great to add a minute wait time for testing. But this test must be important to make sure we do not freeze polling permanently by error. We can change it to inject the required gap time so can test with a reasonable wait time of a few seconds.
I'm ok to release it as is for now and then add the extra test with the next coming release.