summaryrefslogtreecommitdiff
path: root/bittorrent/tests/Network/BitTorrent/Tracker/SessionSpec.hs
blob: 72936ee780162543c5415cc50227dd83fe6c6089 (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
module Network.BitTorrent.Tracker.SessionSpec (spec) where
import Control.Monad
import Data.Default
import Data.List as L
import Test.Hspec

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

import Config

testSession :: Bool -> (Manager -> Session -> IO ()) -> IO ()
testSession runEmpty action = do
  t <- getTestTorrent
  withManager def def $ \ m -> do
    withSession m (idInfoHash (tInfoDict t)) (trackerList t) $ \ s ->
      action m s

    when runEmpty $ do
      withSession m (idInfoHash (tInfoDict t)) def $ \ s ->
        action m s

spec :: Spec
spec = do
  describe "Session" $ do
    it "start new session in paused state" $ do
      testSession True $ \ _ s -> do
        status <- getStatus s
        status `shouldBe` Paused

  describe "Query" $ do
    it "change status after notify" $ do
      testSession True $ \ m s -> do
        notify m s Started
        status <- getStatus s
        status `shouldBe` Running

        notify m s Stopped
        stopped <- getStatus s
        stopped `shouldBe` Paused

    it "completed event do not change status" $ do
      testSession True $ \ m s -> do
        notify m s Completed
        status <- getStatus s
        status `shouldBe` Paused

      testSession True $ \ m s -> do
        notify m s Started
        notify m s Completed
        status <- getStatus s
        status `shouldBe` Running

    it "return non-empty list of peers" $ do
      testSession False $ \ m s -> do
        notify m s Started
        peers <- askPeers m s
        peers `shouldSatisfy` (not . L.null)