-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventQueueManagerTest.java
More file actions
69 lines (57 loc) · 3.04 KB
/
EventQueueManagerTest.java
File metadata and controls
69 lines (57 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.devcycle.sdk.server.local;
import com.devcycle.sdk.server.common.api.IDevCycleApi;
import com.devcycle.sdk.server.common.model.DevCycleResponse;
import com.devcycle.sdk.server.helpers.WhiteBox;
import com.devcycle.sdk.server.local.bucketing.LocalBucketing;
import com.devcycle.sdk.server.local.managers.EventQueueManager;
import com.devcycle.sdk.server.local.model.DevCycleLocalOptions;
import com.devcycle.sdk.server.local.model.FlushPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import retrofit2.Call;
import java.lang.reflect.Field;
import static org.junit.Assert.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
@RunWith(MockitoJUnitRunner.class)
public class EventQueueManagerTest {
/**
* Verifies that isFlushingEvents is reset to false even when the publish loop throws.
* Without the try/finally fix, an exception escaping publishEvents() would leave
* isFlushingEvents stuck true, silently dropping all subsequent flushes.
*/
@Test
@SuppressWarnings("unchecked")
public void flushEvents_resetsIsFlushingEventsOnException() throws Exception {
LocalBucketing mockBucketing = Mockito.mock(LocalBucketing.class);
// Large interval so the background scheduler doesn't fire during the test
DevCycleLocalOptions options = DevCycleLocalOptions.builder()
.eventFlushIntervalMS(Integer.MAX_VALUE)
.build();
// Return empty payloads by default so any early scheduler tick is a no-op
Mockito.when(mockBucketing.flushEventQueue(anyString())).thenReturn(new FlushPayload[0]);
EventQueueManager manager = new EventQueueManager("server-key", mockBucketing, "test-uuid", options);
// Swap in a mock API whose Call.execute() throws to simulate a publish failure
IDevCycleApi mockApi = Mockito.mock(IDevCycleApi.class);
Call<DevCycleResponse> mockCall = Mockito.mock(Call.class);
Mockito.when(mockApi.publishEvents(any())).thenReturn(mockCall);
Mockito.when(mockCall.execute()).thenThrow(new RuntimeException("simulated publish failure"));
WhiteBox.setInternalState(manager, "eventsApiClient", mockApi);
// Return a non-empty payload so flushEvents() reaches the publish loop
FlushPayload payload = new FlushPayload();
payload.payloadId = "test-payload-1";
payload.eventCount = 1;
payload.records = new FlushPayload.Record[0];
Mockito.when(mockBucketing.flushEventQueue(anyString())).thenReturn(new FlushPayload[]{payload});
try {
manager.flushEvents();
} catch (RuntimeException e) {
// expected — the exception should escape flushEvents()
}
Field field = EventQueueManager.class.getDeclaredField("isFlushingEvents");
field.setAccessible(true);
assertFalse("isFlushingEvents must be reset to false after exception", (boolean) field.get(manager));
}
}