tests: randomize db file name to fix io errors (#16)

* tests: check file doesn't exist in store tests after

* Revert "tests: check file doesn't exist in store tests after"

This reverts commit de624433a9.

* [WIP] tests: randomize db file

* [WIP] tests: try runIO to get filename into after

* random db file name for agent tests

* tests: randomize DB file name

* tests: increase SMP agent start delay

Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
This commit is contained in:
Efim Poberezkin
2021-01-16 22:02:18 +04:00
parent 3efb15ecb3
commit c1f15c9d93
4 changed files with 24 additions and 8 deletions
+1
View File
@@ -60,6 +60,7 @@ tests:
- hspec == 2.7.*
- hspec-core == 2.7.*
- HUnit == 1.6.*
- random == 1.1.*
ghc-options:
# - -haddock
+6 -4
View File
@@ -49,7 +49,8 @@ addRcvQueueQuery =
|]
data SQLiteStore = SQLiteStore
{ conn :: DB.Connection,
{ dbFilename :: String,
conn :: DB.Connection,
serversLock :: TMVar (),
rcvQueuesLock :: TMVar (),
sndQueuesLock :: TMVar (),
@@ -58,8 +59,8 @@ data SQLiteStore = SQLiteStore
}
newSQLiteStore :: MonadUnliftIO m => String -> m SQLiteStore
newSQLiteStore dbFile = do
conn <- liftIO $ DB.open dbFile
newSQLiteStore dbFilename = do
conn <- liftIO $ DB.open dbFilename
liftIO $ createSchema conn
serversLock <- newTMVarIO ()
rcvQueuesLock <- newTMVarIO ()
@@ -68,7 +69,8 @@ newSQLiteStore dbFile = do
messagesLock <- newTMVarIO ()
return
SQLiteStore
{ conn,
{ dbFilename,
conn,
serversLock,
rcvQueuesLock,
sndQueuesLock,
+15 -3
View File
@@ -5,11 +5,13 @@
module AgentTests.SQLite where
import Control.Monad.Except
import Data.Word (Word32)
import qualified Database.SQLite.Simple as DB
import Simplex.Messaging.Agent.Store
import Simplex.Messaging.Agent.Store.SQLite
import Simplex.Messaging.Agent.Store.Types
import Simplex.Messaging.Agent.Transmission
import System.Random
import Test.Hspec
import UnliftIO.Directory
@@ -17,9 +19,19 @@ testDB :: String
testDB = "smp-agent.test.db"
withStore :: SpecWith SQLiteStore -> Spec
withStore =
before (newSQLiteStore testDB)
. after (\store -> DB.close (conn store) >> removeFile testDB)
withStore = before createStore . after removeStore
where
createStore :: IO SQLiteStore
createStore = do
-- Randomize DB file name to avoid SQLite IO errors supposedly caused by asynchronous
-- IO operations on multiple similarly named files; error specific to some environments
r <- randomIO :: IO Word32
newSQLiteStore $ testDB <> show r
removeStore :: SQLiteStore -> IO ()
removeStore store = do
DB.close $ conn store
removeFile $ dbFilename store
returnsResult :: (Eq a, Eq e, Show a, Show e) => ExceptT e IO a -> a -> Expectation
action `returnsResult` r = runExceptT action `shouldReturn` Right r
+2 -1
View File
@@ -1,4 +1,5 @@
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
module SMPAgentClient where
@@ -56,7 +57,7 @@ withSmpAgent =
testSMPAgentClient :: MonadUnliftIO m => (Handle -> m a) -> m a
testSMPAgentClient client = do
threadDelay 25000 -- TODO hack: thread delay for SMP agent to start
threadDelay 100_000 -- TODO hack: thread delay for SMP agent to start
runTCPClient agentTestHost agentTestPort $ \h -> do
line <- getLn h
if line == "Welcome to SMP v0.2.0 agent"