4889: added CopyPipeToChan unit test
authorTim Pierce <twp@unchi.org>
Thu, 22 Jan 2015 20:40:46 +0000 (15:40 -0500)
committerTim Pierce <twp@unchi.org>
Thu, 22 Jan 2015 20:40:46 +0000 (15:40 -0500)
Added TestCopyPipeToChanLongLines test to confirm that when
CopyPipeToChan attempts to read a line too long for bufio.Scanner, that
it generates an error message on the output channel.

services/crunchstat/crunchstat_test.go

index 48988a136743d63a352a0eda0a38895a9e8a797c..70caf26430ba6cb064a2e88c20a3f02af169cbb8 100644 (file)
@@ -1,6 +1,8 @@
 package main
 
 import (
+       "bufio"
+       "io"
        "os"
        "regexp"
        "testing"
@@ -48,3 +50,31 @@ func TestReadAllOrWarnSuccess(t *testing.T) {
                t.Fatalf("Expected channel to close, got %s", msg)
        }
 }
+
+// Test that if CopyPipeToChan reads a line longer than
+// bufio.MaxScanTokenSize, it emits an error to the output channel.
+func TestCopyPipeToChanLongLines(t *testing.T) {
+       logChan := make(chan string)
+       control := make(chan bool)
+
+       pipeIn, pipeOut := io.Pipe()
+       go CopyPipeToChan(pipeIn, logChan, control)
+
+       go func() {
+               long_line := make([]byte, bufio.MaxScanTokenSize+1)
+               for i := range long_line {
+                       long_line[i] = byte('x')
+               }
+               pipeOut.Write(long_line)
+               pipeOut.Close()
+       }()
+
+       // Expect error message from logChan.
+
+       errmsg := <-logChan
+       if matched, err := regexp.MatchString("^CopyPipeToChan:.*token too long", errmsg); err != nil || !matched {
+               t.Fatalf("expected CopyPipeToChan error, got %s", errmsg)
+       }
+
+       <-control
+}