Skip to content

Commit cd04ff8

Browse files
authored
Make a test more reliable (#3300)
* Make iface-error-test-1 more reliable The diagnostics can arrive either before or after progress completed, so there's a race condition here: if they arrive after, then the test script will get confused * make iface-error-test-1 more reliable Progress and diagnostics can arrive in any order: a test that waits for both is creating a race condition * added a comment * Fix single-threaded test execution * Fix another diagnostics/progress race condition in tests
1 parent 385dd1b commit cd04ff8

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

ghcide/test/exe/Main.hs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
-- Copyright (c) 2019 The DAML Authors. All rights reserved.
22
-- SPDX-License-Identifier: Apache-2.0
33

4+
{-
5+
NOTE On enforcing determinism
6+
7+
The tests below use two mechanisms to enforce deterministic LSP sequences:
8+
9+
1. Progress reporting: waitForProgress(Begin|Done)
10+
2. Diagnostics: expectDiagnostics
11+
12+
Either is fine, but diagnostics are generally more reliable.
13+
14+
Mixing them both in the same test is NOT FINE as it will introduce race
15+
conditions since multiple interleavings are possible. In other words,
16+
the sequence of diagnostics and progress reports is not deterministic.
17+
For example:
18+
19+
< do something >
20+
waitForProgressDone
21+
expectDiagnostics [...]
22+
23+
- When the diagnostics arrive after the progress done message, as they usually do, the test will pass
24+
- When the diagnostics arrive before the progress done msg, when on a slow machine occasionally, the test will timeout
25+
26+
Therefore, avoid mixing both progress reports and diagnostics in the same test
27+
-}
28+
429
{-# LANGUAGE AllowAmbiguousTypes #-}
530
{-# LANGUAGE CPP #-}
631
{-# LANGUAGE DataKinds #-}
@@ -2690,8 +2715,6 @@ ifaceErrorTest = testCase "iface-error-test-1" $ runWithExtraFiles "recomp" $ \d
26902715
expectDiagnostics
26912716
[("P.hs", [(DsWarning,(4,0), "Top-level binding")])] -- So what we know P has been loaded
26922717

2693-
waitForProgressDone
2694-
26952718
-- Change y from Int to B
26962719
changeDoc bdoc [TextDocumentContentChangeEvent Nothing Nothing $ T.unlines ["module B where", "y :: Bool", "y = undefined"]]
26972720
-- save so that we can that the error propagates to A
@@ -2702,14 +2725,15 @@ ifaceErrorTest = testCase "iface-error-test-1" $ runWithExtraFiles "recomp" $ \d
27022725
expectDiagnostics
27032726
[("A.hs", [(DsError, (5, 4), "Couldn't match expected type 'Int' with actual type 'Bool'")])]
27042727

2705-
27062728
-- Check that we wrote the interfaces for B when we saved
27072729
hidir <- getInterfaceFilesDir bdoc
27082730
hi_exists <- liftIO $ doesFileExist $ hidir </> "B.hi"
27092731
liftIO $ assertBool ("Couldn't find B.hi in " ++ hidir) hi_exists
27102732

27112733
pdoc <- openDoc pPath "haskell"
2712-
waitForProgressDone
2734+
expectDiagnostics
2735+
[("P.hs", [(DsWarning,(4,0), "Top-level binding")])
2736+
]
27132737
changeDoc pdoc [TextDocumentContentChangeEvent Nothing Nothing $ pSource <> "\nfoo = y :: Bool" ]
27142738
-- Now in P we have
27152739
-- bar = x :: Int

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ import Test.Tasty.ExpectedFailure
9797
import Test.Tasty.Golden
9898
import Test.Tasty.HUnit
9999
import Test.Tasty.Ingredients.Rerun
100+
import Test.Tasty.Runners (NumThreads (..))
100101

101102
newtype Log = LogIDEMain IDEMain.Log
102103

@@ -106,7 +107,7 @@ instance Pretty Log where
106107

107108
-- | Run 'defaultMainWithRerun', limiting each single test case running at most 10 minutes
108109
defaultTestRunner :: TestTree -> IO ()
109-
defaultTestRunner = defaultMainWithRerun . adjustOption (const $ mkTimeout 600000000)
110+
defaultTestRunner = defaultMainWithRerun . adjustOption (const $ NumThreads 1) . adjustOption (const $ mkTimeout 600000000)
110111

111112
gitDiff :: FilePath -> FilePath -> [String]
112113
gitDiff fRef fNew = ["git", "-c", "core.fileMode=false", "diff", "--no-index", "--text", "--exit-code", fRef, fNew]

plugins/hls-refactor-plugin/test/Main.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1843,7 +1843,6 @@ suggestImportDisambiguationTests = testGroup "suggest import disambiguation acti
18431843
auxFiles = ["AVec.hs", "BVec.hs", "CVec.hs", "DVec.hs", "EVec.hs", "FVec.hs"]
18441844
withTarget file locs k = runWithExtraFiles "hiding" $ \dir -> do
18451845
doc <- openDoc file "haskell"
1846-
waitForProgressDone
18471846
void $ expectDiagnostics [(file, [(DsError, loc, "Ambiguous occurrence") | loc <- locs])]
18481847
actions <- getAllCodeActions doc
18491848
k dir doc actions

0 commit comments

Comments
 (0)