Skip to content

Commit 1a03884

Browse files
committed
improve waitForDiagnosticsFromSourceWithTimeout
1 parent 5bee014 commit 1a03884

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

hls-test-utils/src/Test/Hls/Util.hs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -284,31 +284,26 @@ expectNoMoreDiagnostics timeout doc src = do
284284
-- If timeout is 0 it will wait until the session timeout
285285
waitForDiagnosticsFromSourceWithTimeout :: Seconds -> TextDocumentIdentifier -> String -> Test.Session [Diagnostic]
286286
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
295289
where
296290
matches :: Diagnostic -> Bool
297291
matches d = d ^. L.source == Just (T.pack source)
298292

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+
312307

313308
failIfSessionTimeout :: IO a -> IO a
314309
failIfSessionTimeout action = action `catch` errorHandler

0 commit comments

Comments
 (0)