|
4 | 4 | import static junit.framework.TestCase.assertTrue;
|
5 | 5 |
|
6 | 6 | import static org.mockito.Matchers.any;
|
| 7 | +import static org.mockito.Mockito.doAnswer; |
7 | 8 | import static org.mockito.Mockito.mock;
|
8 | 9 | import static org.mockito.Mockito.mockStatic;
|
9 | 10 | import static org.mockito.Mockito.spy;
|
|
34 | 35 | import java.util.concurrent.CountDownLatch;
|
35 | 36 | import java.util.concurrent.Executors;
|
36 | 37 | import java.util.concurrent.ScheduledExecutorService;
|
| 38 | +import java.util.concurrent.atomic.AtomicBoolean; |
37 | 39 |
|
38 | 40 |
|
39 | 41 | public class RNInstabugSessionReplayModuleTest {
|
@@ -141,41 +143,44 @@ public void testSetSyncCallback() throws Exception {
|
141 | 143 | MockedStatic mockArgument = mockStatic(Arguments.class);
|
142 | 144 | RNInstabugSessionReplayModule SRModule = spy(new RNInstabugSessionReplayModule(mock(ReactApplicationContext.class)));
|
143 | 145 |
|
144 |
| - CountDownLatch latch =new CountDownLatch(1); |
145 |
| - SRModule.latch=latch; |
| 146 | + AtomicBoolean result = new AtomicBoolean(false); |
| 147 | + boolean shouldSync=true; |
146 | 148 |
|
147 | 149 | when(Arguments.createMap()).thenReturn(new JavaOnlyMap());
|
148 | 150 |
|
149 | 151 | mockSessionReplay.when(() -> SessionReplay.setSyncCallback(any(SessionSyncListener.class)))
|
150 | 152 | .thenAnswer(new Answer<Void>() {
|
151 | 153 | @Override
|
152 | 154 | public Void answer(InvocationOnMock invocation) {
|
153 |
| - ((SessionSyncListener) invocation.getArguments()[0]).onSessionReadyToSync(new SessionMetadata("device","android","1.0",20)); |
| 155 | + SessionSyncListener listener = (SessionSyncListener) invocation.getArguments()[0]; |
| 156 | + SessionMetadata metadata = new SessionMetadata("device", "android", "1.0", 20); |
| 157 | + boolean shouldSync=listener.onSessionReadyToSync(metadata); |
| 158 | + result.set(shouldSync); |
154 | 159 | return null;
|
155 | 160 | }
|
156 | 161 | });
|
157 | 162 |
|
| 163 | + WritableMap params = Arguments.createMap(); |
| 164 | + params.putString("appVersion","1.0"); |
| 165 | + params.putString("OS","android"); |
| 166 | + params.putString("device","device"); |
| 167 | + params.putDouble("sessionDurationInSeconds",20); |
| 168 | + |
158 | 169 | Thread thread= new Thread (() ->{
|
159 | 170 | try {
|
160 | 171 | Thread.sleep(500);
|
161 | 172 | } catch (InterruptedException e) {
|
162 | 173 | throw new RuntimeException(e);
|
163 | 174 | }
|
164 |
| - SRModule.evaluateSync(true); |
| 175 | + |
| 176 | + SRModule.evaluateSync(shouldSync); |
165 | 177 | });
|
| 178 | + |
166 | 179 | thread.start();
|
167 | 180 |
|
168 | 181 | SRModule.setSyncCallback();
|
169 | 182 |
|
170 |
| - WritableMap params = Arguments.createMap(); |
171 |
| - params.putString("appVersion","1.0"); |
172 |
| - params.putString("OS","android"); |
173 |
| - params.putString("device","device"); |
174 |
| - params.putDouble("sessionDurationInSeconds",20); |
175 |
| - |
176 |
| - assertEquals(SRModule.shouldSync,true); |
177 |
| - assertTrue("Latch should be zero after evaluateSync is called", SRModule.latch.getCount() == 0); |
178 |
| - |
| 183 | + assertEquals(shouldSync,result.get()); |
179 | 184 | verify(SRModule).sendEvent(Constants.IBG_SESSION_REPLAY_ON_SYNC_CALLBACK_INVOCATION, params);
|
180 | 185 | mockSessionReplay.verify(() -> SessionReplay.setSyncCallback(any(SessionSyncListener.class)));
|
181 | 186 |
|
|
0 commit comments