Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for AtomicCounter. #631

Merged
merged 4 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions internal/atomiccounter/atomiccounter.go

This file was deleted.

2 changes: 1 addition & 1 deletion logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (l *LogAgent) Run(ctx context.Context) {
for {
select {
case <-t.C:
log.Printf("D! [logagent] open file count, %v", tail.OpenFileCount.Get())
log.Printf("D! [logagent] open file count, %v", tail.OpenFileCount.Load())
for _, c := range l.collections {
srcs := c.FindLogSrc()
for _, src := range srcs {
Expand Down
10 changes: 5 additions & 5 deletions plugins/inputs/logfile/tail/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"io"
"os"
"sync"
"sync/atomic"
"time"

"github.com/aws/amazon-cloudwatch-agent/internal/atomiccounter"
"github.com/aws/amazon-cloudwatch-agent/plugins/inputs/logfile/tail/watch"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/models"
Expand All @@ -24,7 +24,7 @@ var (
ErrDeletedNotReOpen = errors.New("File was deleted, tail should now stop")
exitOnDeletionCheckDuration = time.Minute
exitOnDeletionWaitDuration = 5 * time.Minute
OpenFileCount = atomiccounter.NewAtomicCounter()
OpenFileCount atomic.Int64
)

type Line struct {
Expand Down Expand Up @@ -122,7 +122,7 @@ func TailFile(filename string, config Config) (*Tail, error) {
if err != nil {
return nil, err
}
OpenFileCount.Increment()
OpenFileCount.Add(1)
}

if !config.ReOpen {
Expand Down Expand Up @@ -184,7 +184,7 @@ func (tail *Tail) closeFile() {
if tail.file != nil {
tail.file.Close()
tail.file = nil
OpenFileCount.Decrement()
OpenFileCount.Add(-1)
}
}

Expand All @@ -209,7 +209,7 @@ func (tail *Tail) reopen() error {
}
break
}
OpenFileCount.Increment()
OpenFileCount.Add(1)
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions plugins/inputs/logfile/tail/tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ func TestStopAtEOF(t *testing.T) {
assert.Equal(t, errStopAtEOF, tail.Err())

// Read to EOF
for i := 0; i < linesWrittenToFile - 3; i++ {
for i := 0; i < linesWrittenToFile-3; i++ {
<-tail.Lines
}

// Verify StopAtEOF() has completed.
select {
case <-done:
t.Log("StopAtEOF() completed (as expected)")
case <- time.After(time.Second * 1):
case <-time.After(time.Second * 1):
t.Fatalf("StopAtEOF() has not completed")
}

Expand Down Expand Up @@ -148,7 +148,7 @@ func setup(t *testing.T) (*os.File, *Tail, *testLogger) {
if err != nil {
t.Fatalf("failed to tail file %v: %v", tmpfile.Name(), err)
}
// Cannot expect OpenFileCount.Get() to be 1 because the TailFile struct
// Cannot expect OpenFileCount to be 1 because the TailFile struct
// was not created with MustExist=true, so file may not yet be opened.
return tmpfile, tail, &tl
}
Expand All @@ -165,7 +165,7 @@ func readThreelines(t *testing.T, tail *Tail) {
}
}
// If file was readable, then expect it to exist.
assert.Equal(t, int64(1), OpenFileCount.Get())
assert.Equal(t, int64(1), OpenFileCount.Load())
}

func verifyTailerLogging(t *testing.T, tlog *testLogger, expectedErrorMsg string) {
Expand All @@ -182,7 +182,7 @@ func verifyTailerLogging(t *testing.T, tlog *testLogger, expectedErrorMsg string
func verifyTailerExited(t *testing.T, tail *Tail) {
select {
case <-tail.Dead():
assert.Equal(t, int64(0), OpenFileCount.Get())
assert.Equal(t, int64(0), OpenFileCount.Load())
return
default:
t.Errorf("Tailer is still alive after file removed and wait period")
Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/logfile/tailersrc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestTailerSrc(t *testing.T) {
if err != nil {
t.Errorf("Failed to create temp file: %v", err)
}
beforeCount := tail.OpenFileCount.Get()
beforeCount := tail.OpenFileCount.Load()
tailer, err := tail.TailFile(file.Name(),
tail.Config{
ReOpen: false,
Expand All @@ -63,7 +63,7 @@ func TestTailerSrc(t *testing.T) {
t.Errorf("Failed to create tailer src for file %v with error: %v", file, err)
return
}
assert.Equal(t, beforeCount + 1, tail.OpenFileCount.Get())
assert.Equal(t, beforeCount+1, tail.OpenFileCount.Load())
ts := NewTailerSrc(
"groupName", "streamName",
"destination",
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestTailerSrc(t *testing.T) {
// Most test functions do not wait for the Tail to close the file.
// They rely on Tail to detect file deletion and close the file.
// So the count might be nonzero due to previous test cases.
assert.LessOrEqual(t, tail.OpenFileCount.Get(), beforeCount)
assert.LessOrEqual(t, tail.OpenFileCount.Load(), beforeCount)
}

func TestOffsetDoneCallBack(t *testing.T) {
Expand Down