diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-17 22:08:36 +0000 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-17 22:08:36 +0000 |
| commit | b5b4b86427286002081f102d1e97baef9162851e (patch) | |
| tree | 5d3eeef8abc884931756dd3f9711c355f4a88a4e /server/test | |
| parent | eebc709302833932d7fca95dfc5c8536f8911e69 (diff) | |
| download | thermoprint-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')
| -rw-r--r-- | server/test/Thermoprint/ServerSpec.hs | 43 |
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 | ||
| 4 | module Thermoprint.ServerSpec (spec) where | 4 | module Thermoprint.ServerSpec (spec) where |
| 5 | 5 | ||
| 6 | import Test.Hspec | 6 | import Test.Hspec |
| 7 | 7 | ||
| 8 | import Thermoprint.API | 8 | import Thermoprint.API |
| 9 | import Thermoprint.Server | 9 | import Thermoprint.Server |
| 10 | 10 | ||
| 11 | import Control.Monad | 11 | import Control.Monad |
| 12 | import Control.Monad.Logger | 12 | import Control.Monad.Logger |
| 13 | import Control.Monad.Reader | 13 | import Control.Monad.Reader |
| 14 | import Control.Monad.Trans.Identity | 14 | import Control.Monad.Trans.Identity |
| 15 | import Control.Monad.Trans.Resource | 15 | import Control.Monad.Trans.Resource |
| 16 | 16 | ||
| 17 | import Database.Persist.Sqlite | 17 | import Database.Persist.Sqlite |
| 18 | 18 | ||
| 19 | import Control.Concurrent | 19 | import Control.Concurrent |
| 20 | import Control.Concurrent.STM | 20 | import Control.Concurrent.STM |
| 21 | 21 | ||
| 22 | import System.IO | 22 | import System.IO |
| 23 | import System.IO.Temp | 23 | import System.IO.Temp |
| 24 | 24 | ||
| 25 | import qualified Data.Text as T | 25 | import qualified Data.Text as T |
| 26 | 26 | ||
| 27 | import Debug.Trace | ||
| 28 | |||
| 27 | data TestPrinter = TestPrinter | 29 | data 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 | ||
| 36 | setup :: IO (ThreadId, TestPrinter, TestManager) | 38 | setup :: IO (ThreadId, QSem, TestPrinter, TestManager) |
| 37 | setup = withSystemTempFile "thermoprint.sqlite" $ \fp h -> hClose h >> do | 39 | setup = 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 | |||
| 56 | withSetup :: SpecWith (ThreadId, QSem, TestPrinter, TestManager) -> Spec | ||
| 57 | withSetup = beforeAll setup . afterAll (\(tId, termSem, _, _) -> killThread tId >> waitQSem termSem) | ||
| 52 | 58 | ||
| 53 | spec :: Spec | 59 | spec :: Spec |
| 54 | spec = beforeAll setup $ do | 60 | spec = 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 |
