tests: block on tcp server creation (#99)

Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
This commit is contained in:
Efim PoberezkinandEvgeny Poberezkin authored and GitHub committed 2021-04-14 02:25:57 +04:00
1 parent c4475499ba
commit 2eb5ce24ec
10 files changed
+48 -31

No files matched your search

+7 -4
View File
@@ -9,13 +9,15 @@ import Control.Monad.IO.Unlift
import Crypto.Random
import Network.Socket
import Simplex.Messaging.Protocol
import Simplex.Messaging.Server
import Simplex.Messaging.Server (runSMPServerBlocking)
import Simplex.Messaging.Server.Env.STM
import Simplex.Messaging.Transport
import System.Timeout (timeout)
import Test.Hspec
import UnliftIO.Concurrent
import qualified UnliftIO.Exception as E
import UnliftIO.IO
import UnliftIO.STM (atomically, newEmptyTMVarIO, takeTMVar)
testHost :: HostName
testHost = "localhost"
@@ -25,7 +27,6 @@ testPort = "5000"
testSMPClient :: MonadUnliftIO m => (Handle -> m a) -> m a
testSMPClient client = do
threadDelay 250_000 -- TODO hack: thread delay for SMP server to start
runTCPClient testHost testPort $ \h -> do
line <- liftIO $ getLn h
if line == "Welcome to SMP v0.2.0"
@@ -42,10 +43,12 @@ cfg =
}
withSmpServerThreadOn :: (MonadUnliftIO m, MonadRandom m) => ServiceName -> (ThreadId -> m a) -> m a
withSmpServerThreadOn port =
withSmpServerThreadOn port f = do
started <- newEmptyTMVarIO
E.bracket
(forkIOWithUnmask ($ runSMPServer cfg {tcpPort = port}))
(forkIOWithUnmask ($ runSMPServerBlocking started cfg {tcpPort = port}))
(liftIO . killThread)
\x -> liftIO (5_000_000 `timeout` atomically (takeTMVar started)) >> f x
withSmpServerOn :: (MonadUnliftIO m, MonadRandom m) => ServiceName -> m a -> m a
withSmpServerOn port = withSmpServerThreadOn port . const