Skip to content

Commit 35c98af

Browse files
committed
Merge branch 'master' into ep/smp-rcv-service
2 parents eab5ac0 + 96e8b4a commit 35c98af

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

src/Simplex/Messaging/Server/QueueStore/Postgres.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ instance StoreQueueClass q => QueueStoreClass q (PostgresQueueStore q) where
143143

144144
getEntityCounts :: PostgresQueueStore q -> IO EntityCounts
145145
getEntityCounts st =
146-
withConnection (dbStore st) $ \db -> do
146+
withTransaction (dbStore st) $ \db -> do
147147
(queueCount, notifierCount, rcvServiceCount, ntfServiceCount, rcvServiceQueuesCount, ntfServiceQueuesCount) : _ <-
148148
DB.query
149149
db
@@ -497,7 +497,7 @@ instance StoreQueueClass q => QueueStoreClass q (PostgresQueueStore q) where
497497

498498
batchInsertServices :: [STMService] -> PostgresQueueStore q -> IO Int64
499499
batchInsertServices services' toStore =
500-
withConnection (dbStore toStore) $ \db ->
500+
withTransaction (dbStore toStore) $ \db ->
501501
DB.executeMany db insertServiceQuery $ map (serviceRecToRow . serviceRec) services'
502502

503503
batchInsertQueues :: StoreQueueClass q => Bool -> M.Map RecipientId q -> PostgresQueueStore q' -> IO Int64
@@ -506,7 +506,7 @@ batchInsertQueues tty queues toStore = do
506506
putStrLn $ "Importing " <> show (length qs) <> " queues..."
507507
let st = dbStore toStore
508508
count <-
509-
withConnection st $ \db -> do
509+
withTransaction st $ \db -> do
510510
DB.copy_
511511
db
512512
[sql|
@@ -515,7 +515,7 @@ batchInsertQueues tty queues toStore = do
515515
|]
516516
mapM_ (putQueue db) (zip [1..] qs)
517517
DB.putCopyEnd db
518-
Only qCnt : _ <- withConnection st (`DB.query_` "SELECT count(*) FROM msg_queues")
518+
Only qCnt : _ <- withTransaction st (`DB.query_` "SELECT count(*) FROM msg_queues")
519519
putStrLn $ progress count
520520
pure qCnt
521521
where
@@ -542,7 +542,7 @@ insertServiceQuery =
542542

543543
foldServiceRecs :: Monoid a => PostgresQueueStore q -> (ServiceRec -> IO a) -> IO a
544544
foldServiceRecs st f =
545-
withConnection (dbStore st) $ \db ->
545+
withTransaction (dbStore st) $ \db ->
546546
DB.fold_ db "SELECT service_id, service_role, service_cert, service_cert_hash, created_at FROM services" mempty $
547547
\ !acc -> fmap (acc <>) . f . rowToServiceRec
548548

@@ -553,7 +553,7 @@ foldRcvServiceQueueRecs st serviceId f acc =
553553

554554
foldQueueRecs :: Monoid a => Bool -> Bool -> PostgresQueueStore q -> Maybe Int64 -> ((RecipientId, QueueRec) -> IO a) -> IO a
555555
foldQueueRecs tty withData st skipOld_ f = do
556-
(n, r) <- withConnection (dbStore st) $ \db ->
556+
(n, r) <- withTransaction (dbStore st) $ \db ->
557557
foldRecs db (0 :: Int, mempty) $ \(i, acc) qr -> do
558558
r <- f qr
559559
let !i' = i + 1
@@ -692,7 +692,7 @@ withDB' op st action = withDB op st $ fmap Right . action
692692

693693
withDB :: forall a q. Text -> PostgresQueueStore q -> (DB.Connection -> IO (Either ErrorType a)) -> ExceptT ErrorType IO a
694694
withDB op st action =
695-
ExceptT $ E.try (withConnection (dbStore st) action) >>= either logErr pure
695+
ExceptT $ E.try (withTransaction (dbStore st) action) >>= either logErr pure
696696
where
697697
logErr :: E.SomeException -> IO (Either ErrorType a)
698698
logErr e = logError ("STORE: " <> err) $> Left (STORE err)

tests/PostgresSchemaDump.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ postgresSchemaDumpTest migrations skipComparisonForDownMigrations testDBOpts@DBO
6565
void $ readCreateProcess (shell cmd) ""
6666
threadDelay 20000
6767
let sed = (if ci then "sed -i" else "sed -i ''")
68-
void $ readCreateProcess (shell $ sed <> " '/^--/d' " <> schemaPath) ""
68+
void $ readCreateProcess (shell $ sed <> " '/^--/d; /^\\\\restrict/d; /^\\\\unrestrict/d' " <> schemaPath) ""
6969
sch <- readFile schemaPath
7070
sch `deepseq` pure sch

tests/ServerTests.hs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ testTiming =
11491149
forM_ timingTests $ \tst ->
11501150
it (testName tst) $ \(ATransport t, msType) ->
11511151
smpTest2Cfg (cfgMS msType) (mkVersionRange minServerSMPRelayVersion authCmdsSMPVersion) t $ \rh sh ->
1152-
testSameTiming rh sh tst
1152+
testSameTiming rh sh tst msType
11531153
where
11541154
testName :: (C.AuthAlg, C.AuthAlg, Int) -> String
11551155
testName (C.AuthAlg goodKeyAlg, C.AuthAlg badKeyAlg, _) = unwords ["queue key:", show goodKeyAlg, "/ used key:", show badKeyAlg]
@@ -1166,11 +1166,16 @@ testTiming =
11661166
(C.AuthAlg C.SX25519, C.AuthAlg C.SX25519, 200) -- correct key type
11671167
]
11681168
timeRepeat n = fmap fst . timeItT . forM_ (replicate n ()) . const
1169-
similarTime t1 t2
1170-
| t1 <= t2 = abs (1 - t1 / t2) < 0.3 -- normally the difference between "no queue" and "wrong key" is less than 5%
1171-
| otherwise = similarTime t2 t1
1172-
testSameTiming :: forall c. Transport c => THandleSMP c 'TClient -> THandleSMP c 'TClient -> (C.AuthAlg, C.AuthAlg, Int) -> Expectation
1173-
testSameTiming rh sh (C.AuthAlg goodKeyAlg, C.AuthAlg badKeyAlg, n) = do
1169+
similarTime t1 t2 msType
1170+
| t1 <= t2 = abs (1 - t1 / t2) < diff
1171+
| otherwise = similarTime t2 t1 msType
1172+
where
1173+
-- normally the difference between "no queue" and "wrong key" is less than 5%, but it's higher on PostgreSQL and on CI
1174+
diff = case msType of
1175+
ASType SQSPostgres _ -> 0.45
1176+
_ -> 0.3
1177+
testSameTiming :: forall c. Transport c => THandleSMP c 'TClient -> THandleSMP c 'TClient -> (C.AuthAlg, C.AuthAlg, Int) -> AStoreType -> Expectation
1178+
testSameTiming rh sh (C.AuthAlg goodKeyAlg, C.AuthAlg badKeyAlg, n) msType = do
11741179
g <- C.newRandom
11751180
(rPub, rKey) <- atomically $ C.generateAuthKeyPair goodKeyAlg g
11761181
(dhPub, dhPriv :: C.PrivateKeyX25519) <- atomically $ C.generateKeyPair g
@@ -1205,7 +1210,7 @@ testTiming =
12051210
timeNoQueue <- timeRepeat n $ do
12061211
Resp "dabc" _ (ERR AUTH) <- signSendRecv h badKey ("dabc", EntityId "1234", cmd)
12071212
return ()
1208-
let ok = similarTime timeNoQueue timeWrongKey
1213+
let ok = similarTime timeNoQueue timeWrongKey msType
12091214
unless ok . putStrLn . unwords $
12101215
[ show goodKeyAlg,
12111216
show badKeyAlg,

tests/Test.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ main = do
9595
describe "Agent core tests" agentCoreTests
9696
#if defined(dbServerPostgres)
9797
around_ (postgressBracket testServerDBConnectInfo) $
98-
describe "SMP server schema dump" $
98+
fdescribe "SMP server schema dump" $
9999
postgresSchemaDumpTest
100100
serverMigrations
101101
[ "20250320_short_links" -- snd_secure moves to the bottom on down migration
@@ -116,7 +116,7 @@ main = do
116116
-- before (pure (transport @WS, ASType SQSMemory SMSJournal)) serverTests
117117
#if defined(dbServerPostgres)
118118
around_ (postgressBracket ntfTestServerDBConnectInfo) $
119-
describe "Ntf server schema dump" $
119+
fdescribe "Ntf server schema dump" $
120120
postgresSchemaDumpTest
121121
ntfServerMigrations
122122
[] -- skipComparisonForDownMigrations

0 commit comments

Comments
 (0)