summaryrefslogtreecommitdiff
path: root/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs
blob: 57680a5b39867b909d3bf5f14c74ec4c58e52c71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{-# LANGUAGE RecordWildCards #-}
module Network.BitTorrent.Tracker.RPC.UDPSpec (spec, rpcOpts) where
import Control.Concurrent.Async
import Control.Monad
import Data.Default
import Data.List as L
import Data.Maybe
import Test.Hspec

import Network.BitTorrent.Core
import Network.BitTorrent.Tracker.Message as Message

import Network.BitTorrent.Tracker.TestData
import Network.BitTorrent.Tracker.MessageSpec hiding (spec)
import Network.BitTorrent.Tracker.RPC.UDP


validateInfo :: AnnounceQuery -> AnnounceInfo -> Expectation
validateInfo _ Message.Failure {..} = error "validateInfo: failure"
validateInfo AnnounceQuery {..}  AnnounceInfo {..} = do
    respComplete    `shouldSatisfy` isJust
    respIncomplete  `shouldSatisfy` isJust
    respMinInterval `shouldSatisfy` isNothing
    respWarning     `shouldSatisfy` isNothing
    peerList `shouldSatisfy` L.all (isNothing . peerId)
  where
    peerList = getPeerList respPeers

-- | Number of concurrent calls.
rpcCount :: Int
rpcCount = 100

rpcOpts :: Options
rpcOpts = def
  { optMinTimeout = 1
  , optMaxTimeout = 10
  }

spec :: Spec
spec = parallel $ do
  forM_ (L.filter isUdpTracker trackers) $ \ TrackerEntry {..} ->
    context trackerName $ do

      describe "announce" $ do
        if tryAnnounce then do
          it "have valid response" $ do
            withManager rpcOpts $ \ mgr -> do
              q <- arbitrarySample
              announce mgr trackerURI q >>= validateInfo q
        else do
          it "should throw TrackerNotResponding" $ do
            pending

      describe "scrape" $ do
        if tryScraping then do
          it "have valid response" $ do
            withManager rpcOpts $ \ mgr -> do
              xs <- scrape mgr trackerURI [def]
              L.length xs `shouldSatisfy` (>= 1)
        else do
          it "should throw TrackerNotResponding" $ do
            pending


      describe "Manager" $ do
        when tryScraping $ do
          it "should handle arbitrary intermixed concurrent queries" $ do
            withManager rpcOpts $ \ mgr -> do
              _ <- mapConcurrently (\ _ -> scrape mgr trackerURI [def]) [1..rpcCount]
              return ()