aboutsummaryrefslogtreecommitdiff
path: root/server/test/Thermoprint/ServerSpec.hs
diff options
context:
space:
mode:
Diffstat (limited to 'server/test/Thermoprint/ServerSpec.hs')
-rw-r--r--server/test/Thermoprint/ServerSpec.hs23
1 files changed, 18 insertions, 5 deletions
diff --git a/server/test/Thermoprint/ServerSpec.hs b/server/test/Thermoprint/ServerSpec.hs
index deb2b9c..028ba2d 100644
--- a/server/test/Thermoprint/ServerSpec.hs
+++ b/server/test/Thermoprint/ServerSpec.hs
@@ -31,6 +31,10 @@ import Database.Persist.Sqlite
31import Control.Concurrent 31import Control.Concurrent
32import Control.Concurrent.STM 32import Control.Concurrent.STM
33import Control.Concurrent.STM.TSem 33import Control.Concurrent.STM.TSem
34import Control.Concurrent.Async
35
36import Control.Exception
37import System.IO.Error
34 38
35import System.IO 39import System.IO
36import System.IO.Temp 40import System.IO.Temp
@@ -63,7 +67,8 @@ data TestManager = TestManager
63 67
64data RunningServer = RunningServer 68data RunningServer = RunningServer
65 { thread :: ThreadId 69 { thread :: ThreadId
66 , termination :: QSem 70 , termination :: MVar (Either SomeException ())
71 , startup :: QSem
67 , printer :: TestPrinter 72 , printer :: TestPrinter
68 , manager :: TestManager 73 , manager :: TestManager
69 } 74 }
@@ -72,7 +77,8 @@ setup :: QSem -> IO RunningServer
72setup startup = withSystemTempFile "thermoprint.sqlite" $ \fp h -> hClose h >> do 77setup startup = withSystemTempFile "thermoprint.sqlite" $ \fp h -> hClose h >> do
73 tPrinter <- TestPrinter <$> newEmptyTMVarIO <*> newEmptyTMVarIO 78 tPrinter <- TestPrinter <$> newEmptyTMVarIO <*> newEmptyTMVarIO
74 tManager <- TestManager <$> newEmptyTMVarIO <*> atomically (newTSem 0) 79 tManager <- TestManager <$> newEmptyTMVarIO <*> atomically (newTSem 0)
75 termSem <- newQSem 0 80 term <- newEmptyMVar
81 startSem <- newQSem 0
76 let 82 let
77 runSqlite :: ReaderT ConnectionPool (LoggingT IO) a -> IO a 83 runSqlite :: ReaderT ConnectionPool (LoggingT IO) a -> IO a
78 runSqlite = runStderrLoggingT . filterLogger (const (> LevelDebug)) . withSqlitePool (T.pack fp) 1 . runReaderT 84 runSqlite = runStderrLoggingT . filterLogger (const (> LevelDebug)) . withSqlitePool (T.pack fp) 1 . runReaderT
@@ -84,7 +90,7 @@ setup startup = withSystemTempFile "thermoprint.sqlite" $ \fp h -> hClose h >> d
84 90
85 tPM :: MonadIO m => Printout -> m (Maybe PrintingError) 91 tPM :: MonadIO m => Printout -> m (Maybe PrintingError)
86 tPM printout = liftIO . atomically $ putTMVar (outputChan tPrinter) printout >> tryTakeTMVar (failSwitch tPrinter) 92 tPM printout = liftIO . atomically $ putTMVar (outputChan tPrinter) printout >> tryTakeTMVar (failSwitch tPrinter)
87 RunningServer <$> forkFinally (S.thermoprintServer False (Nat runSqlite) $ def' `S.withPrinters` printers) (const $ signalQSem termSem) <*> pure termSem <*> pure tPrinter <*> pure tManager 93 RunningServer <$> forkFinally (S.thermoprintServer False (Nat runSqlite) $ def' `S.withPrinters` printers) (putMVar term) <*> pure term <*> pure startSem <*> pure tPrinter <*> pure tManager
88 where 94 where
89 def' :: MonadIO m => S.Config m 95 def' :: MonadIO m => S.Config m
90 def' = S.def { S.warpSettings = setBeforeMainLoop (signalQSem startup) $ defaultSettings } 96 def' = S.def { S.warpSettings = setBeforeMainLoop (signalQSem startup) $ defaultSettings }
@@ -94,8 +100,15 @@ withSetup = beforeAll setup' . afterAll teardown
94 where 100 where
95 setup' = do 101 setup' = do
96 startup <- newQSem 0 102 startup <- newQSem 0
97 setup startup <* waitQSem startup 103 r <- setup startup
98 teardown RunningServer{..} = killThread thread >> waitQSem termination 104 waitStartup <- async $ waitQSem startup
105 waitTermination <- async . readMVar $ termination r
106 wait <- waitEitherCancel waitTermination waitStartup
107 case wait of
108 Left (Left err) -> throwIO err
109 Left (Right _) -> throwIO $ userError "Server thread terminated early"
110 Right _ -> return r
111 teardown RunningServer{..} = killThread thread >> void (readMVar termination)
99 112
100spec :: Spec 113spec :: Spec
101spec = withSetup $ do 114spec = withSetup $ do