@@ -27,11 +27,12 @@ import qualified Data.Map as Map
27import Control.Category ((>>>))
27import Control.Category ((>>>))
28import Control.Arrow (second)
28import Control.Arrow (second)
29import Control.Applicative ((<$>))
29import Control.Applicative ((<$>))
30−import Control.Exception (throwIO)
30+import Control.Exception (Exception, throwIO, try)
31import Control.Concurrent (forkIO, threadDelay, ThreadId, killThread)
31import Control.Concurrent (forkIO, threadDelay, ThreadId, killThread)
32import Control.Monad (replicateM, forever, guard)
32import Control.Monad (replicateM, forever, guard)
33import Control.Monad.State.Lazy (StateT, execStateT)
33import Control.Monad.State.Lazy (StateT, execStateT)
34−import Control.Monad.IO.Class (liftIO)
34+import Control.Monad.IO.Class (MonadIO, liftIO)
35+import Data.Typeable (Typeable)
35import Data.Maybe (isJust)
36import Data.Maybe (isJust)
36import Data.List (inits)
37import Data.List (inits)
37import Data.ByteString (ByteString)
38import Data.ByteString (ByteString)
@@ -48,11 +49,9 @@ import Network.Transport
48import Network.Transport.TCP
49import Network.Transport.TCP
49 ( createTransportExposeInternals
50 ( createTransportExposeInternals
50 , defaultTCPParameters
51 , defaultTCPParameters
51− , TransportInternals(socketBetween)
52+ , TransportInternals
54−import Network.Transport.TCP.Mock.Socket (scheduleReadAction, sClose)
56--------------------------------------------------------------------------------
55--------------------------------------------------------------------------------
57-- Script infrastructure --
56-- Script infrastructure --
58--------------------------------------------------------------------------------
57--------------------------------------------------------------------------------
@@ -124,7 +123,7 @@ initialRunState = RunState {
126verify :: (Transport, TransportInternals) -> Script -> IO (Either String ())
125verify :: (Transport, TransportInternals) -> Script -> IO (Either String ())
127−verify (transport, transportInternals) script = do
126+verify (transport, _transportInternals) script = do
128 allEvents <- newQ
127 allEvents <- newQ
130 let runScript :: Script -> StateT RunState IO ()
129 let runScript :: Script -> StateT RunState IO ()
@@ -162,6 +161,8 @@ verify (transport, transportInternals) script = do
162 Right () -> return ()
161 Right () -> return ()
163 Left err -> liftIO $ throwIO err
162 Left err -> liftIO $ throwIO err
164 modify (expectedEventsAt target) (snoc (ExpReceived connId payload))
163 modify (expectedEventsAt target) (snoc (ExpReceived connId payload))
164+ runCmd (BreakAfterReads _n _i _j) =
165+ expectedFailure "BreakAfterReads not implemented"
166 forward :: EndPoint -> IO ()
167 forward :: EndPoint -> IO ()
167 forward endPoint = forever $ do
168 forward endPoint = forever $ do
@@ -493,20 +494,30 @@ testScriptGen transport scriptGen =
493 forAll scriptGen $ \script ->
494 forAll scriptGen $ \script ->
494 morallyDubiousIOProperty $ do
495 morallyDubiousIOProperty $ do
495 logShow script
496 logShow script
496− mErr <- verify transport script
497+ mErr <- try $ verify transport script
497 return $ case mErr of
498 return $ case mErr of
499+ Left (ExpectedFailure str) ->
498− Right () -> result { ok = Just True }
500+ result { ok = Nothing
499− Left err -> result { ok = Just False
504+ result { ok = Just False
500− , reason = '\n' : err ++ "\n"
505+ , reason = '\n' : err ++ "\n"
508+ result { ok = Just True }
503testScript :: (Transport, TransportInternals) -> Script -> Assertion
510testScript :: (Transport, TransportInternals) -> Script -> Assertion
504testScript transport script = do
511testScript transport script = do
505 logShow script
512 logShow script
506− mErr <- verify transport script
513+ mErr <- try $ verify transport script
507 case mErr of
514 case mErr of
515+ Left (ExpectedFailure _str) ->
508− Left err -> assertFailure $ "Failed with script " ++ show script ++ ": " ++ err ++ "\n"
518+ assertFailure $ "Failed with script " ++ show script ++ ": " ++ err ++ "\n"
509− Right () -> return ()
520+ return ()
511--------------------------------------------------------------------------------
522--------------------------------------------------------------------------------
512-- Accessors --
523-- Accessors --
@@ -604,3 +615,14 @@ snoc x xs = xs ++ [x]
605groupByKey :: Ord a => [(a, b)] -> Map a [b]
616groupByKey :: Ord a => [(a, b)] -> Map a [b]
606groupByKey = Map.fromListWith (++) . map (second return)
617groupByKey = Map.fromListWith (++) . map (second return)
619+--------------------------------------------------------------------------------
620+-- Expected failures (can't find explicit support for this in test-framework) --
621+--------------------------------------------------------------------------------
623+data ExpectedFailure = ExpectedFailure String deriving (Typeable, Show)
625+instance Exception ExpectedFailure
627+expectedFailure :: MonadIO m => String -> m ()
628+expectedFailure = liftIO . throwIO . ExpectedFailure