summaryrefslogtreecommitdiff
path: root/tests/Network/BitTorrent/Tracker/ListSpec.hs
blob: 8decd3c98eec780589a84b00b3c8119b66636078 (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
module Network.BitTorrent.Tracker.ListSpec (spec) where
import Control.Exception
import Data.Default
import Data.Foldable as F
import Data.List as L
import Data.Maybe
import Network.URI
import Test.Hspec

import Data.Torrent
import Network.BitTorrent.Tracker.List
import Network.BitTorrent.Tracker.RPC


uris :: [URI]
uris = fmap (fromJust . parseURI . renderURI) [1..10 :: Int]
  where
    renderURI n = "http://" ++ show n ++ ".org"

list :: TrackerList URI
list = fromJust $ trackerList def { tAnnounceList = Just [uris] }

spec :: Spec
spec = do
  describe "TrackerList" $ do
    it "trackerList is not empty" $ do
      pending

    it "shuffleTiers (may fail with very small probability)" $ do
      list' <- shuffleTiers list
      list' `shouldSatisfy` (/= list)

    it "traverseAll" $ do
      xs <- traverseAll (\ uri -> if uri == L.last uris
               then throwIO (GenericException "")
               else return uri { uriScheme = "udp://" }) list
      let udps = F.sum $ fmap (fromEnum . ("udp://" ==) . uriScheme) xs
      udps `shouldBe` pred (L.length uris)

    it "traverseTiers" $ do
      xs' <- traverseTiers (\ uri -> if uri == L.last uris then return uri
                              else throwIO (GenericException "")) list

      return ()