Skip to content

Commit e634a09

Browse files
solomon-bTristanCacqueray
authored andcommitted
Fixes Test Suite
1 parent b08572e commit e634a09

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

matrix-client/src/Network/Matrix/Internal.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import System.IO (stderr)
3434
import Control.Monad.Except
3535
import Control.Monad.Catch.Pure
3636
import Control.Monad.Reader
37-
import Data.Coerce
3837

3938
newtype MatrixToken = MatrixToken Text
4039
newtype Username = Username { username :: Text }
@@ -176,6 +175,7 @@ newtype MatrixM m a = MatrixM { unMatrixM :: ExceptT MatrixError (ReaderT Client
176175
, Applicative
177176
, Monad
178177
, MonadError MatrixError
178+
, MonadFail
179179
, MonadIO
180180
, MonadThrow
181181
, MonadCatch

matrix-client/test/Spec.hs

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
-- | The matrix client specification tests
55
module Main (main) where
66

7-
import Control.Monad (void)
87
import qualified Data.Aeson.Encode.Pretty as Aeson
98
import qualified Data.ByteString.Lazy as BS
109
import Data.Either (isLeft)
@@ -14,6 +13,7 @@ import Network.Matrix.Client
1413
import Network.Matrix.Internal
1514
import System.Environment (lookupEnv)
1615
import Test.Hspec
16+
import Control.Monad.Except
1717

1818
main :: IO ()
1919
main = do
@@ -32,9 +32,8 @@ integration :: ClientSession -> ClientSession -> Spec
3232
integration sess1 sess2 = do
3333
describe "integration tests" $ do
3434
it "create room" $ do
35-
resp <-
35+
resp <- runMatrixM sess1 $ do
3636
createRoom
37-
sess1
3837
( RoomCreateRequest
3938
{ rcrPreset = PublicChat,
4039
rcrRoomAliasName = "test",
@@ -44,25 +43,51 @@ integration sess1 sess2 = do
4443
)
4544
case resp of
4645
Left err -> meError err `shouldBe` "Alias already exists"
47-
Right (RoomID roomID) -> roomID `shouldSatisfy` (/= mempty)
48-
it "join room" $ do
49-
resp <- joinRoom sess1 "#test:localhost"
46+
Right (RoomID roomID') -> roomID' `shouldSatisfy` (/= mempty)
47+
it "join room" $ do
48+
resp <- runMatrixM sess1 $joinRoom "#test:localhost"
5049
case resp of
5150
Left err -> error (show err)
52-
Right (RoomID roomID) -> roomID `shouldSatisfy` (/= mempty)
53-
resp' <- joinRoom sess2 "#test:localhost"
51+
Right (RoomID roomID') -> roomID' `shouldSatisfy` (/= mempty)
52+
resp' <- runMatrixM sess2 $ joinRoom "#test:localhost"
5453
case resp' of
5554
Left err -> error (show err)
56-
Right (RoomID roomID) -> roomID `shouldSatisfy` (/= mempty)
55+
Right (RoomID roomID') -> roomID' `shouldSatisfy` (/= mempty)
5756
it "send message and reply" $ do
58-
-- Flush previous events
59-
Right sr <- sync sess2 Nothing Nothing Nothing Nothing
60-
Right [room] <- getJoinedRooms sess1
61-
let msg body = RoomMessageText $ MessageText body TextType Nothing Nothing
62-
let since = srNextBatch sr
63-
Right eventID <- sendMessage sess1 room (EventRoomMessage $ msg "Hello") (TxnID since)
64-
Right reply <- sendMessage sess2 room (EventRoomReply eventID $ msg "Hi!") (TxnID since)
65-
reply `shouldNotBe` eventID
57+
result <- runMatrixM sess2 $ do
58+
-- Flush previous events
59+
sr <- sync Nothing Nothing Nothing Nothing
60+
[room] <- getJoinedRooms
61+
let msg body = RoomMessageText $ MessageText body TextType Nothing Nothing
62+
let since = srNextBatch sr
63+
eventID <- sendMessage room (EventRoomMessage $ msg "Hello") (TxnID since)
64+
reply <- sendMessage room (EventRoomReply eventID $ msg "Hi!") (TxnID since)
65+
pure (reply, eventID)
66+
case result of
67+
Left err -> error (show err)
68+
Right (reply, eventID) -> reply `shouldNotBe` eventID
69+
it "does not retry on success" $
70+
checkPause (<=) $ do
71+
res <- runMatrixM sess1 $ retry (pure True)
72+
res `shouldBe` pure True
73+
it "does not retry on regular failure" $
74+
checkPause (<=) $ do
75+
let resp = MatrixError "test" "error" Nothing
76+
res <- runMatrixM sess1 $ retry (throwError resp :: MatrixIO Int)
77+
res `shouldBe` Left resp
78+
it "retry on rate limit failure" $
79+
checkPause (>=) $ do
80+
let resp = MatrixError "M_LIMIT_EXCEEDED" "error" (Just 1000)
81+
(runMatrixM sess1 $ retryWithLog 1 (const $ pure ()) (throwError resp))
82+
`shouldThrow` rateLimitSelector
83+
where
84+
rateLimitSelector :: MatrixException -> Bool
85+
rateLimitSelector MatrixRateLimit = True
86+
checkPause op action = do
87+
MkSystemTime start' _ <- getSystemTime
88+
void action
89+
MkSystemTime end' _ <- getSystemTime
90+
(end' - start') `shouldSatisfy` (`op` 1)
6691

6792
spec :: Spec
6893
spec = describe "unit tests" $ do
@@ -93,29 +118,7 @@ spec = describe "unit tests" $ do
93118
it "encode room message" $
94119
encodePretty (RoomMessageText (MessageText "Hello" TextType Nothing Nothing))
95120
`shouldBe` "{\"body\":\"Hello\",\"msgtype\":\"m.text\"}"
96-
it "does not retry on success" $
97-
checkPause (<=) $ do
98-
let resp = Right True
99-
res <- retry (pure resp)
100-
res `shouldBe` resp
101-
it "does not retry on regular failre" $
102-
checkPause (<=) $ do
103-
let resp = Left $ MatrixError "test" "error" Nothing
104-
res <- (retry (pure resp) :: MatrixIO Int)
105-
res `shouldBe` resp
106-
it "retry on rate limit failure" $
107-
checkPause (>=) $ do
108-
let resp = Left $ MatrixError "M_LIMIT_EXCEEDED" "error" (Just 1000)
109-
(retryWithLog 1 (const $ pure ()) (pure resp) :: MatrixIO Int)
110-
`shouldThrow` rateLimitSelector
111121
where
112-
rateLimitSelector :: MatrixException -> Bool
113-
rateLimitSelector MatrixRateLimit = True
114-
checkPause op action = do
115-
MkSystemTime start _ <- getSystemTime
116-
void action
117-
MkSystemTime end _ <- getSystemTime
118-
(end - start) `shouldSatisfy` (`op` 1)
119122
encodePretty =
120123
Aeson.encodePretty'
121124
( Aeson.defConfig {Aeson.confIndent = Aeson.Spaces 0, Aeson.confCompare = compare @Text}

0 commit comments

Comments
 (0)