From 5fb512f86e3444954c70bbbaf6ca506e9752ca34 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Sat, 15 Feb 2014 16:32:28 +0400 Subject: [PATCH] Add ability to expect responses in any order. --- utils/fake.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/utils/fake.go b/utils/fake.go index db44d7ae..e618c41f 100644 --- a/utils/fake.go +++ b/utils/fake.go @@ -16,8 +16,9 @@ type expectedRequest struct { // FakeDownloader is like Downloader, but it used in tests // to stub out results type FakeDownloader struct { - expected []expectedRequest - progress *Progress + expected []expectedRequest + anyExpected map[string]expectedRequest + progress *Progress } // Check interface @@ -29,6 +30,7 @@ var ( func NewFakeDownloader() *FakeDownloader { result := &FakeDownloader{} result.expected = make([]expectedRequest, 0) + result.anyExpected = make(map[string]expectedRequest) result.progress = NewProgress() result.progress.Start() return result @@ -40,6 +42,12 @@ func (f *FakeDownloader) ExpectResponse(url string, response string) *FakeDownlo return f } +// AnyExpectResponse installs expectation on upcoming download with response in any order (url should be unique) +func (f *FakeDownloader) AnyExpectResponse(url string, response string) *FakeDownloader { + f.anyExpected[url] = expectedRequest{URL: url, Response: response} + return f +} + // ExpectError installs expectation on upcoming download with error func (f *FakeDownloader) ExpectError(url string, err error) *FakeDownloader { f.expected = append(f.expected, expectedRequest{URL: url, Err: err}) @@ -51,16 +59,19 @@ func (f *FakeDownloader) Empty() bool { return len(f.expected) == 0 } -// DownloadWithChecksum performs fake download by matching against first expectation in the queue, with cheksum verification +// DownloadWithChecksum performs fake download by matching against first expectation in the queue or any expectation, with cheksum verification func (f *FakeDownloader) DownloadWithChecksum(url string, filename string, result chan<- error, expected ChecksumInfo, ignoreMismatch bool) { - if len(f.expected) == 0 || f.expected[0].URL != url { + var expectation expectedRequest + if len(f.expected) > 0 && f.expected[0].URL == url { + expectation, f.expected = f.expected[0], f.expected[1:] + } else if _, ok := f.anyExpected[url]; ok { + expectation = f.anyExpected[url] + delete(f.anyExpected, url) + } else { result <- fmt.Errorf("unexpected request for %s", url) return } - expectation := f.expected[0] - f.expected = f.expected[1:] - if expectation.Err != nil { result <- expectation.Err return @@ -92,9 +103,9 @@ func (f *FakeDownloader) DownloadWithChecksum(url string, filename string, resul if expected.Size != cks.Sum().Size || expected.MD5 != "" && expected.MD5 != cks.Sum().MD5 || expected.SHA1 != "" && expected.SHA1 != cks.Sum().SHA1 || expected.SHA256 != "" && expected.SHA256 != cks.Sum().SHA256 { if ignoreMismatch { - fmt.Printf("WARNING: checksums don't match: %#v != %#v\n", expected, cks.Sum()) + fmt.Printf("WARNING: checksums don't match: %#v != %#v for %s\n", expected, cks.Sum(), url) } else { - result <- fmt.Errorf("checksums don't match: %#v != %#v", expected, cks.Sum()) + result <- fmt.Errorf("checksums don't match: %#v != %#v for %s", expected, cks.Sum(), url) return } }