From 4c731f7597f251957b4ded726a0c33d8b8bf0ad7 Mon Sep 17 00:00:00 2001 From: Tim Pierce Date: Thu, 22 Jan 2015 15:40:46 -0500 Subject: [PATCH] 4889: added CopyPipeToChan unit test 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 | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/services/crunchstat/crunchstat_test.go b/services/crunchstat/crunchstat_test.go index 48988a1367..70caf26430 100644 --- a/services/crunchstat/crunchstat_test.go +++ b/services/crunchstat/crunchstat_test.go @@ -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 +} -- 2.30.2