Skip to content

Commit

Permalink
remove unnecessary goroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
kvch committed Oct 15, 2020
1 parent f1edfc9 commit b867d92
Showing 1 changed file with 19 additions and 41 deletions.
60 changes: 19 additions & 41 deletions filebeat/input/filestream/filestream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -186,20 +171,21 @@ 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)

assert.Equal(t, ErrClosed, err)
}

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)
Expand All @@ -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)
Expand Down

0 comments on commit b867d92

Please sign in to comment.