From b867d924e39c2d83791939ba4417d640e3afd572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9mi=20V=C3=A1nyi?= Date: Thu, 15 Oct 2020 13:24:45 +0200 Subject: [PATCH] remove unnecessary goroutines --- filebeat/input/filestream/filestream_test.go | 60 +++++++------------- 1 file changed, 19 insertions(+), 41 deletions(-) diff --git a/filebeat/input/filestream/filestream_test.go b/filebeat/input/filestream/filestream_test.go index d8f26b6a523..05de2045579 100644 --- a/filebeat/input/filestream/filestream_test.go +++ b/filebeat/input/filestream/filestream_test.go @@ -91,13 +91,7 @@ func TestLogFileTruncated(t *testing.T) { defer f.Close() defer os.Remove(f.Name()) - reader, err := newFileReader( - logp.L(), - context.TODO(), - f, - readerConfig{}, - closerConfig{}, - ) + reader, err := newFileReader(logp.L(), context.TODO(), f, readerConfig{}, closerConfig{}) if err != nil { t.Fatalf("error while creating logReader: %+v", err) } @@ -106,20 +100,14 @@ func TestLogFileTruncated(t *testing.T) { _, err = reader.Read(buf) assert.Nil(t, err) - go func() { - err := f.Truncate(0) - if err != nil { - panic(err) - } - }() - - for err == nil { - buf := make([]byte, 1024) - _, err = reader.Read(buf) + err = f.Truncate(0) + if err != nil { + t.Fatalf("error while truncating file: %+v", err) } - assert.Equal(t, ErrFileTruncate, err) + err = readUntilError(reader) + assert.Equal(t, ErrFileTruncate, err) } func TestLogFileRenamed(t *testing.T) { @@ -148,18 +136,15 @@ func TestLogFileRenamed(t *testing.T) { _, err = reader.Read(buf) assert.Nil(t, err) - go func() { - err := os.Rename(f.Name(), renamedFile) - if err != nil { - panic(err) - } - }() + err = os.Rename(f.Name(), renamedFile) + if err != nil { + t.Fatalf("error while renaming file: %+v", err) + } err = readUntilError(reader) + os.Remove(renamedFile) assert.Equal(t, ErrClosed, err) - - os.Remove(renamedFile) } func TestLogFileRemoved(t *testing.T) { @@ -186,12 +171,10 @@ func TestLogFileRemoved(t *testing.T) { _, err = reader.Read(buf) assert.Nil(t, err) - go func() { - err := os.Remove(f.Name()) - if err != nil { - panic(err) - } - }() + err = os.Remove(f.Name()) + if err != nil { + t.Fatalf("error while remove file: %+v", err) + } err = readUntilError(reader) @@ -199,7 +182,10 @@ func TestLogFileRemoved(t *testing.T) { } func createTestLogFile() *os.File { - f := createEmptyTestFile() + f, err := ioutil.TempFile("", "filestream_reader_test") + if err != nil { + panic(err) + } content := []byte("first log line\nanother interesting line\na third log message\n") if _, err := f.Write(content); err != nil { panic(err) @@ -210,14 +196,6 @@ func createTestLogFile() *os.File { return f } -func createEmptyTestFile() *os.File { - f, err := ioutil.TempFile("", "filestream_reader_test") - if err != nil { - panic(err) - } - return f -} - func readUntilError(reader *logFile) error { buf := make([]byte, 1024) _, err := reader.Read(buf)