2021-07-07 22:46:38 +01:00
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
2021-08-05 20:51:48 +01:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
2021-07-07 22:46:38 +01:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
2022-04-21 20:04:22 +01:00
|
|
|
{-# LANGUAGE NumericUnderscores #-}
|
2021-07-07 22:46:38 +01:00
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2021-08-05 20:51:48 +01:00
|
|
|
{-# LANGUAGE TypeApplications #-}
|
2021-07-07 22:46:38 +01:00
|
|
|
|
|
|
|
module ChatClient where
|
|
|
|
|
2022-09-14 19:45:21 +04:00
|
|
|
import Control.Concurrent (ThreadId, forkIO, forkIOWithUnmask, killThread, threadDelay)
|
2021-07-07 22:46:38 +01:00
|
|
|
import Control.Concurrent.Async
|
2021-08-05 20:51:48 +01:00
|
|
|
import Control.Concurrent.STM
|
|
|
|
import Control.Exception (bracket, bracket_)
|
2021-07-07 22:46:38 +01:00
|
|
|
import Control.Monad.Except
|
2022-04-25 16:30:21 +01:00
|
|
|
import Data.List (dropWhileEnd, find)
|
2022-07-04 11:15:25 +01:00
|
|
|
import Data.Maybe (fromJust, isNothing)
|
2022-03-13 19:34:03 +00:00
|
|
|
import qualified Data.Text as T
|
2021-08-05 20:51:48 +01:00
|
|
|
import Network.Socket
|
2021-07-07 22:46:38 +01:00
|
|
|
import Simplex.Chat
|
2021-09-04 07:32:56 +01:00
|
|
|
import Simplex.Chat.Controller (ChatConfig (..), ChatController (..))
|
2022-04-10 17:13:06 +01:00
|
|
|
import Simplex.Chat.Core
|
2021-07-07 22:46:38 +01:00
|
|
|
import Simplex.Chat.Options
|
|
|
|
import Simplex.Chat.Store
|
2022-01-21 11:09:33 +00:00
|
|
|
import Simplex.Chat.Terminal
|
|
|
|
import Simplex.Chat.Terminal.Output (newChatTerminal)
|
2022-03-13 19:34:03 +00:00
|
|
|
import Simplex.Chat.Types (Profile, User (..))
|
2021-08-02 20:10:24 +01:00
|
|
|
import Simplex.Messaging.Agent.Env.SQLite
|
2021-08-14 21:04:51 +01:00
|
|
|
import Simplex.Messaging.Agent.RetryInterval
|
2022-08-02 15:36:12 +01:00
|
|
|
import Simplex.Messaging.Client (ProtocolClientConfig (..), defaultNetworkConfig)
|
2021-08-05 20:51:48 +01:00
|
|
|
import Simplex.Messaging.Server (runSMPServerBlocking)
|
|
|
|
import Simplex.Messaging.Server.Env.STM
|
|
|
|
import Simplex.Messaging.Transport
|
2022-06-09 14:52:12 +01:00
|
|
|
import Simplex.Messaging.Version
|
2022-05-05 07:37:33 +01:00
|
|
|
import System.Directory (createDirectoryIfMissing, removePathForcibly)
|
2021-07-07 22:46:38 +01:00
|
|
|
import qualified System.Terminal as C
|
2021-08-05 20:51:48 +01:00
|
|
|
import System.Terminal.Internal (VirtualTerminal (..), VirtualTerminalSettings (..), withVirtualTerminal)
|
|
|
|
import System.Timeout (timeout)
|
2022-02-02 23:50:43 +04:00
|
|
|
import Test.Hspec (Expectation, shouldReturn)
|
2021-07-07 22:46:38 +01:00
|
|
|
|
2021-07-24 10:26:28 +01:00
|
|
|
testDBPrefix :: FilePath
|
|
|
|
testDBPrefix = "tests/tmp/test"
|
2021-07-07 22:46:38 +01:00
|
|
|
|
2021-08-05 20:51:48 +01:00
|
|
|
serverPort :: ServiceName
|
2021-12-27 15:15:35 +04:00
|
|
|
serverPort = "5001"
|
2021-08-05 20:51:48 +01:00
|
|
|
|
2022-06-06 16:23:47 +01:00
|
|
|
testOpts :: ChatOpts
|
|
|
|
testOpts =
|
2021-07-07 22:46:38 +01:00
|
|
|
ChatOpts
|
2022-01-21 11:09:33 +00:00
|
|
|
{ dbFilePrefix = undefined,
|
2022-08-30 12:49:07 +01:00
|
|
|
dbKey = "",
|
|
|
|
-- dbKey = "this is a pass-phrase to encrypt the database",
|
2022-01-20 20:23:21 +00:00
|
|
|
smpServers = ["smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=@localhost:5001"],
|
2022-08-02 15:36:12 +01:00
|
|
|
networkConfig = defaultNetworkConfig,
|
2022-02-25 16:29:36 +04:00
|
|
|
logConnections = False,
|
2022-08-13 14:18:12 +01:00
|
|
|
logServerHosts = False,
|
2022-04-10 17:13:06 +01:00
|
|
|
logAgent = False,
|
|
|
|
chatCmd = "",
|
2022-05-13 19:44:03 +01:00
|
|
|
chatCmdDelay = 3,
|
2022-06-06 16:23:47 +01:00
|
|
|
chatServerPort = Nothing,
|
|
|
|
maintenance = False
|
2021-07-07 22:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
termSettings :: VirtualTerminalSettings
|
|
|
|
termSettings =
|
|
|
|
VirtualTerminalSettings
|
|
|
|
{ virtualType = "xterm",
|
|
|
|
virtualWindowSize = pure C.Size {height = 24, width = 1000},
|
|
|
|
virtualEvent = retry,
|
|
|
|
virtualInterrupt = retry
|
|
|
|
}
|
|
|
|
|
2021-08-05 20:51:48 +01:00
|
|
|
data TestCC = TestCC
|
|
|
|
{ chatController :: ChatController,
|
|
|
|
virtualTerminal :: VirtualTerminal,
|
|
|
|
chatAsync :: Async (),
|
|
|
|
termAsync :: Async (),
|
|
|
|
termQ :: TQueue String
|
|
|
|
}
|
2021-07-07 22:46:38 +01:00
|
|
|
|
2021-08-02 20:10:24 +01:00
|
|
|
aCfg :: AgentConfig
|
|
|
|
aCfg = agentConfig defaultChatConfig
|
|
|
|
|
2022-06-09 14:52:12 +01:00
|
|
|
testAgentCfg :: AgentConfig
|
|
|
|
testAgentCfg = aCfg {reconnectInterval = (reconnectInterval aCfg) {initialInterval = 50000}}
|
|
|
|
|
|
|
|
testCfg :: ChatConfig
|
|
|
|
testCfg =
|
2021-08-02 20:10:24 +01:00
|
|
|
defaultChatConfig
|
2022-06-09 14:52:12 +01:00
|
|
|
{ agentConfig = testAgentCfg,
|
2022-02-09 20:58:02 +04:00
|
|
|
testView = True
|
2021-08-02 20:10:24 +01:00
|
|
|
}
|
|
|
|
|
2022-06-09 14:52:12 +01:00
|
|
|
testAgentCfgV1 :: AgentConfig
|
|
|
|
testAgentCfgV1 =
|
|
|
|
testAgentCfg
|
2022-08-13 11:53:53 +01:00
|
|
|
{ smpClientVRange = mkVersionRange 1 1,
|
2022-06-29 09:04:53 +01:00
|
|
|
smpAgentVRange = mkVersionRange 1 1,
|
|
|
|
smpCfg = (smpCfg testAgentCfg) {smpServerVRange = mkVersionRange 1 1}
|
2022-06-09 14:52:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
testCfgV1 :: ChatConfig
|
|
|
|
testCfgV1 = testCfg {agentConfig = testAgentCfgV1}
|
|
|
|
|
|
|
|
createTestChat :: ChatConfig -> ChatOpts -> String -> Profile -> IO TestCC
|
2022-08-30 12:49:07 +01:00
|
|
|
createTestChat cfg opts@ChatOpts {dbKey} dbPrefix profile = do
|
2022-04-26 12:52:41 +04:00
|
|
|
let dbFilePrefix = testDBPrefix <> dbPrefix
|
2022-09-02 16:38:41 +01:00
|
|
|
st <- createChatStore (dbFilePrefix <> "_chat.db") dbKey False
|
2022-06-18 20:06:13 +01:00
|
|
|
Right user <- withTransaction st $ \db -> runExceptT $ createUser db profile True
|
2022-06-09 14:52:12 +01:00
|
|
|
startTestChat_ st cfg opts dbFilePrefix user
|
2022-04-25 16:30:21 +01:00
|
|
|
|
2022-06-09 14:52:12 +01:00
|
|
|
startTestChat :: ChatConfig -> ChatOpts -> String -> IO TestCC
|
2022-08-30 12:49:07 +01:00
|
|
|
startTestChat cfg opts@ChatOpts {dbKey} dbPrefix = do
|
2022-04-26 12:52:41 +04:00
|
|
|
let dbFilePrefix = testDBPrefix <> dbPrefix
|
2022-09-02 16:38:41 +01:00
|
|
|
st <- createChatStore (dbFilePrefix <> "_chat.db") dbKey False
|
2022-06-18 20:06:13 +01:00
|
|
|
Just user <- find activeUser <$> withTransaction st getUsers
|
2022-06-09 14:52:12 +01:00
|
|
|
startTestChat_ st cfg opts dbFilePrefix user
|
2022-04-25 16:30:21 +01:00
|
|
|
|
2022-06-09 14:52:12 +01:00
|
|
|
startTestChat_ :: SQLiteStore -> ChatConfig -> ChatOpts -> FilePath -> User -> IO TestCC
|
|
|
|
startTestChat_ st cfg opts dbFilePrefix user = do
|
2021-07-07 22:46:38 +01:00
|
|
|
t <- withVirtualTerminal termSettings pure
|
2022-01-21 11:09:33 +00:00
|
|
|
ct <- newChatTerminal t
|
2022-04-10 17:13:06 +01:00
|
|
|
cc <- newChatController st (Just user) cfg opts {dbFilePrefix} Nothing -- no notifications
|
2022-06-06 16:23:47 +01:00
|
|
|
chatAsync <- async . runSimplexChat opts user cc . const $ runChatTerminal ct
|
2022-07-04 11:15:25 +01:00
|
|
|
atomically . unless (maintenance opts) $ readTVar (agentAsync cc) >>= \a -> when (isNothing a) retry
|
2021-08-05 20:51:48 +01:00
|
|
|
termQ <- newTQueueIO
|
|
|
|
termAsync <- async $ readTerminalOutput t termQ
|
|
|
|
pure TestCC {chatController = cc, virtualTerminal = t, chatAsync, termAsync, termQ}
|
|
|
|
|
2022-04-25 16:30:21 +01:00
|
|
|
stopTestChat :: TestCC -> IO ()
|
|
|
|
stopTestChat TestCC {chatController = cc, chatAsync, termAsync} = do
|
2022-09-14 19:45:21 +04:00
|
|
|
void . forkIO $ stopChatController cc
|
2022-04-25 16:30:21 +01:00
|
|
|
uninterruptibleCancel termAsync
|
|
|
|
uninterruptibleCancel chatAsync
|
2022-09-14 19:45:21 +04:00
|
|
|
threadDelay 100000
|
2022-04-25 16:30:21 +01:00
|
|
|
|
2022-04-26 12:52:41 +04:00
|
|
|
withNewTestChat :: String -> Profile -> (TestCC -> IO a) -> IO a
|
2022-06-09 14:52:12 +01:00
|
|
|
withNewTestChat = withNewTestChatCfgOpts testCfg testOpts
|
|
|
|
|
|
|
|
withNewTestChatV1 :: String -> Profile -> (TestCC -> IO a) -> IO a
|
|
|
|
withNewTestChatV1 = withNewTestChatCfg testCfgV1
|
|
|
|
|
|
|
|
withNewTestChatCfg :: ChatConfig -> String -> Profile -> (TestCC -> IO a) -> IO a
|
|
|
|
withNewTestChatCfg cfg = withNewTestChatCfgOpts cfg testOpts
|
2022-06-06 16:23:47 +01:00
|
|
|
|
|
|
|
withNewTestChatOpts :: ChatOpts -> String -> Profile -> (TestCC -> IO a) -> IO a
|
2022-06-09 14:52:12 +01:00
|
|
|
withNewTestChatOpts = withNewTestChatCfgOpts testCfg
|
|
|
|
|
|
|
|
withNewTestChatCfgOpts :: ChatConfig -> ChatOpts -> String -> Profile -> (TestCC -> IO a) -> IO a
|
|
|
|
withNewTestChatCfgOpts cfg opts dbPrefix profile = bracket (createTestChat cfg opts dbPrefix profile) (\cc -> cc <// 100000 >> stopTestChat cc)
|
|
|
|
|
|
|
|
withTestChatV1 :: String -> (TestCC -> IO a) -> IO a
|
|
|
|
withTestChatV1 = withTestChatCfg testCfgV1
|
2022-04-25 16:30:21 +01:00
|
|
|
|
2022-04-26 12:52:41 +04:00
|
|
|
withTestChat :: String -> (TestCC -> IO a) -> IO a
|
2022-06-09 14:52:12 +01:00
|
|
|
withTestChat = withTestChatCfgOpts testCfg testOpts
|
|
|
|
|
|
|
|
withTestChatCfg :: ChatConfig -> String -> (TestCC -> IO a) -> IO a
|
|
|
|
withTestChatCfg cfg = withTestChatCfgOpts cfg testOpts
|
2022-06-06 16:23:47 +01:00
|
|
|
|
|
|
|
withTestChatOpts :: ChatOpts -> String -> (TestCC -> IO a) -> IO a
|
2022-06-09 14:52:12 +01:00
|
|
|
withTestChatOpts = withTestChatCfgOpts testCfg
|
|
|
|
|
|
|
|
withTestChatCfgOpts :: ChatConfig -> ChatOpts -> String -> (TestCC -> IO a) -> IO a
|
|
|
|
withTestChatCfgOpts cfg opts dbPrefix = bracket (startTestChat cfg opts dbPrefix) (\cc -> cc <// 100000 >> stopTestChat cc)
|
2022-04-25 16:30:21 +01:00
|
|
|
|
2021-08-05 20:51:48 +01:00
|
|
|
readTerminalOutput :: VirtualTerminal -> TQueue String -> IO ()
|
|
|
|
readTerminalOutput t termQ = do
|
|
|
|
let w = virtualWindow t
|
|
|
|
winVar <- atomically $ newTVar . init =<< readTVar w
|
|
|
|
forever . atomically $ do
|
|
|
|
win <- readTVar winVar
|
|
|
|
win' <- init <$> readTVar w
|
|
|
|
if win' == win
|
|
|
|
then retry
|
|
|
|
else do
|
|
|
|
let diff = getDiff win' win
|
|
|
|
forM_ diff $ writeTQueue termQ
|
|
|
|
writeTVar winVar win'
|
|
|
|
where
|
|
|
|
getDiff :: [String] -> [String] -> [String]
|
|
|
|
getDiff win win' = getDiff_ 1 (length win) win win'
|
|
|
|
getDiff_ :: Int -> Int -> [String] -> [String] -> [String]
|
|
|
|
getDiff_ n len win' win =
|
|
|
|
let diff = drop (len - n) win'
|
|
|
|
in if drop n win <> diff == win'
|
|
|
|
then map (dropWhileEnd (== ' ')) diff
|
|
|
|
else getDiff_ (n + 1) len win' win
|
2021-07-07 22:46:38 +01:00
|
|
|
|
2022-02-06 16:18:01 +00:00
|
|
|
withTmpFiles :: IO () -> IO ()
|
|
|
|
withTmpFiles =
|
2021-07-24 10:26:28 +01:00
|
|
|
bracket_
|
|
|
|
(createDirectoryIfMissing False "tests/tmp")
|
2022-05-05 07:37:33 +01:00
|
|
|
(removePathForcibly "tests/tmp")
|
2022-02-06 16:18:01 +00:00
|
|
|
|
2022-06-09 14:52:12 +01:00
|
|
|
testChatN :: ChatConfig -> ChatOpts -> [Profile] -> ([TestCC] -> IO ()) -> IO ()
|
|
|
|
testChatN cfg opts ps test = withTmpFiles $ do
|
2022-04-25 16:30:21 +01:00
|
|
|
tcs <- getTestCCs (zip ps [1 ..]) []
|
2022-02-06 16:18:01 +00:00
|
|
|
test tcs
|
|
|
|
concurrentlyN_ $ map (<// 100000) tcs
|
2022-04-25 16:30:21 +01:00
|
|
|
concurrentlyN_ $ map stopTestChat tcs
|
2021-07-24 10:26:28 +01:00
|
|
|
where
|
2022-04-25 16:30:21 +01:00
|
|
|
getTestCCs :: [(Profile, Int)] -> [TestCC] -> IO [TestCC]
|
2021-07-24 10:26:28 +01:00
|
|
|
getTestCCs [] tcs = pure tcs
|
2022-06-09 14:52:12 +01:00
|
|
|
getTestCCs ((p, db) : envs') tcs = (:) <$> createTestChat cfg opts (show db) p <*> getTestCCs envs' tcs
|
2021-07-24 10:26:28 +01:00
|
|
|
|
2022-02-02 23:50:43 +04:00
|
|
|
(<//) :: TestCC -> Int -> Expectation
|
|
|
|
(<//) cc t = timeout t (getTermLine cc) `shouldReturn` Nothing
|
|
|
|
|
|
|
|
getTermLine :: TestCC -> IO String
|
|
|
|
getTermLine = atomically . readTQueue . termQ
|
|
|
|
|
2022-03-13 19:34:03 +00:00
|
|
|
-- Use code below to echo virtual terminal
|
2022-04-05 10:01:08 +04:00
|
|
|
-- getTermLine :: TestCC -> IO String
|
2022-03-10 15:45:40 +04:00
|
|
|
-- getTermLine cc = do
|
|
|
|
-- s <- atomically . readTQueue $ termQ cc
|
2022-03-13 19:34:03 +00:00
|
|
|
-- name <- userName cc
|
|
|
|
-- putStrLn $ name <> ": " <> s
|
2022-03-10 15:45:40 +04:00
|
|
|
-- pure s
|
|
|
|
|
2022-03-13 19:34:03 +00:00
|
|
|
userName :: TestCC -> IO [Char]
|
|
|
|
userName (TestCC ChatController {currentUser} _ _ _ _) = T.unpack . localDisplayName . fromJust <$> readTVarIO currentUser
|
|
|
|
|
2021-07-07 22:46:38 +01:00
|
|
|
testChat2 :: Profile -> Profile -> (TestCC -> TestCC -> IO ()) -> IO ()
|
2022-06-09 14:52:12 +01:00
|
|
|
testChat2 = testChatCfgOpts2 testCfg testOpts
|
|
|
|
|
|
|
|
testChatCfg2 :: ChatConfig -> Profile -> Profile -> (TestCC -> TestCC -> IO ()) -> IO ()
|
|
|
|
testChatCfg2 cfg = testChatCfgOpts2 cfg testOpts
|
2022-06-06 16:23:47 +01:00
|
|
|
|
|
|
|
testChatOpts2 :: ChatOpts -> Profile -> Profile -> (TestCC -> TestCC -> IO ()) -> IO ()
|
2022-06-09 14:52:12 +01:00
|
|
|
testChatOpts2 = testChatCfgOpts2 testCfg
|
|
|
|
|
|
|
|
testChatCfgOpts2 :: ChatConfig -> ChatOpts -> Profile -> Profile -> (TestCC -> TestCC -> IO ()) -> IO ()
|
|
|
|
testChatCfgOpts2 cfg opts p1 p2 test = testChatN cfg opts [p1, p2] test_
|
2021-07-24 10:26:28 +01:00
|
|
|
where
|
|
|
|
test_ :: [TestCC] -> IO ()
|
|
|
|
test_ [tc1, tc2] = test tc1 tc2
|
|
|
|
test_ _ = error "expected 2 chat clients"
|
|
|
|
|
|
|
|
testChat3 :: Profile -> Profile -> Profile -> (TestCC -> TestCC -> TestCC -> IO ()) -> IO ()
|
2022-06-09 14:52:12 +01:00
|
|
|
testChat3 = testChatCfgOpts3 testCfg testOpts
|
|
|
|
|
|
|
|
testChatCfg3 :: ChatConfig -> Profile -> Profile -> Profile -> (TestCC -> TestCC -> TestCC -> IO ()) -> IO ()
|
|
|
|
testChatCfg3 cfg = testChatCfgOpts3 cfg testOpts
|
|
|
|
|
|
|
|
testChatCfgOpts3 :: ChatConfig -> ChatOpts -> Profile -> Profile -> Profile -> (TestCC -> TestCC -> TestCC -> IO ()) -> IO ()
|
|
|
|
testChatCfgOpts3 cfg opts p1 p2 p3 test = testChatN cfg opts [p1, p2, p3] test_
|
2021-07-24 10:26:28 +01:00
|
|
|
where
|
|
|
|
test_ :: [TestCC] -> IO ()
|
|
|
|
test_ [tc1, tc2, tc3] = test tc1 tc2 tc3
|
|
|
|
test_ _ = error "expected 3 chat clients"
|
|
|
|
|
|
|
|
testChat4 :: Profile -> Profile -> Profile -> Profile -> (TestCC -> TestCC -> TestCC -> TestCC -> IO ()) -> IO ()
|
2022-06-09 14:52:12 +01:00
|
|
|
testChat4 p1 p2 p3 p4 test = testChatN testCfg testOpts [p1, p2, p3, p4] test_
|
2021-07-24 10:26:28 +01:00
|
|
|
where
|
|
|
|
test_ :: [TestCC] -> IO ()
|
|
|
|
test_ [tc1, tc2, tc3, tc4] = test tc1 tc2 tc3 tc4
|
|
|
|
test_ _ = error "expected 4 chat clients"
|
|
|
|
|
|
|
|
concurrentlyN_ :: [IO a] -> IO ()
|
|
|
|
concurrentlyN_ = mapConcurrently_ id
|
2021-08-05 20:51:48 +01:00
|
|
|
|
|
|
|
serverCfg :: ServerConfig
|
|
|
|
serverCfg =
|
|
|
|
ServerConfig
|
2022-01-11 08:50:44 +00:00
|
|
|
{ transports = [(serverPort, transport @TLS)],
|
2021-08-05 20:51:48 +01:00
|
|
|
tbqSize = 1,
|
2022-01-06 16:03:45 +00:00
|
|
|
serverTbqSize = 1,
|
2022-04-26 12:52:41 +04:00
|
|
|
msgQueueQuota = 16,
|
2021-08-05 20:51:48 +01:00
|
|
|
queueIdBytes = 12,
|
|
|
|
msgIdBytes = 6,
|
2022-04-21 20:04:22 +01:00
|
|
|
storeLogFile = Nothing,
|
2022-06-16 20:00:51 +01:00
|
|
|
storeMsgsFile = Nothing,
|
2022-04-21 20:04:22 +01:00
|
|
|
allowNewQueues = True,
|
2022-04-30 12:47:50 +01:00
|
|
|
messageExpiration = Just defaultMessageExpiration,
|
|
|
|
inactiveClientExpiration = Just defaultInactiveClientExpiration,
|
2022-01-11 08:50:44 +00:00
|
|
|
caCertificateFile = "tests/fixtures/tls/ca.crt",
|
|
|
|
privateKeyFile = "tests/fixtures/tls/server.key",
|
2022-05-09 18:53:39 +04:00
|
|
|
certificateFile = "tests/fixtures/tls/server.crt",
|
|
|
|
logStatsInterval = Just 86400,
|
2022-06-09 14:52:12 +01:00
|
|
|
logStatsStartTime = 0,
|
2022-07-17 15:51:17 +01:00
|
|
|
serverStatsLogFile = "tests/smp-server-stats.daily.log",
|
|
|
|
serverStatsBackupFile = Nothing,
|
2022-06-09 14:52:12 +01:00
|
|
|
smpServerVRange = supportedSMPServerVRange
|
2021-08-05 20:51:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
withSmpServer :: IO a -> IO a
|
|
|
|
withSmpServer = serverBracket (`runSMPServerBlocking` serverCfg) (pure ()) . const
|
|
|
|
|
|
|
|
serverBracket :: (TMVar Bool -> IO ()) -> IO () -> (ThreadId -> IO a) -> IO a
|
|
|
|
serverBracket process afterProcess f = do
|
|
|
|
started <- newEmptyTMVarIO
|
|
|
|
bracket
|
|
|
|
(forkIOWithUnmask ($ process started))
|
|
|
|
(\t -> killThread t >> afterProcess >> waitFor started "stop")
|
|
|
|
(\t -> waitFor started "start" >> f t)
|
|
|
|
where
|
|
|
|
waitFor started s =
|
|
|
|
5000000 `timeout` atomically (takeTMVar started) >>= \case
|
|
|
|
Nothing -> error $ "server did not " <> s
|
|
|
|
_ -> pure ()
|