@@ -20,7 +20,8 @@ import Network.Transport (EndPointAddress)
20import Network.Transport.TCP (encodeEndPointAddress, decodeEndPointAddress)
20import Network.Transport.TCP (encodeEndPointAddress, decodeEndPointAddress)
21import Data.Either (isRight)
21import Data.Either (isRight)
22import Control.Exception
22import Control.Exception
23−--import Control.Monad.IO.Class
23+import System.IO.Temp
24+import System.FilePath
24#if defined(linux_HOST_OS)
25#if defined(linux_HOST_OS)
25import System.Directory
26import System.Directory
@@ -43,7 +44,7 @@ testList sessionId conn notificationTestMVar = TestList $ serverTests ++ session
43 testNotification notificationTestMVar
44 testNotification notificationTestMVar
45 serverTests = [testRequestTimeout, testFileDescriptorCount]
46 serverTests = [testRequestTimeout, testFileDescriptorCount]
47main :: IO ()
48main :: IO ()
49 (serverAddress, _) <- launchTestServer 0
50 (serverAddress, _) <- launchTestServer 0
@@ -80,13 +81,17 @@ testConnection serverAddress mvar = do
80-- | A version of 'launchServer' which returns the port on which the server is listening on a secondary thread
81-- | A version of 'launchServer' which returns the port on which the server is listening on a secondary thread
81launchTestServer :: Timeout -> IO (EndPointAddress, ThreadId)
82launchTestServer :: Timeout -> IO (EndPointAddress, ThreadId)
82launchTestServer ti = do
83launchTestServer ti = do
83− let config = defaultServerConfig { databaseName = testDatabaseName,
84− perRequestTimeout = ti,
88 addressMVar <- newEmptyMVar
84 addressMVar <- newEmptyMVar
86+ withSystemTempDirectory "projectm36test" $ \tempdir -> do
87+ let config = defaultServerConfig { databaseName = testDatabaseName,
88+ persistenceStrategy = CrashSafePersistence (tempdir </> "db"),
89+ perRequestTimeout = ti,
89− tid <- forkIO $ launchServer config (Just addressMVar) >> pure ()
94+ launchServer config (Just addressMVar) >> pure ()
90 endPointAddress <- takeMVar addressMVar
95 endPointAddress <- takeMVar addressMVar
91 --liftIO $ putStrLn ("launched server on " ++ show endPointAddress)
96 --liftIO $ putStrLn ("launched server on " ++ show endPointAddress)
92 pure (endPointAddress, tid)
97 pure (endPointAddress, tid)
@@ -212,19 +217,24 @@ testFileDescriptorCount :: Test
212--validate that creating a server, connecting a client, and then disconnecting doesn't leak file descriptors
217--validate that creating a server, connecting a client, and then disconnecting doesn't leak file descriptors
213testFileDescriptorCount = TestCase $ do
218testFileDescriptorCount = TestCase $ do
214 (serverAddress, serverTid) <- launchTestServer 1000
219 (serverAddress, serverTid) <- launchTestServer 1000
215− startCount <- fdCount
216 unusedMVar <- newEmptyMVar
220 unusedMVar <- newEmptyMVar
221+ startCount <- fdCount
217− Right (_, testConn) <- testConnection serverAddress unusedMVar
222+ Right (sess, testConn) <- testConnection serverAddress unusedMVar
223+ --add a test commit to trigger the fsync machinery
224+ executeDatabaseContextExpr sess testConn (Assign "x" (ExistingRelation relationFalse)) >>= eitherFail
225+ commit sess testConn >>= eitherFail
218 close testConn
226 close testConn
219 endCount <- fdCount
227 endCount <- fdCount
220− assertBool "fd leak" (endCount - startCount <= 1)
228+ let fd_diff = endCount - startCount
229+ assertBool ("fd leak: " ++ show fd_diff) (fd_diff <= 0)
221 killThread serverTid
230 killThread serverTid
223-- returns the number of open file descriptors -- linux only /proc usage
233-- returns the number of open file descriptors -- linux only /proc usage
224fdCount :: IO Int
234fdCount :: IO Int
225fdCount = do
235fdCount = do
226 fds <- getDirectoryContents "/proc/self/fd"
236 fds <- getDirectoryContents "/proc/self/fd"
227− pure ((length fds) - 2)
237+ pure (length fds)
229--pass on non-linux platforms
239--pass on non-linux platforms
230testFileDescriptorCount = TestCase (pure ())
240testFileDescriptorCount = TestCase (pure ())