tests: Use async instead of forkIO in test setup

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5544
GitOrigin-RevId: e932e1f76c6e20121c64a429a6eb613d550c6eca
This commit is contained in:
Daniel Harvey 2022-08-19 15:55:45 +01:00 committed by hasura-bot
parent 2ff8aa8f7d
commit 6040cb065e
4 changed files with 18 additions and 17 deletions

View File

@ -39,7 +39,7 @@ where
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
import Control.Concurrent (forkIO) import Control.Concurrent.Async qualified as Async
import Control.Concurrent.Extended (sleep) import Control.Concurrent.Extended (sleep)
import Control.Monad.Trans.Managed (ManagedT (..), lowerManagedT) import Control.Monad.Trans.Managed (ManagedT (..), lowerManagedT)
import Data.Aeson (Value, object, (.=)) import Data.Aeson (Value, object, (.=))
@ -223,18 +223,18 @@ args:
-- The port availability is subject to races. -- The port availability is subject to races.
startServerThread :: Maybe (String, Int) -> IO Server startServerThread :: Maybe (String, Int) -> IO Server
startServerThread murlPrefixport = do startServerThread murlPrefixport = do
(urlPrefix, port, threadId) <- (urlPrefix, port, thread) <-
case murlPrefixport of case murlPrefixport of
Just (urlPrefix, port) -> do Just (urlPrefix, port) -> do
threadId <- forkIO (forever (sleep 1)) -- Just wait. thread <- Async.async (forever (sleep 1)) -- Just wait.
pure (urlPrefix, port, threadId) pure (urlPrefix, port, thread)
Nothing -> do Nothing -> do
port <- bracket (Warp.openFreePort) (Socket.close . snd) (pure . fst) port <- bracket (Warp.openFreePort) (Socket.close . snd) (pure . fst)
let urlPrefix = "http://127.0.0.1" let urlPrefix = "http://127.0.0.1"
threadId <- thread <-
forkIO (runApp Constants.serveOptions {soPort = unsafePort port}) Async.async (runApp Constants.serveOptions {soPort = unsafePort port})
pure (urlPrefix, port, threadId) pure (urlPrefix, port, thread)
let server = Server {port = fromIntegral port, urlPrefix, threadId} let server = Server {port = fromIntegral port, urlPrefix, thread}
Http.healthCheck (serverUrl server) Http.healthCheck (serverUrl server)
pure server pure server

View File

@ -9,7 +9,7 @@ where
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
import Control.Concurrent (forkIO) import Control.Concurrent.Async qualified as Async
import Control.Exception.Safe (bracket) import Control.Exception.Safe (bracket)
import Data.Aeson qualified as Aeson import Data.Aeson qualified as Aeson
import Data.ByteString.Lazy qualified as Lazy (ByteString) import Data.ByteString.Lazy qualified as Lazy (ByteString)
@ -58,7 +58,7 @@ run ::
run (Interpreter interpreter) = do run (Interpreter interpreter) = do
let urlPrefix = "http://127.0.0.1" let urlPrefix = "http://127.0.0.1"
port <- bracket (Warp.openFreePort) (Socket.close . snd) (pure . fst) port <- bracket (Warp.openFreePort) (Socket.close . snd) (pure . fst)
threadId <- forkIO $ thread <- Async.async $
Spock.runSpockNoBanner port $ Spock.runSpockNoBanner port $
Spock.spockT id $ do Spock.spockT id $ do
Spock.get "/" $ do Spock.get "/" $ do
@ -69,7 +69,7 @@ run (Interpreter interpreter) = do
result <- liftIO $ interpreter body result <- liftIO $ interpreter body
Spock.setHeader "Content-Type" "application/json; charset=utf-8" Spock.setHeader "Content-Type" "application/json; charset=utf-8"
Spock.lazyBytes result Spock.lazyBytes result
let server = Server {port = fromIntegral port, urlPrefix, threadId} let server = Server {port = fromIntegral port, urlPrefix, thread}
Http.healthCheck $ serverUrl server Http.healthCheck $ serverUrl server
pure server pure server

View File

@ -11,7 +11,8 @@ module Harness.TestEnvironment
) )
where where
import Control.Concurrent (ThreadId, killThread) import Control.Concurrent.Async (Async)
import Control.Concurrent.Async qualified as Async
import Data.UUID (UUID) import Data.UUID (UUID)
import Data.Word import Data.Word
import Harness.Test.BackendType import Harness.Test.BackendType
@ -44,7 +45,7 @@ data Server = Server
-- | The full URI prefix e.g. http://localhost -- | The full URI prefix e.g. http://localhost
urlPrefix :: String, urlPrefix :: String,
-- | The thread that the server is running on, so we can stop it later. -- | The thread that the server is running on, so we can stop it later.
threadId :: ThreadId thread :: Async ()
} }
-- | Retrieve the 'Server' associated with some 'TestEnvironment'. -- | Retrieve the 'Server' associated with some 'TestEnvironment'.
@ -62,4 +63,4 @@ serverUrl Server {urlPrefix, port} = urlPrefix ++ ":" ++ show port
-- | Forcibly stop a given 'Server'. -- | Forcibly stop a given 'Server'.
stopServer :: Server -> IO () stopServer :: Server -> IO ()
stopServer Server {threadId} = killThread threadId stopServer Server {thread} = Async.cancel thread

View File

@ -5,7 +5,7 @@ module Harness.Webhook
) )
where where
import Control.Concurrent (forkIO) import Control.Concurrent.Async as Async
import Control.Concurrent.Chan qualified as Chan import Control.Concurrent.Chan qualified as Chan
import Control.Exception.Safe (bracket) import Control.Exception.Safe (bracket)
import Data.Aeson qualified as Aeson import Data.Aeson qualified as Aeson
@ -42,7 +42,7 @@ run = do
port <- bracket (Warp.openFreePort) (Socket.close . snd) (pure . fst) port <- bracket (Warp.openFreePort) (Socket.close . snd) (pure . fst)
eventsQueueChan <- Chan.newChan eventsQueueChan <- Chan.newChan
let eventsQueue = EventsQueue eventsQueueChan let eventsQueue = EventsQueue eventsQueueChan
threadId <- forkIO $ thread <- Async.async $
Spock.runSpockNoBanner port $ Spock.runSpockNoBanner port $
Spock.spockT id $ do Spock.spockT id $ do
Spock.get "/" $ Spock.get "/" $
@ -63,6 +63,6 @@ run = do
fromMaybe (error "error in parsing the event data from the body") eventDataPayload fromMaybe (error "error in parsing the event data from the body") eventDataPayload
Spock.setHeader "Content-Type" "application/json; charset=utf-8" Spock.setHeader "Content-Type" "application/json; charset=utf-8"
Spock.json $ Aeson.object ["success" Aeson..= True] Spock.json $ Aeson.object ["success" Aeson..= True]
let server = Server {port = fromIntegral port, urlPrefix, threadId} let server = Server {port = fromIntegral port, urlPrefix, thread}
Http.healthCheck $ serverUrl server Http.healthCheck $ serverUrl server
pure (server, eventsQueue) pure (server, eventsQueue)