Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Simplex/Messaging/Agent/Store/AgentStore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,7 @@ deleteDeliveredSndMsg db connId msgId = do

deleteSndMsgDelivery :: DB.Connection -> ConnId -> SndQueue -> InternalId -> Bool -> IO ()
deleteSndMsgDelivery db connId SndQueue {dbQueueId} msgId keepForReceipt = do
lockConnForUpdate db connId
DB.execute
db
"DELETE FROM snd_message_deliveries WHERE conn_id = ? AND snd_queue_id = ? AND internal_id = ?"
Expand Down
3 changes: 2 additions & 1 deletion tests/AgentTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Simplex.Messaging.Transport (ASrvTransport)
import Test.Hspec hiding (fit, it)

#if defined(dbPostgres)
import AgentTests.FunctionalAPITests (workItemReadTests)
import AgentTests.FunctionalAPITests (connectionDeletionTests, workItemReadTests)
#else
import AgentTests.SQLiteTests (storeTests)
#endif
Expand All @@ -38,6 +38,7 @@ agentCoreTests = do
describe "resolve names" resolveNameTests
#if defined(dbPostgres)
describe "Work item reads" workItemReadTests
describe "Connection deletion" connectionDeletionTests
#endif

agentTests :: (ASrvTransport, AStoreType) -> Spec
Expand Down
52 changes: 50 additions & 2 deletions tests/AgentTests/FunctionalAPITests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ module AgentTests.FunctionalAPITests
testServerInformation,
#if defined(dbPostgres)
workItemReadTests,
connectionDeletionTests,
#endif
)
where
Expand Down Expand Up @@ -140,9 +141,10 @@ import Database.PostgreSQL.Simple (Only (..), Query)
import Fixtures
import Simplex.Messaging.Agent.Client (withStoreIsolation', withWork, withWorkItems)
import Simplex.Messaging.Agent.Env.SQLite (AM)
import Simplex.Messaging.Agent.Store (AgentCommand (AClientCommand), RcvQueue, RcvQueueSub (..), ServiceAssoc)
import Simplex.Messaging.Agent.Store.AgentStore (createCommand, createNtfSubscription, createServer, createSndFile, deleteClientService, getNextNtfSubNTFActions, getNextNtfSubSMPActions, getNextSndFileToPrepare, getPendingServerCommand, getSubscriptionService, getUserServerRcvQueueSubs, removeRcvServiceAssocs, setRcvServiceAssocs, startPreparedSndFile)
import Simplex.Messaging.Agent.Store (AgentCommand (AClientCommand), InternalId (..), RcvQueue, RcvQueueSub (..), ServiceAssoc)
import Simplex.Messaging.Agent.Store.AgentStore (createCommand, createNtfSubscription, createServer, createSndFile, deleteClientService, deleteConn, deleteSndMsgDelivery, getNextNtfSubNTFActions, getNextNtfSubSMPActions, getNextSndFileToPrepare, getPendingServerCommand, getSubscriptionService, getUserServerRcvQueueSubs, removeRcvServiceAssocs, setRcvServiceAssocs, startPreparedSndFile)
import Simplex.Messaging.Agent.Store.Common (IsolationLevel (..))
import Simplex.Messaging.Agent.Store.Entity (DBEntityId' (..))
import Simplex.Messaging.Notifications.Types (NtfAgentSubStatus (NASNew), NtfSubAction (NSANtf, NSASMP), NtfSubNTFAction (NSACreate), NtfSubSMPAction (NSASmpKey), newNtfSubscription)
#endif
#if defined(dbServerPostgres)
Expand Down Expand Up @@ -5173,6 +5175,52 @@ waitForLock st holder waitingQuery = timeout 5000000 waitLoop `shouldReturn` Jus
withTransaction st $ \db ->
DB.query db "SELECT EXISTS (SELECT 1 FROM pg_stat_activity WHERE ? = ANY (pg_blocking_pids(pid)) AND position(? IN query) > 0)" (holder, waitingQuery)
unless waiting $ threadDelay 10000 >> waitLoop

connectionDeletionTests :: Spec
connectionDeletionTests =
it "should delete a connection while the delivery of its sent message is deleted" $
withAgent 1 agentCfg initAgentServers testDB $ \a -> do
let st = store $ agentEnv a
connId = "conn1"
g <- C.newRandom
sndPrivateKey <- atomically $ C.generatePrivateAuthKey C.SEd25519 g
(dhPubKey, dhPrivKey) <- atomically $ C.generateKeyPair g
let sq =
SndQueue
{ userId = 1,
connId,
server = testSMPServer,
sndId = SMP.EntityId "1",
queueMode = Nothing,
sndPrivateKey,
e2ePubKey = Nothing,
e2eDhSecret = C.dh' dhPubKey dhPrivKey,
status = Active,
dbQueueId = DBEntityId 1,
primary = True,
dbReplaceQueueId = Nothing,
sndSwchStatus = Nothing,
smpClientVersion = Version 1
}
withTransaction st $ \db -> do
DB.execute db "INSERT INTO connections (user_id, conn_id, conn_mode) VALUES (1,?,?)" (connId, SCMInvitation)
DB.execute db "INSERT INTO messages (conn_id, internal_id, internal_ts, internal_snd_id, msg_type) VALUES (?,1,now(),1,'')" (Only connId)
DB.execute db "INSERT INTO snd_messages (conn_id, internal_snd_id, internal_id, internal_hash) VALUES (?,1,1,'')" (Only connId)
DB.execute db "INSERT INTO snd_message_deliveries (conn_id, snd_queue_id, internal_id) VALUES (?,1,1)" (Only connId)
(delivery, deletion) <- withTransaction st $ \db -> do
_ :: [Only Int] <- DB.query_ db "SELECT 1 FROM snd_messages FOR UPDATE"
[Only holder] <- DB.query_ db "SELECT pg_backend_pid()"
deliveryPid <- newEmptyMVar
delivery <- async $ withTransaction st $ \db' -> do
[Only pid] <- DB.query_ db' "SELECT pg_backend_pid()"
putMVar deliveryPid pid
deleteSndMsgDelivery db' connId sq (InternalId 1) False
waitForLock st holder "FROM snd_messages"
deletion <- async $ withTransaction st $ \db' -> deleteConn db' Nothing connId
readMVar deliveryPid >>= \pid -> waitForLock st pid "DELETE FROM connections"
pure (delivery, deletion)
wait delivery `shouldReturn` ()
wait deletion `shouldReturn` Just connId
#endif

testClientServiceConnection :: (HasCallStack, HasTestEnv) => (ASrvTransport, AStoreType) -> IO ()
Expand Down
Loading