Skip to content
Browse files

Add basic.reject specs

  • Loading branch information...
1 parent 0d68d3b commit 5c1c6f3910f4f5ce333c52efaf33203922a2de3e @himura himura committed
Showing with 110 additions and 0 deletions.
  1. +107 −0 test/BasicRejectSpec.hs
  2. +3 −0 test/Runner.hs
View
107 test/BasicRejectSpec.hs
@@ -0,0 +1,107 @@
+{-# OPTIONS -XOverloadedStrings #-}
+
+module BasicRejectSpec (main, spec) where
+
+import Test.Hspec
+import Network.AMQP
+
+import Data.ByteString.Lazy.Char8 as L8
+import Control.Concurrent (threadDelay)
+import Control.Exception (bracket)
+
+main :: IO ()
+main = hspec spec
+
+withTestConnection :: (Channel -> IO c) -> IO c
+withTestConnection job = do
+ bracket (openConnection "127.0.0.1" "/" "guest" "guest") closeConnection $ \conn -> do
+ ch <- openChannel conn
+ job ch
+
+spec :: Spec
+spec = do
+ describe "rejectMsg" $ do
+ context "requeue = True" $ do
+ it "requeues a message" $ withTestConnection $ \ch -> do
+ let q = "haskell-amqp.basic.reject.with-requeue-true"
+
+ (_, n1, _) <- declareQueue ch $ newQueue {queueName = q, queueDurable = False}
+ n1 `shouldBe` 0
+
+ -- publishes using default exchange
+ publishMsg ch "" q $ newMsg {msgBody = (L8.pack "hello")}
+ threadDelay (1000 * 100)
+
+ (_, n2, _) <- declareQueue ch (newQueue {queueName = q, queueDurable = False})
+ n2 `shouldBe` 1
+
+ Just (_msg, env) <- getMsg ch Ack q
+ rejectMsg (envChannel env) (envDeliveryTag env) True
+ threadDelay (1000 * 100)
+
+ n3 <- deleteQueue ch q
+ n3 `shouldBe` 1
+
+ context "requeue = False" $ do
+ it "rejects a message" $ withTestConnection $ \ch -> do
+ let q = "haskell-amqp.basic.reject.with-requeue-false"
+
+ (_, n1, _) <- declareQueue ch $ newQueue {queueName = q, queueDurable = False}
+ n1 `shouldBe` 0
+
+ -- publishes using default exchange
+ publishMsg ch "" q $ newMsg {msgBody = (L8.pack "hello")}
+ threadDelay (1000 * 100)
+
+ (_, n2, _) <- declareQueue ch (newQueue {queueName = q, queueDurable = False})
+ n2 `shouldBe` 1
+
+ Just (_msg, env) <- getMsg ch Ack q
+ rejectMsg (envChannel env) (envDeliveryTag env) False
+ threadDelay (1000 * 100)
+
+ n3 <- deleteQueue ch q
+ n3 `shouldBe` 0
+
+ describe "rejectEnv" $ do
+ context "requeue = True" $ do
+ it "requeues a message" $ withTestConnection $ \ch -> do
+ let q = "haskell-amqp.basic.reject.with-requeue-true"
+
+ (_, n1, _) <- declareQueue ch $ newQueue {queueName = q, queueDurable = False}
+ n1 `shouldBe` 0
+
+ -- publishes using default exchange
+ publishMsg ch "" q $ newMsg {msgBody = (L8.pack "hello")}
+ threadDelay (1000 * 100)
+
+ (_, n2, _) <- declareQueue ch (newQueue {queueName = q, queueDurable = False})
+ n2 `shouldBe` 1
+
+ Just (_msg, env) <- getMsg ch Ack q
+ rejectEnv env True
+ threadDelay (1000 * 100)
+
+ n3 <- deleteQueue ch q
+ n3 `shouldBe` 1
+
+ context "requeue = False" $ do
+ it "rejects a message" $ withTestConnection $ \ch -> do
+ let q = "haskell-amqp.basic.reject.with-requeue-false"
+
+ (_, n1, _) <- declareQueue ch $ newQueue {queueName = q, queueDurable = False}
+ n1 `shouldBe` 0
+
+ -- publishes using default exchange
+ publishMsg ch "" q $ newMsg {msgBody = (L8.pack "hello")}
+ threadDelay (1000 * 100)
+
+ (_, n2, _) <- declareQueue ch (newQueue {queueName = q, queueDurable = False})
+ n2 `shouldBe` 1
+
+ Just (_msg, env) <- getMsg ch Ack q
+ rejectEnv env False
+ threadDelay (1000 * 100)
+
+ n3 <- deleteQueue ch q
+ n3 `shouldBe` 0
View
3 test/Runner.hs
@@ -9,6 +9,7 @@ import qualified ExchangeDeclareSpec
import qualified ExchangeDeleteSpec
import qualified BasicPublishSpec
+import qualified BasicRejectSpec
main :: IO ()
main = hspec $ do
@@ -28,3 +29,5 @@ main = hspec $ do
describe "QueuePurgeSpec" QueuePurgeSpec.spec
-- basic.publish
describe "BasicPublishSpec" BasicPublishSpec.spec
+ -- basic.reject
+ describe "BasicRejectSpec" BasicRejectSpec.spec

0 comments on commit 5c1c6f3

Please sign in to comment.
Something went wrong with that request. Please try again.