Skip to content

Commit

Permalink
system-tests: Add test for RequestResume
Browse files Browse the repository at this point in the history
  • Loading branch information
supersven committed Apr 12, 2020
1 parent a188a19 commit c90d926
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
13 changes: 12 additions & 1 deletion test/ghc-debugger.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ test-suite system-test
, Server
default-language: Haskell2010
ghc-options: -threaded -debug -g3
build-tool-depends: ghc-debug-stub:debug-test, ghc-debugger:save-one
build-tool-depends: ghc-debug-stub:debug-test
, ghc-debugger:save-one
, ghc-debugger:clock

executable save-one
main-is: SaveOne.hs
Expand All @@ -68,3 +70,12 @@ executable save-one
, ghc-debug-stub
default-language: Haskell2010

executable clock
main-is: Clock.hs
hs-source-dirs:
test-progs
ghc-options: -threaded -debug -g3 -O0
build-depends: base
, ghc-debug-stub
default-language: Haskell2010

24 changes: 24 additions & 0 deletions test/test-progs/Clock.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import GHC.Debug.Stub
import System.Mem
import Control.Concurrent
import System.IO
import Data.Word
import GHC.Stats
import GHC.Clock

loop :: IO ()
loop = do
time <- getMonotonicTimeNSec
print time
hFlush stdout
threadDelay oneSecond
loop
where
oneSecond = 1000000

main :: IO ()
main = do
start
print "sync"
hFlush stdout
loop
57 changes: 57 additions & 0 deletions test/test/SystemTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ import Control.Monad

import Control.Concurrent.Async
import Control.Concurrent
import Control.Monad.Extra

import Data.Word
import Data.IORef
import GHC.Clock
import System.Timeout
import Data.List.Extra

-- TODO use timeout for tests
spec :: SpecWith ()
spec = do
describe "debuggeeDwarf" $
Expand Down Expand Up @@ -54,6 +62,44 @@ spec = do
hg <- buildHeapGraph (derefBox d) 20 () o
ppHeapGraph hg `shouldBe` "I# 1"

describe "RequestResume" $
it "should resume a paused debugee" $
withStartedDebuggeeAndHandles "clock" $ \ h d -> do
waitForSync $ Server.stdout h
ref <- newIORef []
withAsync (pipeStreamToListThread ref (Server.stdout h)) $ \_ -> do
request d RequestPause
(t:_) <- readIORef ref
assertNoNewClockTimes ref t

request d RequestResume

assertNewClockTime ref
where
oneSecondInMicros = 1000000
fiveSecondsInMicros = 5000000

assertNoNewClockTimes :: IORef [ClockTime] -> ClockTime -> Expectation
assertNoNewClockTimes ref t0 = do
result <- timeout fiveSecondsInMicros $ whileM $ do
threadDelay oneSecondInMicros
(t1:_) <- readIORef ref
return $ t0 == t1

result `shouldBe` Nothing

assertNewClockTime :: IORef [ClockTime] -> Expectation
assertNewClockTime ref = do
now <- getMonotonicTimeNSec
print $ "now2 : " ++ show now
result <- timeout fiveSecondsInMicros $ whileM $ do
threadDelay 5000
(t:_) <- readIORef ref
return $ t < now

result `shouldBe` Just ()


waitForSync :: Handle -> IO ()
waitForSync h = do
hSetBuffering h LineBuffering
Expand All @@ -71,6 +117,17 @@ pipeStreamThread h = forever $ do
l <- hGetLine h
print l

type ClockTime = Word64

pipeStreamToListThread :: IORef [ClockTime] -> Handle -> IO ()
pipeStreamToListThread ref h = forever $ do
l <- hGetLine h
timesList <- readIORef ref
writeIORef ref $ (toClockTime l) : timesList
where
toClockTime :: String -> ClockTime
toClockTime s = read . trim $ s

shouldContainCuName :: Dwarf -> String -> Expectation
shouldContainCuName dwarf name = allCuNames `shouldContain` [name]
where
Expand Down

0 comments on commit c90d926

Please sign in to comment.