|
| 1 | +package io.flutter.dart; |
| 2 | + |
| 3 | +import com.intellij.openapi.project.Project; |
| 4 | +import com.intellij.testFramework.ServiceContainerUtil; |
| 5 | +import com.jetbrains.lang.dart.ide.toolingDaemon.DartToolingDaemonService; |
| 6 | +import io.flutter.testing.CodeInsightProjectFixture; |
| 7 | +import io.flutter.testing.Testing; |
| 8 | +import org.junit.After; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Rule; |
| 11 | +import org.junit.Test; |
| 12 | +import org.mockito.Mock; |
| 13 | +import org.mockito.MockitoAnnotations; |
| 14 | + |
| 15 | +import java.util.concurrent.CompletableFuture; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertTrue; |
| 18 | +import static org.mockito.Mockito.when; |
| 19 | + |
| 20 | +public class DtdUtilsTest { |
| 21 | + @Rule |
| 22 | + public final CodeInsightProjectFixture fixture = Testing.makeCodeInsightModule(); |
| 23 | + |
| 24 | + @Mock |
| 25 | + private DartToolingDaemonService mockDtdService; |
| 26 | + |
| 27 | + private Project project; |
| 28 | + |
| 29 | + @Before |
| 30 | + public void setUp() throws Exception { |
| 31 | + MockitoAnnotations.initMocks(this); |
| 32 | + project = fixture.getProject(); |
| 33 | + |
| 34 | + // Register mock service |
| 35 | + // We need to use the disposable from the fixture to ensure it gets cleaned up |
| 36 | + ServiceContainerUtil.registerServiceInstance(project, DartToolingDaemonService.class, mockDtdService); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testReadyDtdServiceRemovesProjectFromWaiters() throws Exception { |
| 41 | + when(mockDtdService.getWebSocketReady()).thenReturn(true); |
| 42 | + |
| 43 | + DtdUtils dtdUtils = new DtdUtils(); |
| 44 | + CompletableFuture<DartToolingDaemonService> future = dtdUtils.readyDtdService(project); |
| 45 | + |
| 46 | + assertTrue(future.isDone()); |
| 47 | + // Verify WAITERS map is empty |
| 48 | + assertTrue("WAITERS map should be empty after service is ready", DtdUtils.WAITERS.isEmpty()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testWaitersMapIsClearedAfterAsyncCompletion() throws Exception { |
| 53 | + when(mockDtdService.getWebSocketReady()).thenReturn(false); |
| 54 | + |
| 55 | + DtdUtils dtdUtils = new DtdUtils(); |
| 56 | + CompletableFuture<DartToolingDaemonService> future = dtdUtils.readyDtdService(project); |
| 57 | + |
| 58 | + // It should be in the map now |
| 59 | + assertTrue("Project should be in WAITERS map", DtdUtils.WAITERS.containsKey(project)); |
| 60 | + |
| 61 | + // Simulate service becoming ready |
| 62 | + when(mockDtdService.getWebSocketReady()).thenReturn(true); |
| 63 | + |
| 64 | + // Wait for the poller to pick it up (it runs every 500ms) |
| 65 | + // We can just wait on the future with a timeout |
| 66 | + future.get(5, java.util.concurrent.TimeUnit.SECONDS); |
| 67 | + |
| 68 | + assertTrue(future.isDone()); |
| 69 | + |
| 70 | + // Wait for the removal to happen (it happens in whenComplete, which might be slightly delayed if async) |
| 71 | + // We can poll the map |
| 72 | + long start = System.currentTimeMillis(); |
| 73 | + while (!DtdUtils.WAITERS.isEmpty() && System.currentTimeMillis() - start < 2000) { |
| 74 | + Thread.sleep(100); |
| 75 | + } |
| 76 | + |
| 77 | + assertTrue("WAITERS map should be empty after async completion", DtdUtils.WAITERS.isEmpty()); |
| 78 | + } |
| 79 | +} |
0 commit comments