| 1 |
{-# LANGUAGE FlexibleInstances #-} |
| 2 |
{-# LANGUAGE OverloadedStrings #-} |
| 3 |
{-# LANGUAGE GADTs #-} |
| 4 |
|
| 5 |
module System.ZMQ4.Test.Properties where |
| 6 |
|
| 7 |
import Test.QuickCheck |
| 8 |
import Test.QuickCheck.Monadic (monadicIO, run) |
| 9 |
import Test.Tasty |
| 10 |
import Test.Tasty.HUnit |
| 11 |
import Test.Tasty.QuickCheck |
| 12 |
|
| 13 |
import Control.Applicative |
| 14 |
import Control.Concurrent.Async (wait) |
| 15 |
import Data.Int |
| 16 |
import Data.Word |
| 17 |
import Data.Restricted |
| 18 |
import Data.Maybe (fromJust) |
| 19 |
import Data.ByteString (ByteString) |
| 20 |
import System.ZMQ4.Monadic |
| 21 |
import System.Posix.Types (Fd(..)) |
| 22 |
import Prelude |
| 23 |
|
| 24 |
import qualified Data.ByteString as SB |
| 25 |
import qualified Data.ByteString.Char8 as CB |
| 26 |
import qualified Test.QuickCheck.Monadic as QM |
| 27 |
|
| 28 |
tests :: TestTree |
| 29 |
tests = testGroup "0MQ Socket Properties" |
| 30 |
[ testProperty "get socket option (Pair)" (prop_get_socket_option Pair) |
| 31 |
, testProperty "get socket option (Pub)" (prop_get_socket_option Pub) |
| 32 |
, testProperty "get socket option (Sub)" (prop_get_socket_option Sub) |
| 33 |
, testProperty "get socket option (XPub)" (prop_get_socket_option XPub) |
| 34 |
, testProperty "get socket option (XSub)" (prop_get_socket_option XSub) |
| 35 |
, testProperty "get socket option (Req)" (prop_get_socket_option Req) |
| 36 |
, testProperty "get socket option (Rep)" (prop_get_socket_option Rep) |
| 37 |
, testProperty "get socket option (Dealer)" (prop_get_socket_option Dealer) |
| 38 |
, testProperty "get socket option (Router)" (prop_get_socket_option Router) |
| 39 |
, testProperty "get socket option (Pull)" (prop_get_socket_option Pull) |
| 40 |
, testProperty "get socket option (Push)" (prop_get_socket_option Push) |
| 41 |
, testProperty "set;get socket option (Pair)" (prop_set_get_socket_option Pair) |
| 42 |
, testProperty "set;get socket option (Pub)" (prop_set_get_socket_option Pub) |
| 43 |
, testProperty "set;get socket option (Sub)" (prop_set_get_socket_option Sub) |
| 44 |
, testProperty "set;get socket option (XPub)" (prop_set_get_socket_option XPub) |
| 45 |
, testProperty "set;get socket option (XSub)" (prop_set_get_socket_option XSub) |
| 46 |
, testProperty "set;get socket option (Req)" (prop_set_get_socket_option Req) |
| 47 |
, testProperty "set;get socket option (Rep)" (prop_set_get_socket_option Rep) |
| 48 |
, testProperty "set;get socket option (Dealer)" (prop_set_get_socket_option Dealer) |
| 49 |
, testProperty "set;get socket option (Router)" (prop_set_get_socket_option Router) |
| 50 |
, testProperty "set;get socket option (Pull)" (prop_set_get_socket_option Pull) |
| 51 |
, testProperty "set;get socket option (Push)" (prop_set_get_socket_option Push) |
| 52 |
, testProperty "(un-)subscribe" (prop_subscribe Sub) |
| 53 |
, testCase "last_enpoint" (last_endpoint) |
| 54 |
, testGroup "connect disconnect" |
| 55 |
[ testProperty "" (prop_connect_disconnect x) |
| 56 |
| x <- [ (AnySocket Rep, AnySocket Req) |
| 57 |
, (AnySocket Router, AnySocket Req) |
| 58 |
, (AnySocket Pull, AnySocket Push) |
| 59 |
] |
| 60 |
] |
| 61 |
, testGroup "0MQ Messages" |
| 62 |
[ testProperty "msg send == msg received (Req/Rep)" (prop_send_receive Req Rep) |
| 63 |
, testProperty "msg send == msg received (Push/Pull)" (prop_send_receive Push Pull) |
| 64 |
, testProperty "msg send == msg received (Pair/Pair)" (prop_send_receive Pair Pair) |
| 65 |
-- , testProperty "publish/subscribe" (prop_pub_sub Pub Sub) |
| 66 |
-- (disabled due to LIBZMQ-270 [https://zeromq.jira.com/browse/LIBZMQ-270]) |
| 67 |
] |
| 68 |
] |
| 69 |
|
| 70 |
prop_get_socket_option :: SocketType t => t -> GetOpt -> Property |
| 71 |
prop_get_socket_option t opt = monadicIO $ run $ do |
| 72 |
runZMQ $ do |
| 73 |
s <- socket t |
| 74 |
case opt of |
| 75 |
Events _ -> events s >> return () |
| 76 |
Filedesc _ -> fileDescriptor s >> return () |
| 77 |
ReceiveMore _ -> moreToReceive s >> return () |
| 78 |
|
| 79 |
prop_set_get_socket_option :: SocketType t => t -> SetOpt -> Property |
| 80 |
prop_set_get_socket_option t opt = monadicIO $ do |
| 81 |
r <- run $ runZMQ $ do |
| 82 |
s <- socket t |
| 83 |
case opt of |
| 84 |
Identity val -> (== (rvalue val)) <$> (setIdentity val s >> identity s) |
| 85 |
Ipv4Only val -> (== val) <$> (setIpv4Only val s >> ipv4Only s) |
| 86 |
Affinity val -> (ieq val) <$> (setAffinity val s >> affinity s) |
| 87 |
Backlog val -> (ieq (rvalue val)) <$> (setBacklog val s >> backlog s) |
| 88 |
Linger val -> (ieq (rvalue val)) <$> (setLinger val s >> linger s) |
| 89 |
Rate val -> (ieq (rvalue val)) <$> (setRate val s >> rate s) |
| 90 |
ReceiveBuf val -> (ieq (rvalue val)) <$> (setReceiveBuffer val s >> receiveBuffer s) |
| 91 |
ReconnectIVL val -> (ieq (rvalue val)) <$> (setReconnectInterval val s >> reconnectInterval s) |
| 92 |
ReconnectIVLMax val -> (ieq (rvalue val)) <$> (setReconnectIntervalMax val s >> reconnectIntervalMax s) |
| 93 |
RecoveryIVL val -> (ieq (rvalue val)) <$> (setRecoveryInterval val s >> recoveryInterval s) |
| 94 |
SendBuf val -> (ieq (rvalue val)) <$> (setSendBuffer val s >> sendBuffer s) |
| 95 |
MaxMessageSize val -> (ieq (rvalue val)) <$> (setMaxMessageSize val s >> maxMessageSize s) |
| 96 |
McastHops val -> (ieq (rvalue val)) <$> (setMcastHops val s >> mcastHops s) |
| 97 |
ReceiveHighWM val -> (ieq (rvalue val)) <$> (setReceiveHighWM val s >> receiveHighWM s) |
| 98 |
ReceiveTimeout val -> (ieq (rvalue val)) <$> (setReceiveTimeout val s >> receiveTimeout s) |
| 99 |
SendHighWM val -> (ieq (rvalue val)) <$> (setSendHighWM val s >> sendHighWM s) |
| 100 |
SendTimeout val -> (ieq (rvalue val)) <$> (setSendTimeout val s >> sendTimeout s) |
| 101 |
ZapDomain val -> (== (rvalue val)) <$> (setZapDomain val s >> zapDomain s) |
| 102 |
PlainPassword val -> (== (rvalue val)) <$> (setPlainPassword val s >> plainPassword s) |
| 103 |
PlainUsername val -> (== (rvalue val)) <$> (setPlainUserName val s >> plainUserName s) |
| 104 |
QM.assert r |
| 105 |
where |
| 106 |
ieq :: (Integral i, Integral k) => i -> k -> Bool |
| 107 |
ieq i k = (fromIntegral i :: Int) == (fromIntegral k :: Int) |
| 108 |
|
| 109 |
last_endpoint :: IO () |
| 110 |
last_endpoint = do |
| 111 |
let a = "tcp://127.0.0.1:43821" |
| 112 |
a' <- runZMQ $ do |
| 113 |
s <- socket Rep |
| 114 |
bind s a |
| 115 |
lastEndpoint s |
| 116 |
a @=? a' |
| 117 |
|
| 118 |
prop_subscribe :: (Subscriber a, SocketType a) => a -> Bytes -> Property |
| 119 |
prop_subscribe t (Bytes subs) = monadicIO $ run $ |
| 120 |
runZMQ $ do |
| 121 |
s <- socket t |
| 122 |
subscribe s subs |
| 123 |
unsubscribe s subs |
| 124 |
|
| 125 |
prop_send_receive :: (SocketType a, SocketType b, Receiver b, Sender a) => a -> b -> Bytes -> Property |
| 126 |
prop_send_receive a b (Bytes msg) = monadicIO $ do |
| 127 |
msg' <- run $ runZMQ $ do |
| 128 |
sender <- socket a |
| 129 |
receiver <- socket b |
| 130 |
bind receiver "inproc://endpoint" |
| 131 |
x <- async $ receive receiver |
| 132 |
connect sender "inproc://endpoint" |
| 133 |
send sender [] msg |
| 134 |
liftIO $ wait x |
| 135 |
QM.assert (msg == msg') |
| 136 |
|
| 137 |
prop_pub_sub :: (SocketType a, Subscriber b, SocketType b, Sender a, Receiver b) => a -> b -> Bytes -> Property |
| 138 |
prop_pub_sub a b (Bytes msg) = monadicIO $ do |
| 139 |
msg' <- run $ runZMQ $ do |
| 140 |
pub <- socket a |
| 141 |
sub <- socket b |
| 142 |
subscribe sub "" |
| 143 |
bind sub "inproc://endpoint" |
| 144 |
connect pub "inproc://endpoint" |
| 145 |
send pub [] msg |
| 146 |
receive sub |
| 147 |
QM.assert (msg == msg') |
| 148 |
|
| 149 |
|
| 150 |
prop_connect_disconnect :: (AnySocket, AnySocket) -> Property |
| 151 |
prop_connect_disconnect (AnySocket t0, AnySocket t) = monadicIO $ run $ |
| 152 |
runZMQ $ do |
| 153 |
s0 <- socket t0 |
| 154 |
bind s0 "inproc://endpoint" |
| 155 |
s <- socket t |
| 156 |
connect s "inproc://endpoint" |
| 157 |
disconnect s "inproc://endpoint" |
| 158 |
|
| 159 |
newtype Bytes = Bytes { unbytes :: ByteString } deriving Show |
| 160 |
|
| 161 |
instance Arbitrary Bytes where |
| 162 |
arbitrary = Bytes . CB.filter (/= '\NUL') . CB.pack <$> arbitrary |
| 163 |
|
| 164 |
data GetOpt = |
| 165 |
Events Int |
| 166 |
| Filedesc Fd |
| 167 |
| ReceiveMore Bool |
| 168 |
deriving Show |
| 169 |
|
| 170 |
data SetOpt = |
| 171 |
Affinity Word64 |
| 172 |
| Backlog (Restricted (N0, Int32) Int) |
| 173 |
| Identity (Restricted (N1, N254) ByteString) |
| 174 |
| Ipv4Only Bool |
| 175 |
| Linger (Restricted (Nneg1, Int32) Int) |
| 176 |
| MaxMessageSize (Restricted (Nneg1, Int64) Int64) |
| 177 |
| McastHops (Restricted (N1, Int32) Int) |
| 178 |
| Rate (Restricted (N1, Int32) Int) |
| 179 |
| ReceiveBuf (Restricted (N0, Int32) Int) |
| 180 |
| ReceiveHighWM (Restricted (N0, Int32) Int) |
| 181 |
| ReceiveTimeout (Restricted (Nneg1, Int32) Int) |
| 182 |
| ReconnectIVL (Restricted (N0, Int32) Int) |
| 183 |
| ReconnectIVLMax (Restricted (N0, Int32) Int) |
| 184 |
| RecoveryIVL (Restricted (N0, Int32) Int) |
| 185 |
| SendBuf (Restricted (N0, Int32) Int) |
| 186 |
| SendHighWM (Restricted (N0, Int32) Int) |
| 187 |
| SendTimeout (Restricted (Nneg1, Int32) Int) |
| 188 |
| ZapDomain (Restricted (N1, N254) ByteString) |
| 189 |
| PlainPassword (Restricted (N1, N254) ByteString) |
| 190 |
| PlainUsername (Restricted (N1, N254) ByteString) |
| 191 |
deriving Show |
| 192 |
|
| 193 |
instance Arbitrary GetOpt where |
| 194 |
arbitrary = oneof [ |
| 195 |
Events <$> arbitrary |
| 196 |
, Filedesc . Fd . fromIntegral <$> (arbitrary :: Gen Int32) |
| 197 |
, ReceiveMore <$> arbitrary |
| 198 |
] |
| 199 |
|
| 200 |
instance Arbitrary SetOpt where |
| 201 |
arbitrary = oneof [ |
| 202 |
Affinity <$> (arbitrary :: Gen Word64) |
| 203 |
, Ipv4Only <$> (arbitrary :: Gen Bool) |
| 204 |
, Backlog . toR0 <$> (arbitrary :: Gen Int32) `suchThat` (>= 0) |
| 205 |
, Linger . toRneg1 <$> (arbitrary :: Gen Int32) `suchThat` (>= -1) |
| 206 |
, Rate . toR1 <$> (arbitrary :: Gen Int32) `suchThat` (> 0) |
| 207 |
, ReceiveBuf . toR0 <$> (arbitrary :: Gen Int32) `suchThat` (>= 0) |
| 208 |
, ReconnectIVL . toR0 <$> (arbitrary :: Gen Int32) `suchThat` (>= 0) |
| 209 |
, ReconnectIVLMax . toR0 <$> (arbitrary :: Gen Int32) `suchThat` (>= 0) |
| 210 |
, RecoveryIVL . toR0 <$> (arbitrary :: Gen Int32) `suchThat` (>= 0) |
| 211 |
, SendBuf . toR0 <$> (arbitrary :: Gen Int32) `suchThat` (>= 0) |
| 212 |
, McastHops . toR1 <$> (arbitrary :: Gen Int32) `suchThat` (> 0) |
| 213 |
, ReceiveHighWM . toR0 <$> (arbitrary :: Gen Int32) `suchThat` (>= 0) |
| 214 |
, ReceiveTimeout . toRneg1 <$> (arbitrary :: Gen Int32) `suchThat` (>= -1) |
| 215 |
, SendHighWM . toR0 <$> (arbitrary :: Gen Int32) `suchThat` (>= 0) |
| 216 |
, SendTimeout . toRneg1 <$> (arbitrary :: Gen Int32) `suchThat` (>= -1) |
| 217 |
, MaxMessageSize . toRneg1' <$> (arbitrary :: Gen Int64) `suchThat` (>= -1) |
| 218 |
, ZapDomain . fromJust . toRestricted <$> (unbytes <$> arbitrary) `suchThat` (\s -> SB.length s > 0 && SB.length s < 255) |
| 219 |
, PlainPassword . fromJust . toRestricted <$> (unbytes <$> arbitrary) `suchThat` (\s -> SB.length s > 0 && SB.length s < 255) |
| 220 |
, PlainUsername . fromJust . toRestricted <$> (unbytes <$> arbitrary) `suchThat` (\s -> SB.length s > 0 && SB.length s < 255) |
| 221 |
, Identity . fromJust . toRestricted <$> (unbytes <$> arbitrary) `suchThat` (\s -> SB.length s > 0 && SB.length s < 255) |
| 222 |
] |
| 223 |
|
| 224 |
toR1 :: Int32 -> Restricted (N1, Int32) Int |
| 225 |
toR1 = fromJust . toRestricted . fromIntegral |
| 226 |
|
| 227 |
toR0 :: Int32 -> Restricted (N0, Int32) Int |
| 228 |
toR0 = fromJust . toRestricted . fromIntegral |
| 229 |
|
| 230 |
toRneg1 :: Int32 -> Restricted (Nneg1, Int32) Int |
| 231 |
toRneg1 = fromJust . toRestricted . fromIntegral |
| 232 |
|
| 233 |
toRneg1' :: Int64 -> Restricted (Nneg1, Int64) Int64 |
| 234 |
toRneg1' = fromJust . toRestricted . fromIntegral |
| 235 |
|
| 236 |
data AnySocket where |
| 237 |
AnySocket :: SocketType a => a -> AnySocket |
| 238 |
|