|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +{-# OPTIONS_GHC -Wno-orphans #-} |
| 3 | + |
| 4 | +module ShakeRestartTests (tests) where |
| 5 | + |
| 6 | +import Control.Concurrent.STM |
| 7 | +import Data.IORef |
| 8 | +import Data.IORef.Extra (atomicModifyIORef'_) |
| 9 | +import Development.IDE.Core.Shake |
| 10 | +import Development.IDE.Graph (newKey) |
| 11 | +import Language.LSP.VFS |
| 12 | +import Test.Tasty |
| 13 | +import Test.Tasty.HUnit |
| 14 | + |
| 15 | +tests :: TestTree |
| 16 | +tests = testGroup "shake restart merging" |
| 17 | + [ testCase "newestVFSModified" $ do |
| 18 | + let vfs1 = VFSModified (VFS mempty) |
| 19 | + newestVFSModified VFSUnmodified VFSUnmodified @?= VFSUnmodified |
| 20 | + newestVFSModified vfs1 VFSUnmodified @?= vfs1 |
| 21 | + newestVFSModified VFSUnmodified vfs1 @?= vfs1 |
| 22 | + |
| 23 | + , testCase "mergePendingRestart Nothing" $ do |
| 24 | + let p = PendingRestart VFSUnmodified (pure []) ["reason"] [] [] |
| 25 | + if mergePendingRestart p Nothing == p |
| 26 | + then pure () |
| 27 | + else assertFailure "merging with nothing should get new" |
| 28 | + |
| 29 | + , testCase "mergePendingRestart Just" $ do |
| 30 | + done1 <- newEmptyTMVarIO |
| 31 | + done2 <- newEmptyTMVarIO |
| 32 | + let key1 = newKey ("1" :: String) |
| 33 | + key2 = newKey ("2" :: String) |
| 34 | + p1 = PendingRestart VFSUnmodified (pure [key1]) ["r1"] [] [done1] |
| 35 | + p2 = PendingRestart VFSUnmodified (pure [key2]) ["r2"] [] [done2] |
| 36 | + merged = mergePendingRestart p1 (Just p2) |
| 37 | + |
| 38 | + pendingRestartReasons merged @?= ["r1", "r2"] |
| 39 | + keys <- pendingRestartActionBetweenSessions merged |
| 40 | + keys @?= [key2, key1] |
| 41 | + |
| 42 | + , testCase "RestartSlot coalescing" $ do |
| 43 | + slot <- newRestartSlot |
| 44 | + let p1 = PendingRestart VFSUnmodified (pure []) ["r1"] [] [] |
| 45 | + p2 = PendingRestart VFSUnmodified (pure []) ["r2"] [] [] |
| 46 | + |
| 47 | + atomicModifyIORef'_ (queuedRestart slot) $ Just . mergePendingRestart p1 |
| 48 | + atomicModifyIORef'_ (queuedRestart slot) $ Just . mergePendingRestart p2 |
| 49 | + |
| 50 | + res <- atomicModifyIORef' (queuedRestart slot) (Nothing,) |
| 51 | + case res of |
| 52 | + Nothing -> assertFailure "Should have a pending restart" |
| 53 | + Just p -> pendingRestartReasons p @?= ["r2", "r1"] |
| 54 | + ] |
| 55 | + |
| 56 | +instance Eq VFSModified where |
| 57 | + VFSUnmodified == VFSUnmodified = True |
| 58 | + VFSModified (VFS _) == VFSModified (VFS _) = True |
| 59 | + _ == _ = False |
| 60 | + |
| 61 | +instance Eq PendingRestart where |
| 62 | + p1 == p2 = pendingRestartVFS p1 == pendingRestartVFS p2 && |
| 63 | + pendingRestartReasons p1 == pendingRestartReasons p2 |
0 commit comments