From 8370f2aee294e270c1f89379af813510fb06079e Mon Sep 17 00:00:00 2001 From: mishaz Date: Fri, 10 Oct 2014 20:27:36 +0000 Subject: [PATCH] Added tests to check that we're iterating on manifest lines correctly and handling blank lines in manifests. LineIter will now handle long manifest lines properly and added test to check that we continue to do so. --- sdk/go/manifest/manifest.go | 23 +++++++------ sdk/go/manifest/manifest_test.go | 43 ++++++++++++++++++++++--- sdk/go/manifest/testdata/short_manifest | 6 ++++ 3 files changed, 57 insertions(+), 15 deletions(-) create mode 100644 sdk/go/manifest/testdata/short_manifest diff --git a/sdk/go/manifest/manifest.go b/sdk/go/manifest/manifest.go index 404a1beda8..d3316ffc1d 100644 --- a/sdk/go/manifest/manifest.go +++ b/sdk/go/manifest/manifest.go @@ -5,7 +5,6 @@ package manifest import ( - "bufio" "fmt" "log" "regexp" @@ -71,15 +70,19 @@ func parseManifestLine(s string) (m ManifestLine) { func (m *Manifest) LineIter() <-chan ManifestLine { ch := make(chan ManifestLine) go func(input string) { - scanner := bufio.NewScanner(strings.NewReader(input)) - for scanner.Scan() { - // We parse one line at a time, to save effort if we only need - // the first few lines. - ch <- parseManifestLine(scanner.Text()) - } - if err := scanner.Err(); err != nil { - log.Fatalf("Error encountered iterating through manifest lines: %v", - err) + // This slice holds the current line and the remainder of the + // manifest. We parse one line at a time, to save effort if we + // only need the first few lines. + lines := []string{"", input} + for { + lines = strings.SplitN(lines[1], "\n", 2) + if len(lines[0]) > 0 { + // Only parse non-blank lines + ch <- parseManifestLine(lines[0]) + } + if len(lines) == 1 { + break + } } close(ch) }(m.Text) diff --git a/sdk/go/manifest/manifest_test.go b/sdk/go/manifest/manifest_test.go index 1fea5fb2f1..b108870d61 100644 --- a/sdk/go/manifest/manifest_test.go +++ b/sdk/go/manifest/manifest_test.go @@ -125,6 +125,28 @@ func TestParseBlockLocatorSimple(t *testing.T) { "Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf"}}) } +func TestLineIterShortManifestWithBlankLines(t *testing.T) { + content, err := ioutil.ReadFile("testdata/short_manifest") + if err != nil { + t.Fatalf("Unexpected error reading manifest from file: %v", err) + } + manifest := Manifest{string(content)} + lineIter := manifest.LineIter() + + firstLine := <-lineIter + expectManifestLine(t, + firstLine, + ManifestLine{StreamName: ".", + Blocks: []string{"b746e3d2104645f2f64cd3cc69dd895d+15693477+E2866e643690156651c03d876e638e674dcd79475@5441920c"}, + Files: []string{"0:15893477:chr10_band0_s0_e3000000.fj"}}) + + received, ok := <- lineIter + if ok { + t.Fatalf("Expected lineIter to be closed, but received %v instead.", + received) + } +} + func TestBlockIterLongManifest(t *testing.T) { content, err := ioutil.ReadFile("testdata/long_manifest") if err != nil { @@ -136,9 +158,20 @@ func TestBlockIterLongManifest(t *testing.T) { firstBlock := <-blockChannel expectBlockLocator(t, firstBlock, - BlockLocator{Digest: "b748a3d2104645e2e84cd3cc69ddf95d", - Size: 15893477, - Hints: []string{"A2f66a643690158851c03df78a83fa874dcd79475@5441920c"}}) - // TODO(misha): Add tests to check the number of blocks and that we - // see the last block. + BlockLocator{Digest: "b746e3d2104645f2f64cd3cc69dd895d", + Size: 15693477, + Hints: []string{"E2866e643690156651c03d876e638e674dcd79475@5441920c"}}) + blocksRead := 1 + var lastBlock BlockLocator + for lastBlock = range blockChannel { + //log.Printf("Blocks Read: %d", blocksRead) + blocksRead++ + } + expectEqual(t, blocksRead, 853) + + expectBlockLocator(t, + lastBlock, + BlockLocator{Digest: "f9ce82f59e5908d2d70e18df9679b469", + Size: 31367794, + Hints: []string{"E53f903684239bcc114f7bf8ff9bd6089f33058db@5441920c"}}) } diff --git a/sdk/go/manifest/testdata/short_manifest b/sdk/go/manifest/testdata/short_manifest new file mode 100644 index 0000000000..bb9336c47b --- /dev/null +++ b/sdk/go/manifest/testdata/short_manifest @@ -0,0 +1,6 @@ +. b746e3d2104645f2f64cd3cc69dd895d+15693477+E2866e643690156651c03d876e638e674dcd79475@5441920c 0:15893477:chr10_band0_s0_e3000000.fj + + + + + -- 2.30.2