aboutsummaryrefslogtreecommitdiff
path: root/server/test/Thermoprint
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-02-17 22:08:36 +0000
committerGregor Kleen <gkleen@yggdrasil.li>2016-02-17 22:08:36 +0000
commitb5b4b86427286002081f102d1e97baef9162851e (patch)
tree5d3eeef8abc884931756dd3f9711c355f4a88a4e /server/test/Thermoprint
parenteebc709302833932d7fca95dfc5c8536f8911e69 (diff)
downloadthermoprint-b5b4b86427286002081f102d1e97baef9162851e.tar
thermoprint-b5b4b86427286002081f102d1e97baef9162851e.tar.gz
thermoprint-b5b4b86427286002081f102d1e97baef9162851e.tar.bz2
thermoprint-b5b4b86427286002081f102d1e97baef9162851e.tar.xz
thermoprint-b5b4b86427286002081f102d1e97baef9162851e.zip
concurrency & dyre fixes for server spec
Diffstat (limited to 'server/test/Thermoprint')
-rw-r--r--server/test/Thermoprint/ServerSpec.hs43
1 files changed, 25 insertions, 18 deletions
diff --git a/server/test/Thermoprint/ServerSpec.hs b/server/test/Thermoprint/ServerSpec.hs
index 0d698f0..fe06a05 100644
--- a/server/test/Thermoprint/ServerSpec.hs
+++ b/server/test/Thermoprint/ServerSpec.hs
@@ -3,27 +3,29 @@
3 3
4module Thermoprint.ServerSpec (spec) where 4module Thermoprint.ServerSpec (spec) where
5 5
6import Test.Hspec 6import Test.Hspec
7 7
8import Thermoprint.API 8import Thermoprint.API
9import Thermoprint.Server 9import Thermoprint.Server
10 10
11import Control.Monad 11import Control.Monad
12import Control.Monad.Logger 12import Control.Monad.Logger
13import Control.Monad.Reader 13import Control.Monad.Reader
14import Control.Monad.Trans.Identity 14import Control.Monad.Trans.Identity
15import Control.Monad.Trans.Resource 15import Control.Monad.Trans.Resource
16 16
17import Database.Persist.Sqlite 17import Database.Persist.Sqlite
18 18
19import Control.Concurrent 19import Control.Concurrent
20import Control.Concurrent.STM 20import Control.Concurrent.STM
21 21
22import System.IO 22import System.IO
23import System.IO.Temp 23import System.IO.Temp
24 24
25import qualified Data.Text as T 25import qualified Data.Text as T
26 26
27import Debug.Trace
28
27data TestPrinter = TestPrinter 29data TestPrinter = TestPrinter
28 { outputChan :: TChan Printout 30 { outputChan :: TChan Printout
29 , failSwitch :: TMVar PrintingError 31 , failSwitch :: TMVar PrintingError
@@ -33,13 +35,14 @@ data TestManager = TestManager
33 { manage :: TMVar (QueueManager IdentityT) 35 { manage :: TMVar (QueueManager IdentityT)
34 } 36 }
35 37
36setup :: IO (ThreadId, TestPrinter, TestManager) 38setup :: IO (ThreadId, QSem, TestPrinter, TestManager)
37setup = withSystemTempFile "thermoprint.sqlite" $ \fp h -> hClose h >> do 39setup = withSystemTempFile "thermoprint.sqlite" $ \fp h -> hClose h >> do
38 tPrinter <- TestPrinter <$> newTChanIO <*> newEmptyTMVarIO 40 tPrinter <- TestPrinter <$> newTChanIO <*> newEmptyTMVarIO
39 tManager <- TestManager <$> newEmptyTMVarIO 41 tManager <- TestManager <$> newEmptyTMVarIO
42 termSem <- newQSem 0
40 let 43 let
41 runSqlite :: ReaderT ConnectionPool (LoggingT IO) a -> IO a 44 runSqlite :: ReaderT ConnectionPool (LoggingT IO) a -> IO a
42 runSqlite = runStderrLoggingT . withSqlitePool (T.pack fp) 1 . runReaderT 45 runSqlite = runNoLoggingT . withSqlitePool (T.pack fp) 1 . runReaderT
43 46
44 printers = [ ( pure $ PM tPM 47 printers = [ ( pure $ PM tPM
45 , QMConfig (join . lift $ takeTMVar (manage tManager)) (Nat $ liftIO . runIdentityT) 48 , QMConfig (join . lift $ takeTMVar (manage tManager)) (Nat $ liftIO . runIdentityT)
@@ -48,12 +51,16 @@ setup = withSystemTempFile "thermoprint.sqlite" $ \fp h -> hClose h >> do
48 51
49 tPM :: MonadIO m => Printout -> m (Maybe PrintingError) 52 tPM :: MonadIO m => Printout -> m (Maybe PrintingError)
50 tPM printout = liftIO . atomically $ writeTChan (outputChan tPrinter) printout >> tryTakeTMVar (failSwitch tPrinter) 53 tPM printout = liftIO . atomically $ writeTChan (outputChan tPrinter) printout >> tryTakeTMVar (failSwitch tPrinter)
51 (,,) <$> forkIO (thermoprintServer (Nat runSqlite) $ def `withPrinters` printers) <*> pure tPrinter <*> pure tManager 54 (,,,) <$> forkFinally (thermoprintServer False (Nat runSqlite) $ def `withPrinters` printers) (const $ signalQSem termSem) <*> pure termSem <*> pure tPrinter <*> pure tManager
55
56withSetup :: SpecWith (ThreadId, QSem, TestPrinter, TestManager) -> Spec
57withSetup = beforeAll setup . afterAll (\(tId, termSem, _, _) -> killThread tId >> waitQSem termSem)
52 58
53spec :: Spec 59spec :: Spec
54spec = beforeAll setup $ do 60spec = withSetup $ do
55 describe "blubTests" $ do 61 describe "blubTests" $ do
56 it "prints Blub." $ \(tId, _, _) -> do 62 it "prints Blub." $ \(tId, _, _, _) -> do
63 threadDelay 5000
57 putStrLn "Blub." 64 putStrLn "Blub."
58 System.IO.print tId 65 System.IO.print tId
59 True `shouldSatisfy` id 66 True `shouldSatisfy` id