From 0a0d09bca9b312de72b4ca57904fae3a7f423e4b Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Sat, 8 Feb 2014 04:02:17 +0400 Subject: Allow empty tracker list --- src/Network/BitTorrent/Tracker/List.hs | 8 +++++--- tests/Network/BitTorrent/Tracker/ListSpec.hs | 5 +---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Network/BitTorrent/Tracker/List.hs b/src/Network/BitTorrent/Tracker/List.hs index b6371f1d..f2a4b264 100644 --- a/src/Network/BitTorrent/Tracker/List.hs +++ b/src/Network/BitTorrent/Tracker/List.hs @@ -26,6 +26,7 @@ import Prelude hiding (mapM, foldr) import Control.Applicative import Control.Exception import Data.List as L (elem, any, filter, null) +import Data.Maybe import Data.Foldable import Data.Traversable import Network.URI @@ -95,9 +96,10 @@ fixList mxss mx = do -- | Extract set of trackers from torrent file. The 'tAnnounce' key is -- only ignored if the 'tAnnounceList' key is present. -trackerList :: Torrent -> Maybe (TrackerList URI) -trackerList Torrent {..} = TierList <$> (tAnnounceList `fixList` tAnnounce) - <|> Announce <$> tAnnounce +trackerList :: Torrent -> TrackerList URI +trackerList Torrent {..} = fromMaybe (TierList []) $ do + TierList <$> (tAnnounceList `fixList` tAnnounce) + <|> Announce <$> tAnnounce -- | Shuffle /order of trackers/ in each tier, preserving original -- /order of tiers/. This can help to balance the load between the diff --git a/tests/Network/BitTorrent/Tracker/ListSpec.hs b/tests/Network/BitTorrent/Tracker/ListSpec.hs index 8decd3c9..03379136 100644 --- a/tests/Network/BitTorrent/Tracker/ListSpec.hs +++ b/tests/Network/BitTorrent/Tracker/ListSpec.hs @@ -18,14 +18,11 @@ uris = fmap (fromJust . parseURI . renderURI) [1..10 :: Int] renderURI n = "http://" ++ show n ++ ".org" list :: TrackerList URI -list = fromJust $ trackerList def { tAnnounceList = Just [uris] } +list = 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) -- cgit v1.2.3