server: Replace forkIO with async in the DC mock agent.

I got a flaky test run recently, in which some Data Connectors tests in
_tests-hspec_ failed. The failure was not very helpful, but the log
output contained this message:

> Network.Socket.bind: resource busy (Address already in use)

I _think_ this was caused by killing the Data Connectors mock agent
thread, but not waiting for the server to stop. `Async.cancel` should
handle this, as it waits for the thread to stop.

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5121
GitOrigin-RevId: 3419dce2fc5ff52e3a6f2d452ea44dd85b326452
This commit is contained in:
Samir Talwar 2022-07-21 06:56:26 +02:00 committed by hasura-bot
parent ee846db056
commit aaf97834f3

View File

@ -24,7 +24,8 @@ where
--------------------------------------------------------------------------------
import Control.Concurrent (ThreadId, forkIO, killThread)
import Control.Concurrent.Async (Async)
import Control.Concurrent.Async qualified as Async
import Data.Aeson qualified as Aeson
import Data.IORef qualified as I
import Harness.Backend.DataConnector.MockAgent
@ -109,7 +110,7 @@ teardown (testEnvironment, _) = do
data MockAgentEnvironment = MockAgentEnvironment
{ maeConfig :: I.IORef MockConfig,
maeQuery :: I.IORef (Maybe API.QueryRequest),
maeThreadId :: ThreadId,
maeThread :: Async (),
maeQueryConfig :: I.IORef (Maybe API.Config)
}
@ -119,7 +120,7 @@ mkLocalTestEnvironmentMock _ = do
maeConfig <- I.newIORef chinookMock
maeQuery <- I.newIORef Nothing
maeQueryConfig <- I.newIORef Nothing
maeThreadId <- forkIO $ runMockServer maeConfig maeQuery maeQueryConfig
maeThread <- Async.async $ runMockServer maeConfig maeQuery maeQueryConfig
healthCheck $ "http://127.0.0.1:" <> show mockAgentPort <> "/health"
pure $ MockAgentEnvironment {..}
@ -133,7 +134,7 @@ setupMock sourceMetadata backendConfig (testEnvironment, _mockAgentEnvironment)
teardownMock :: (TestEnvironment, MockAgentEnvironment) -> IO ()
teardownMock (testEnvironment, MockAgentEnvironment {..}) = do
GraphqlEngine.clearMetadata testEnvironment
killThread maeThreadId
Async.cancel maeThread
-- | Mock Agent test case input.
data TestCase = TestCase