@@ -284,31 +284,26 @@ expectNoMoreDiagnostics timeout doc src = do
284
284
-- If timeout is 0 it will wait until the session timeout
285
285
waitForDiagnosticsFromSourceWithTimeout :: Seconds -> TextDocumentIdentifier -> String -> Test. Session [Diagnostic ]
286
286
waitForDiagnosticsFromSourceWithTimeout timeout document source = do
287
- when (timeout > 0 ) $
288
- -- Give any further diagnostic messages time to arrive.
289
- liftIO $ sleep timeout
290
- -- Send a dummy message to provoke a response from the server.
291
- -- This guarantees that we have at least one message to
292
- -- process, so message won't block or timeout.
293
- testId <- Test. sendRequest (SMethod_CustomMethod (Proxy @ " test" )) A. Null
294
- handleMessages testId
287
+ diags <- withTimeout timeout $ waitForDiagnosticsFrom document
288
+ return $ filter matches diags
295
289
where
296
290
matches :: Diagnostic -> Bool
297
291
matches d = d ^. L. source == Just (T. pack source)
298
292
299
- handleMessages testId = handleDiagnostic testId <|> handleMethod_CustomMethodResponse testId <|> ignoreOthers testId
300
- handleDiagnostic testId = do
301
- diagsNot <- Test. message SMethod_TextDocumentPublishDiagnostics
302
- let fileUri = diagsNot ^. L. params . L. uri
303
- diags = diagsNot ^. L. params . L. diagnostics
304
- res = filter matches diags
305
- if fileUri == document ^. L. uri && not (null res)
306
- then return res else handleMessages testId
307
- handleMethod_CustomMethodResponse testId = do
308
- _ <- Test. responseForId (SMethod_CustomMethod (Proxy @ " test" )) testId
309
- pure []
310
-
311
- ignoreOthers testId = void Test. anyMessage >> handleMessages testId
293
+ withTimeout :: Seconds -> Session a -> Session a
294
+ withTimeout duration f = do
295
+ chan <- asks messageChan
296
+ timeoutId <- getCurTimeoutId
297
+ modify $ \ s -> s { overridingTimeout = True }
298
+ tid <- liftIO $ forkIO $ do
299
+ threadDelay (ceiling $ duration * 1000000 )
300
+ writeChan chan (TimeoutMessage timeoutId)
301
+ res <- f
302
+ liftIO $ killThread tid
303
+ bumpTimeoutId timeoutId
304
+ modify $ \ s -> s { overridingTimeout = False }
305
+ return res
306
+
312
307
313
308
failIfSessionTimeout :: IO a -> IO a
314
309
failIfSessionTimeout action = action `catch` errorHandler
0 commit comments