From 4d69ee4eaa2dfdb12c866873d66e94e7c2847d33 Mon Sep 17 00:00:00 2001 From: ElegantMonkey Date: Sat, 13 May 2017 19:35:35 -0300 Subject: [PATCH] Use buffered channels, add test for invalid hash --- .../filesizeFetcher/filesizeFetcher.go | 4 +- service/torrent/filesizeFetcher/operation.go | 2 +- .../torrent/filesizeFetcher/operation_test.go | 45 +++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 service/torrent/filesizeFetcher/operation_test.go diff --git a/service/torrent/filesizeFetcher/filesizeFetcher.go b/service/torrent/filesizeFetcher/filesizeFetcher.go index b4509377..8c0cc022 100644 --- a/service/torrent/filesizeFetcher/filesizeFetcher.go +++ b/service/torrent/filesizeFetcher/filesizeFetcher.go @@ -29,11 +29,11 @@ func New(fetcherConfig *config.FilesizeFetcherConfig) (fetcher *FilesizeFetcher, client, err := torrent.NewClient(nil) fetcher = &FilesizeFetcher{ torrentClient: client, - results: make(chan Result), + results: make(chan Result, fetcherConfig.QueueSize), queueSize: fetcherConfig.QueueSize, timeout: fetcherConfig.Timeout, maxDays: fetcherConfig.MaxDays, - done: make(chan int), + done: make(chan int, 1), failedOperations: make(map[uint]struct{}), wakeUp: time.NewTicker(time.Second * time.Duration(fetcherConfig.WakeUpInterval)), } diff --git a/service/torrent/filesizeFetcher/operation.go b/service/torrent/filesizeFetcher/operation.go index 00f5b8c2..1a08d061 100644 --- a/service/torrent/filesizeFetcher/operation.go +++ b/service/torrent/filesizeFetcher/operation.go @@ -26,7 +26,7 @@ func NewFetchOperation(fetcher *FilesizeFetcher, dbEntry model.Torrent) (op *Fet op = &FetchOperation{ fetcher: fetcher, torrent: dbEntry, - done: make(chan int), + done: make(chan int, 1), } return } diff --git a/service/torrent/filesizeFetcher/operation_test.go b/service/torrent/filesizeFetcher/operation_test.go new file mode 100644 index 00000000..21e36faa --- /dev/null +++ b/service/torrent/filesizeFetcher/operation_test.go @@ -0,0 +1,45 @@ +package filesizeFetcher; + +import ( + "testing" + + "github.com/anacrolix/torrent" + "github.com/ewhal/nyaa/model" +) + +func TestInvalidHash(t *testing.T) { + client, err := torrent.NewClient(nil) + if err != nil { + t.Skipf("Failed to create client, with err %v. Skipping.", err) + } + + fetcher := &FilesizeFetcher{ + timeout: 5, + torrentClient: client, + results: make(chan Result, 1), + } + + dbEntry := model.Torrent{ + Hash: "INVALID", + Name: "Invalid", + } + + op := NewFetchOperation(fetcher, dbEntry) + fetcher.wg.Add(1) + op.Start(fetcher.results) + + var res Result + select { + case res = <-fetcher.results: + break + default: + t.Fatal("No result in channel, should have one") + } + + if res.err == nil { + t.Fatal("Got no error, should have got invalid magnet") + } + + t.Logf("Got error %s, shouldn't be timeout", res.err) +} +