Skip to content

Commit

Permalink
Add basic.reject specs
Browse files Browse the repository at this point in the history
  • Loading branch information
himura committed Jan 5, 2014
1 parent 0d68d3b commit 5c1c6f3
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
107 changes: 107 additions & 0 deletions test/BasicRejectSpec.hs
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions test/Runner.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import qualified ExchangeDeclareSpec
import qualified ExchangeDeleteSpec

import qualified BasicPublishSpec
import qualified BasicRejectSpec

main :: IO ()
main = hspec $ do
Expand All @@ -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.