aboutsummaryrefslogtreecommitdiff
path: root/server/test
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-02-17 01:05:57 +0000
committerGregor Kleen <gkleen@yggdrasil.li>2016-02-17 01:05:57 +0000
commitfff9cbfc9e7919723349e18c4b9aea89bcc48c1a (patch)
tree8256a7c6a8e77d01446e70c5692c868c24a18b1f /server/test
parente3068de72434d6152c40df691f26943c88327406 (diff)
downloadthermoprint-fff9cbfc9e7919723349e18c4b9aea89bcc48c1a.tar
thermoprint-fff9cbfc9e7919723349e18c4b9aea89bcc48c1a.tar.gz
thermoprint-fff9cbfc9e7919723349e18c4b9aea89bcc48c1a.tar.bz2
thermoprint-fff9cbfc9e7919723349e18c4b9aea89bcc48c1a.tar.xz
thermoprint-fff9cbfc9e7919723349e18c4b9aea89bcc48c1a.zip
Harness for tests of Thermoprint.Server
Diffstat (limited to 'server/test')
-rw-r--r--server/test/Thermoprint/ServerSpec.hs59
1 files changed, 59 insertions, 0 deletions
diff --git a/server/test/Thermoprint/ServerSpec.hs b/server/test/Thermoprint/ServerSpec.hs
new file mode 100644
index 0000000..aa654b1
--- /dev/null
+++ b/server/test/Thermoprint/ServerSpec.hs
@@ -0,0 +1,59 @@
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE ImpredicativeTypes #-}
3
4module Thermoprint.ServerSpec (spec) where
5
6import Test.HUnit
7import Test.Hspec
8import Test.Hspec.Contrib.HUnit
9
10import Thermoprint.API
11import Thermoprint.Server
12
13import Control.Monad
14import Control.Monad.Logger
15import Control.Monad.Reader
16import Control.Monad.Trans.Identity
17import Control.Monad.Trans.Resource
18
19import Database.Persist.Sqlite
20
21import Control.Concurrent
22import Control.Concurrent.STM
23
24import System.IO
25import System.IO.Temp
26
27import qualified Data.Text as T
28
29data TestPrinter = TestPrinter
30 { outputChan :: TChan Printout
31 , failSwitch :: TMVar PrintingError
32 }
33
34data TestManager = TestManager
35 { manage :: TMVar (QueueManager IdentityT)
36 }
37
38setup :: IO (ThreadId, TestPrinter, TestManager)
39setup = withSystemTempFile "thermoprint.sqlite" $ \fp h -> hClose h >> do
40 tPrinter <- TestPrinter <$> newTChanIO <*> newEmptyTMVarIO
41 tManager <- TestManager <$> newEmptyTMVarIO
42 let
43 runSqlite :: ReaderT ConnectionPool (LoggingT IO) a -> IO a
44 runSqlite = runStderrLoggingT . withSqlitePool (T.pack fp) 1 . runReaderT
45
46 printers = [ ( pure $ PM tPM
47 , QMConfig (join . lift $ takeTMVar (manage tManager)) (Nat $ liftIO . runIdentityT)
48 )
49 ]
50
51 tPM :: MonadIO m => Printout -> m (Maybe PrintingError)
52 tPM printout = liftIO . atomically $ writeTChan (outputChan tPrinter) printout >> tryTakeTMVar (failSwitch tPrinter)
53 (,,) <$> forkIO (thermoprintServer (Nat runSqlite) $ def `withPrinters` printers) <*> pure tPrinter <*> pure tManager
54
55spec :: Spec
56spec = do
57 fromHUnitTest . test . ("blub" ~:) $ do
58 putStrLn "Blub."
59 return True