From cb75f50f4cae778d1dfc57edff771a5145dd9894 Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Thu, 17 Apr 2014 15:27:43 +0400 Subject: [Exchange] Move all download stuff to single module --- tests/Network/BitTorrent/Exchange/DownloadSpec.hs | 71 ++++++++++++++++++++++ .../BitTorrent/Exchange/Session/MetadataSpec.hs | 70 --------------------- 2 files changed, 71 insertions(+), 70 deletions(-) create mode 100644 tests/Network/BitTorrent/Exchange/DownloadSpec.hs delete mode 100644 tests/Network/BitTorrent/Exchange/Session/MetadataSpec.hs (limited to 'tests/Network/BitTorrent') diff --git a/tests/Network/BitTorrent/Exchange/DownloadSpec.hs b/tests/Network/BitTorrent/Exchange/DownloadSpec.hs new file mode 100644 index 00000000..a0d40af3 --- /dev/null +++ b/tests/Network/BitTorrent/Exchange/DownloadSpec.hs @@ -0,0 +1,71 @@ +{-# LANGUAGE RecordWildCards #-} +module Network.BitTorrent.Exchange.DownloadSpec (spec) where +import Control.Concurrent +import Data.ByteString as BS +import Data.ByteString.Lazy as BL +import Test.Hspec +import Test.QuickCheck + +import Data.BEncode as BE +import Data.Torrent as Torrent +import Network.BitTorrent.Address +import Network.BitTorrent.Exchange.Download +import Network.BitTorrent.Exchange.Message + +import Config +import Network.BitTorrent.CoreSpec () + + +placeholderAddr :: PeerAddr IP +placeholderAddr = "0.0.0.0:0" + +chunkBy :: Int -> BS.ByteString -> [BS.ByteString] +chunkBy s bs + | BS.null bs = [] + | otherwise = BS.take s bs : chunkBy s (BS.drop s bs) + +withUpdates :: Updates s a -> IO a +withUpdates m = do + Torrent {..} <- getTestTorrent + let infoDictLen = fromIntegral $ BL.length $ BE.encode tInfoDict + --mvar <- newMVar (nullStatus infoDictLen) + --runUpdates mvar placeholderAddr m + undefined + +simulateFetch :: InfoDict -> Updates s (Maybe InfoDict) +simulateFetch dict = go + where + blocks = chunkBy metadataPieceSize (BL.toStrict (BE.encode dict)) + packPiece ix = Torrent.Piece ix (blocks !! ix) + ih = idInfoHash dict + + go = do + mix <- scheduleBlock undefined undefined + case mix of + Nothing -> return Nothing + Just ix -> do + mdict <- pushBlock undefined (packPiece ix) + maybe go (return . Just) mdict + +spec :: Spec +spec = do + describe "scheduleBlock" $ do + it "never schedule the same index twice" $ do + pending + + describe "resetPending" $ do + it "" $ do + pending + + describe "cancelPending" $ do + it "must not throw an exception if cancel the same piece twice" $ do + pending + + describe "pushBlock" $ do + it "assemble infodict from chunks" $ do + Torrent {..} <- getTestTorrent + mdict <- withUpdates $ simulateFetch tInfoDict + mdict `shouldBe` Just tInfoDict + + it "must throw an exception if block if not requested" $ do + pending \ No newline at end of file diff --git a/tests/Network/BitTorrent/Exchange/Session/MetadataSpec.hs b/tests/Network/BitTorrent/Exchange/Session/MetadataSpec.hs deleted file mode 100644 index fc5236da..00000000 --- a/tests/Network/BitTorrent/Exchange/Session/MetadataSpec.hs +++ /dev/null @@ -1,70 +0,0 @@ -{-# LANGUAGE RecordWildCards #-} -module Network.BitTorrent.Exchange.Session.MetadataSpec (spec) where -import Control.Concurrent -import Data.ByteString as BS -import Data.ByteString.Lazy as BL -import Test.Hspec -import Test.QuickCheck - -import Data.BEncode as BE -import Data.Torrent as Torrent -import Network.BitTorrent.Address -import Network.BitTorrent.Exchange.Message -import Network.BitTorrent.Exchange.Session.Metadata - -import Config -import Network.BitTorrent.CoreSpec () - - -placeholderAddr :: PeerAddr IP -placeholderAddr = "0.0.0.0:0" - -chunkBy :: Int -> BS.ByteString -> [BS.ByteString] -chunkBy s bs - | BS.null bs = [] - | otherwise = BS.take s bs : chunkBy s (BS.drop s bs) - -withUpdates :: Updates a -> IO a -withUpdates m = do - Torrent {..} <- getTestTorrent - let infoDictLen = fromIntegral $ BL.length $ BE.encode tInfoDict - mvar <- newMVar (nullStatus infoDictLen) - runUpdates mvar placeholderAddr m - -simulateFetch :: InfoDict -> Updates (Maybe InfoDict) -simulateFetch dict = go - where - blocks = chunkBy metadataPieceSize (BL.toStrict (BE.encode dict)) - packPiece ix = Torrent.Piece ix (blocks !! ix) - ih = idInfoHash dict - - go = do - mix <- scheduleBlock - case mix of - Nothing -> return Nothing - Just ix -> do - mdict <- pushBlock (packPiece ix) ih - maybe go (return . Just) mdict - -spec :: Spec -spec = do - describe "scheduleBlock" $ do - it "never schedule the same index twice" $ do - pending - - describe "resetPending" $ do - it "" $ do - pending - - describe "cancelPending" $ do - it "must not throw an exception if cancel the same piece twice" $ do - pending - - describe "pushBlock" $ do - it "assemble infodict from chunks" $ do - Torrent {..} <- getTestTorrent - mdict <- withUpdates $ simulateFetch tInfoDict - mdict `shouldBe` Just tInfoDict - - it "must throw an exception if block if not requested" $ do - pending \ No newline at end of file -- cgit v1.2.3