mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-11-22 19:58:39 +00:00
smp server: decrease service subscription count on DEL and SUBS (#2046)
* tests: service subscription counts after queue deletion * smp server: decrease service subscription count on DEL and SUBS * tests: service subscription count after the client service changes * smp server: on DEL, decrease the count of the queue's own service only
This commit is contained in:
1 parent
71bb0d34a4
commit
f16343ef2a
2 files changed
+82
-2
No files matched your search
@@ -1950,7 +1950,7 @@ client
|
||||
subsChange <- atomically $ do
|
||||
writeTVar (clientServiceSubscribed clnt) True
|
||||
currSubs <- swapTVar (clientServiceSubs clnt) subs'
|
||||
pure $ subtractServiceSubs currSubs subs'
|
||||
pure (count' - fst currSubs, idsHash' <> snd currSubs)
|
||||
let incSrvStat sel n = liftIO $ atomicModifyIORef'_ (sel $ servicesSel stats) (+ n)
|
||||
diff = fromIntegral $ count' - count
|
||||
if -- `count == -1` only for subscriptions by old NTF servers
|
||||
@@ -2256,7 +2256,9 @@ client
|
||||
-- we delete subscription here, so the client with no subscriptions can be disconnected.
|
||||
sub <- atomically $ TM.lookupDelete entId $ subscriptions clnt
|
||||
liftIO $ mapM_ cancelSub sub
|
||||
when (isJust rcvServiceId) $ atomically $ modifyTVar' (serviceSubsCount clnt) $ subtractServiceSubs (1, queueIdHash (recipientId q))
|
||||
when (isJust rcvServiceId && rcvServiceId == clntServiceId) $ atomically $ whenM (((0 /=) . fst) <$> readTVar (serviceSubsCount clnt)) $ do
|
||||
modifyTVar' (serviceSubsCount clnt) $ subtractServiceSubs (1, queueIdHash (recipientId q))
|
||||
modifyTVar' (totalServiceSubs subscribers) $ subtractServiceSubs (1, queueIdHash (recipientId q))
|
||||
liftIO $ processSub clnt $ CSDeleted entId rcvServiceId
|
||||
forM_ (notifier qr) $ \NtfCreds {notifierId = nId, ntfServiceId} -> do
|
||||
-- queue is deleted by a different client from the one subscribed to notifications,
|
||||
|
||||
@@ -100,6 +100,9 @@ serverTests = do
|
||||
testServiceDeliverSubscribe
|
||||
testServiceUpgradeAndDowngrade
|
||||
testServiceSubsTotalCount
|
||||
testServiceSubsTotalCountDeleted
|
||||
testServiceSubsTotalCountOperatorDeleted
|
||||
testServiceSubsTotalCountOtherServiceDeleted
|
||||
testServiceSubSendBlock
|
||||
testServiceSubAssociatedQueue
|
||||
testServiceDowngradeConnected
|
||||
@@ -1149,6 +1152,81 @@ testServiceSubsTotalCount =
|
||||
threadDelay 1500000
|
||||
B.readFile testPrometheusMetricsFile >>= \m -> readServiceSubsMetric (B.unpack m) `shouldBe` Just 3
|
||||
|
||||
testServiceSubsTotalCountDeleted :: SpecWith (ASrvTransport, AStoreType)
|
||||
testServiceSubsTotalCountDeleted =
|
||||
it "should subtract the queue deleted by the service from totalServiceSubs" $ \(at@(ATransport t), msType) -> do
|
||||
g <- C.newRandom
|
||||
creds <- genCredentials g Nothing (0, 2400) "localhost"
|
||||
let (_fp, tlsCred) = tlsCredentials [creds]
|
||||
serviceKeys@(_, servicePK) <- atomically $ C.generateKeyPair g
|
||||
(rPub, rKey) <- atomically $ C.generateAuthKeyPair C.SEd25519 g
|
||||
(dhPub, _ :: C.PrivateKeyX25519) <- atomically $ C.generateKeyPair g
|
||||
let cfg' = updateCfg (cfgMS msType) $ \cfg_ -> cfg_ {prometheusInterval = Just 1}
|
||||
serviceSubsMetric n = threadDelay 1500000 >> B.readFile testPrometheusMetricsFile >>= \m -> readServiceSubsMetric (B.unpack m) `shouldBe` Just n
|
||||
withSmpServerConfigOn at cfg' testPort $ \_ -> do
|
||||
runSMPServiceClient t (tlsCred, serviceKeys) $ \sh -> do
|
||||
Resp "1" NoEntity (Ids_ rId1 _ _ _) <- serviceSignSendRecv sh rKey servicePK ("1", NoEntity, New rPub dhPub)
|
||||
Resp "2" NoEntity (IDS _) <- serviceSignSendRecv sh rKey servicePK ("2", NoEntity, New rPub dhPub)
|
||||
serviceSubsMetric 2
|
||||
Resp "3" _ OK <- signSendRecv sh rKey ("3", rId1, DEL)
|
||||
serviceSubsMetric 1
|
||||
serviceSubsMetric 0
|
||||
|
||||
testServiceSubsTotalCountOperatorDeleted :: SpecWith (ASrvTransport, AStoreType)
|
||||
testServiceSubsTotalCountOperatorDeleted =
|
||||
it "should reduce totalServiceSubs to the store count on SUBS after the operator deletes a queue" $ \(at@(ATransport t), msType) -> do
|
||||
g <- C.newRandom
|
||||
creds <- genCredentials g Nothing (0, 2400) "localhost"
|
||||
let (_fp, tlsCred) = tlsCredentials [creds]
|
||||
serviceKeys@(_, servicePK) <- atomically $ C.generateKeyPair g
|
||||
(rPub, rKey) <- atomically $ C.generateAuthKeyPair C.SEd25519 g
|
||||
(dhPub, _ :: C.PrivateKeyX25519) <- atomically $ C.generateKeyPair g
|
||||
let aServicePK = C.APrivateAuthKey C.SEd25519 servicePK
|
||||
cpPort = testServerPort 8
|
||||
cfg' = updateCfg (cfgMS msType) $ \cfg_ -> cfg_ {prometheusInterval = Just 1, controlPort = Just cpPort, controlPortAdminAuth = Just "admin"}
|
||||
serviceSubsMetric n = threadDelay 1500000 >> B.readFile testPrometheusMetricsFile >>= \m -> readServiceSubsMetric (B.unpack m) `shouldBe` Just n
|
||||
withSmpServerConfigOn at cfg' testPort $ \_ -> runSMPServiceClient t (tlsCred, serviceKeys) $ \sh -> do
|
||||
Resp "1" NoEntity (Ids_ rId1 _ _ serviceId) <- serviceSignSendRecv sh rKey servicePK ("1", NoEntity, New rPub dhPub)
|
||||
Resp "2" NoEntity (Ids_ rId2 sId2 _ _) <- serviceSignSendRecv sh rKey servicePK ("2", NoEntity, New rPub dhPub)
|
||||
serviceSubsMetric 2
|
||||
[_, deleted] <- controlPortCommands cpPort ["auth admin", "delete " <> B.unpack (strEncode sId2)]
|
||||
deleted `shouldBe` "ok, 0 messages deleted"
|
||||
signSend_ sh aServicePK Nothing ("3", serviceId, SUBS 2 (queueIdsHash [rId1, rId2]))
|
||||
void $
|
||||
receiveInAnyOrder sh
|
||||
[ \case
|
||||
Resp "3" _ (SOKS n idsHash') -> Just () <$ ((n, idsHash') `shouldBe` (1, queueIdsHash [rId1]))
|
||||
_ -> pure Nothing,
|
||||
\case
|
||||
Resp "" NoEntity ALLS -> pure $ Just ()
|
||||
_ -> pure Nothing
|
||||
]
|
||||
serviceSubsMetric 1
|
||||
|
||||
testServiceSubsTotalCountOtherServiceDeleted :: SpecWith (ASrvTransport, AStoreType)
|
||||
testServiceSubsTotalCountOtherServiceDeleted =
|
||||
it "should keep totalServiceSubs when the queue of the previous service is deleted from the new service" $ \(at@(ATransport t), msType) -> do
|
||||
g <- C.newRandom
|
||||
prevCreds <- genCredentials g Nothing (0, 2400) "localhost"
|
||||
creds <- genCredentials g Nothing (0, 2400) "localhost"
|
||||
let (_, prevTlsCred) = tlsCredentials [prevCreds]
|
||||
(_, tlsCred) = tlsCredentials [creds]
|
||||
prevServiceKeys@(_, prevServicePK) <- atomically $ C.generateKeyPair g
|
||||
serviceKeys@(_, servicePK) <- atomically $ C.generateKeyPair g
|
||||
(rPub, rKey) <- atomically $ C.generateAuthKeyPair C.SEd25519 g
|
||||
(dhPub, _ :: C.PrivateKeyX25519) <- atomically $ C.generateKeyPair g
|
||||
let cfg' = updateCfg (cfgMS msType) $ \cfg_ -> cfg_ {prometheusInterval = Just 1}
|
||||
serviceSubsMetric n = threadDelay 1500000 >> B.readFile testPrometheusMetricsFile >>= \m -> readServiceSubsMetric (B.unpack m) `shouldBe` Just n
|
||||
withSmpServerConfigOn at cfg' testPort $ \_ -> do
|
||||
prevRId <- runSMPServiceClient t (prevTlsCred, prevServiceKeys) $ \sh -> do
|
||||
Resp "1" NoEntity (Ids_ prevRId _ _ _) <- serviceSignSendRecv sh rKey prevServicePK ("1", NoEntity, New rPub dhPub)
|
||||
pure prevRId
|
||||
runSMPServiceClient t (tlsCred, serviceKeys) $ \sh -> do
|
||||
Resp "2" NoEntity (IDS _) <- serviceSignSendRecv sh rKey servicePK ("2", NoEntity, New rPub dhPub)
|
||||
Resp "3" _ OK <- signSendRecv sh rKey ("3", prevRId, DEL)
|
||||
serviceSubsMetric 1
|
||||
serviceSubsMetric 0
|
||||
|
||||
testServiceSubSendBlock :: SpecWith (ASrvTransport, AStoreType)
|
||||
testServiceSubSendBlock =
|
||||
it "should deliver message sent in the same block as service SUB" $ \(at@(ATransport t), msType) -> do
|
||||
|
||||
Reference in new issue
Block a user