Repository navigation
fix: Restore flag analytics after failed delivery #261
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
Open
pcbeingused33
wants to merge
1
commit into
Flagsmith:main
Choose a base branch
from
pcbeingused33:fix/restore-failed-analytics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,25 @@ | |
| import com.flagsmith.config.FlagsmithConfig; | ||
| import com.flagsmith.config.Retry; | ||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.concurrent.BlockingQueue; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.LinkedBlockingQueue; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import lombok.SneakyThrows; | ||
| import okhttp3.Protocol; | ||
| import okhttp3.RequestBody; | ||
| import okhttp3.Response; | ||
| import okhttp3.ResponseBody; | ||
| import okio.Buffer; | ||
| import okhttp3.mock.MockInterceptor; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
|
|
||
| public class AnalyticsProcessorTest { | ||
|
|
||
|
|
@@ -47,6 +59,8 @@ public void init() { | |
| .build(); | ||
| api = mock(FlagsmithApiWrapper.class); | ||
| requestProcessor = mock(RequestProcessor.class); | ||
| when(requestProcessor.submitForStatus(any())) | ||
| .thenReturn(CompletableFuture.completedFuture(true)); | ||
| when(api.getConfig()).thenReturn(defaultConfig); | ||
| analytics = new AnalyticsProcessor(api, flagsmithLogger, requestProcessor); | ||
| } | ||
|
|
@@ -75,7 +89,7 @@ public void AnalyticsProcessor_checkAnalyticsDataCheckFlushRuns() throws Interru | |
| Assertions.assertEquals(analytics.getAnalyticsData().size(), 0); | ||
| Assertions.assertNotEquals(nextFlush, analytics.getNextFlush()); | ||
| verify(api, times(1)).newPostRequest(any(), any()); | ||
| verify(requestProcessor, times(1)).executeAsync(any(), any()); | ||
| verify(requestProcessor, times(1)).submitForStatus(any()); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -88,7 +102,63 @@ public void AnalyticsProcessor_checkAnalyticsRequestCheckFlushRuns() { | |
|
|
||
| analytics.flush(); | ||
| verify(api, times(1)).newPostRequest(any(), any()); | ||
| verify(requestProcessor, times(1)).executeAsync(any(), any()); | ||
| verify(requestProcessor, times(1)).submitForStatus(any()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFlush_RestoresCountsAfterRetriesAreExhausted() throws Exception { | ||
| BlockingQueue<String> bodies = new LinkedBlockingQueue<>(); | ||
| AtomicInteger attempts = new AtomicInteger(); | ||
| FlagsmithConfig config = FlagsmithConfig.newBuilder().baseUri("http://base-uri/") | ||
| .addHttpInterceptor(chain -> { | ||
| Buffer body = new Buffer(); | ||
| chain.request().body().writeTo(body); | ||
| bodies.add(body.readUtf8()); | ||
| int status = attempts.incrementAndGet() <= 2 ? 503 : 200; | ||
|
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. P3: The two 503 responses call Prompt for AI agents |
||
| return new Response.Builder().request(chain.request()).protocol(Protocol.HTTP_1_1) | ||
| .code(status).message("").body(ResponseBody.create("", null)).build(); | ||
| }).build(); | ||
| Retry retry = new Retry(2, 0, 0f, 15f, Collections.singleton(503), true); | ||
| RequestProcessor processor = new RequestProcessor(config.getHttpClient(), flagsmithLogger, | ||
| retry); | ||
| FlagsmithApiWrapper wrapper = | ||
| new FlagsmithApiWrapper(config, new HashMap<>(), flagsmithLogger, "test-key", processor); | ||
| AnalyticsProcessor tracker = new AnalyticsProcessor(wrapper, flagsmithLogger, processor); | ||
|
|
||
| try { | ||
| tracker.trackFeature("foo"); | ||
| tracker.trackFeature("foo"); | ||
| tracker.flush(); | ||
| Assertions.assertEquals("{\"foo\":2}", bodies.poll(5, TimeUnit.SECONDS)); | ||
| Assertions.assertEquals("{\"foo\":2}", bodies.poll(5, TimeUnit.SECONDS)); | ||
|
|
||
| long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(2); | ||
| while (!tracker.getAnalyticsData().containsKey("foo") && System.nanoTime() < deadline) { | ||
| Thread.sleep(10); | ||
| } | ||
| tracker.trackFeature("foo"); | ||
| tracker.flush(); | ||
| Assertions.assertEquals("{\"foo\":3}", bodies.poll(5, TimeUnit.SECONDS)); | ||
| } finally { | ||
| tracker.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testFlush_DoesNotResendSuccessfulCounts() throws IOException { | ||
| analytics.trackFeature("foo"); | ||
| analytics.trackFeature("foo"); | ||
| analytics.flush(); | ||
| analytics.trackFeature("foo"); | ||
| analytics.flush(); | ||
|
|
||
| ArgumentCaptor<RequestBody> bodies = ArgumentCaptor.forClass(RequestBody.class); | ||
| verify(api, times(2)).newPostRequest(any(), bodies.capture()); | ||
| Buffer body = new Buffer(); | ||
| bodies.getAllValues().get(0).writeTo(body); | ||
| Assertions.assertEquals("{\"foo\":2}", body.readUtf8()); | ||
| bodies.getAllValues().get(1).writeTo(body); | ||
| Assertions.assertEquals("{\"foo\":1}", body.readUtf8()); | ||
| } | ||
|
|
||
| @Test | ||
|
|
||
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.
P3: These new tests omit the required
// Given,// When, and// Thensections; please add the markers to match TE03.Prompt for AI agents