From 402548eb553a4aaffdd3570ae81efcaa5b3fe2e2 Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:26:45 +0000 Subject: [PATCH] tests: accept agent events from independent threads in any order (#2032) * tests: accept UP and MSG in either order when subscribing * tests: add the type of the UP and MSG reader * tests: accept SENT of two connections in any order --- tests/AgentTests/FunctionalAPITests.hs | 27 +++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index 85880041d..2e1cac849 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -72,7 +72,7 @@ import Data.Bifunctor (first) import qualified Data.ByteString.Base64 as B64 import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B -import Data.Either (isRight) +import Data.Either (isRight, partitionEithers) import Data.Int (Int64) import Data.List (find, isInfixOf, isPrefixOf, isSuffixOf) import Data.List.NonEmpty (NonEmpty (..)) @@ -1018,10 +1018,14 @@ testAgentClient3 = 2 <- sendMessage a cId SMP.noMsgFlags "c4" 3 <- sendMessage a bId SMP.noMsgFlags "b5" 3 <- sendMessage a cId SMP.noMsgFlags "c5" - get a =##> \case ("", connId, SENT 2) -> connId == bId || connId == cId; _ -> False - get a =##> \case ("", connId, SENT 2) -> connId == bId || connId == cId; _ -> False - get a =##> \case ("", connId, SENT 3) -> connId == bId || connId == cId; _ -> False - get a =##> \case ("", connId, SENT 3) -> connId == bId || connId == cId; _ -> False + liftIO $ + getInAnyOrder + a + [ \case ("", connId, AEvt SAEConn (SENT 2)) -> connId == bId; _ -> False, + \case ("", connId, AEvt SAEConn (SENT 2)) -> connId == cId; _ -> False, + \case ("", connId, AEvt SAEConn (SENT 3)) -> connId == bId; _ -> False, + \case ("", connId, AEvt SAEConn (SENT 3)) -> connId == cId; _ -> False + ] get b =##> \case ("", connId, Msg "b4") -> connId == aIdForB; _ -> False ackMessage b aIdForB 2 Nothing get b =##> \case ("", connId, Msg "b5") -> connId == aIdForB; _ -> False @@ -3282,15 +3286,20 @@ testBatchedPendingMessages nCreate nMsgs = withB $ \b -> runRight_ $ do let aIds = map fst conns subscribeAllConnections b False Nothing - ("", "", UP _ aIds') <- nGet b - liftIO $ S.fromList aIds' `shouldBe` S.fromList aIds - replicateM_ nMsgs $ do - ("", cId, Msg' msgId _ "hello") <- get b + (ups, msgs) <- partitionEithers <$> replicateM (nMsgs + 1) (upOrMsg b) + liftIO $ map S.fromList ups `shouldBe` [S.fromList aIds] + forM_ msgs $ \(cId, msgId) -> do liftIO $ isJust (find ((cId ==) . fst) msgConns) `shouldBe` True ackMessage b cId msgId Nothing where withA = withAgent 1 agentCfg initAgentServers testDB withB = withAgent 2 agentCfg initAgentServers testDB2 + upOrMsg :: AgentClient -> ExceptT AgentErrorType IO (Either [ConnId] (ConnId, AgentMsgId)) + upOrMsg b = + pGet b >>= \case + ("", "", AEvt SAENone (UP _ aIds')) -> pure $ Left aIds' + ("", cId, AEvt SAEConn (Msg' msgId _ "hello")) -> pure $ Right (cId, msgId) + r -> error $ "unexpected event: " <> show r testSendMessagesB :: HasTestEnv => IO () testSendMessagesB = withAgentClients2 $ \a b -> runRight_ $ do