Skip to content

Commit

Permalink
[chore] [pkg/stanza] Mock time functions for flaky TestExcludeOlderTh…
Browse files Browse the repository at this point in the history
…anFilter (#34174)

**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
Similar to
#34128.
Mocking the time functions to fix flakiness on windows.

**Link to tracking Issue:** <Issue number if applicable>
#32838

**Testing:** <Describe what testing was performed and which tests were
added.>

**Documentation:** <Describe the documentation added.>

Signed-off-by: ChrsMark <chrismarkou92@gmail.com>
  • Loading branch information
ChrsMark authored Jul 23, 2024
1 parent c71919f commit 6a4d7cd
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 46 deletions.
1 change: 1 addition & 0 deletions cmd/otelcontribcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ require (
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
1 change: 1 addition & 0 deletions cmd/oteltestbedcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ require (
github.com/ionos-cloud/sdk-go/v6 v6.1.11 // indirect
github.com/jaegertracing/jaeger v1.59.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
2 changes: 2 additions & 0 deletions connector/datadogconnector/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions exporter/datadogexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/ionos-cloud/sdk-go/v6 v6.1.11 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
2 changes: 2 additions & 0 deletions exporter/datadogexporter/integrationtest/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 3 additions & 20 deletions pkg/stanza/fileconsumer/internal/reader/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (
"testing"
"time"

"github.com/jonboulle/clockwork"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/filetest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/fingerprint"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/now"
internaltime "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/time"
)

func TestFileReader_FingerprintUpdated(t *testing.T) {
Expand Down Expand Up @@ -207,25 +206,9 @@ func TestFlushPeriodEOF(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, int64(0), r.Offset)

now.Now = newAlwaysIncreasingClock().Now
defer func() { now.Now = time.Now }()
internaltime.Now = internaltime.NewAlwaysIncreasingClock().Now
defer func() { internaltime.Now = time.Now }()

r.ReadToEnd(context.Background())
sink.ExpectTokens(t, content[0:aContentLength], []byte{'b'})
}

// Clock where Now() always returns a greater value than the previous return value
type alwaysIncreasingClock struct {
clockwork.FakeClock
}

func newAlwaysIncreasingClock() alwaysIncreasingClock {
return alwaysIncreasingClock{
FakeClock: clockwork.NewFakeClock(),
}
}

func (c alwaysIncreasingClock) Now() time.Time {
c.FakeClock.Advance(time.Nanosecond)
return c.FakeClock.Now()
}
4 changes: 3 additions & 1 deletion pkg/stanza/fileconsumer/matcher/internal/filter/exclude.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"time"

"go.uber.org/multierr"

internaltime "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/time"
)

type excludeOlderThanOption struct {
Expand All @@ -25,7 +27,7 @@ func (eot excludeOlderThanOption) apply(items []*item) ([]*item, error) {

// Keep (include) the file if its age (since last modification)
// is the same or less than the configured age.
fileAge := time.Since(fi.ModTime())
fileAge := internaltime.Since(fi.ModTime())
if fileAge <= eot.age {
filteredItems = append(filteredItems, item)
}
Expand Down
16 changes: 4 additions & 12 deletions pkg/stanza/fileconsumer/matcher/internal/filter/exclude_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"time"

"github.com/stretchr/testify/require"

internaltime "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/time"
)

func TestExcludeOlderThanFilter(t *testing.T) {
Expand All @@ -25,10 +27,6 @@ func TestExcludeOlderThanFilter(t *testing.T) {
expect []string
expectedErr string
expectedWindowsErr string

// List of OSes on which to skip this test case
skipOS []string
skipReason string
}{
"no_files": {
files: []string{},
Expand All @@ -47,9 +45,6 @@ func TestExcludeOlderThanFilter(t *testing.T) {
expectedErr: "",
},
"exclude_some_files": {
skipOS: []string{"windows"},
skipReason: "Flaky test case on Windows: https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/32838",

files: []string{"a.log", "b.log"},
fileMTimes: []time.Time{twoHoursAgo, threeHoursAgo},
excludeOlderThan: 3 * time.Hour,
Expand Down Expand Up @@ -78,11 +73,8 @@ func TestExcludeOlderThanFilter(t *testing.T) {

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
for _, os := range tc.skipOS {
if runtime.GOOS == os {
t.Skipf("Skipping test case: %s", tc.skipReason)
}
}
internaltime.Since = internaltime.NewAlwaysIncreasingClock().Since
defer func() { internaltime.Since = time.Since }()

tmpDir := t.TempDir()
var items []*item
Expand Down
10 changes: 5 additions & 5 deletions pkg/stanza/flush/flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"bufio"
"time"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/now"
internaltime "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/time"
)

type State struct {
Expand Down Expand Up @@ -42,7 +42,7 @@ func (s *State) Func(splitFunc bufio.SplitFunc, period time.Duration) bufio.Spli

// If there's a token, return it
if token != nil {
s.LastDataChange = now.Now()
s.LastDataChange = internaltime.Now()
s.LastDataLength = 0
return advance, token, err
}
Expand All @@ -55,14 +55,14 @@ func (s *State) Func(splitFunc bufio.SplitFunc, period time.Duration) bufio.Spli

// We're seeing new data so postpone the next flush
if len(data) > s.LastDataLength {
s.LastDataChange = now.Now()
s.LastDataChange = internaltime.Now()
s.LastDataLength = len(data)
return 0, nil, nil
}

// Flush timed out
if time.Since(s.LastDataChange) > period {
s.LastDataChange = now.Now()
s.LastDataChange = internaltime.Now()
s.LastDataLength = 0
return len(data), data, nil
}
Expand All @@ -74,6 +74,6 @@ func (s *State) Func(splitFunc bufio.SplitFunc, period time.Duration) bufio.Spli

// Deprecated: [v0.88.0] Use WithFunc instead.
func WithPeriod(splitFunc bufio.SplitFunc, period time.Duration) bufio.SplitFunc {
s := &State{LastDataChange: now.Now()}
s := &State{LastDataChange: internaltime.Now()}
return s.Func(splitFunc, period)
}
8 changes: 0 additions & 8 deletions pkg/stanza/internal/now/now.go

This file was deleted.

35 changes: 35 additions & 0 deletions pkg/stanza/internal/time/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package time // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/time"

import (
"time"

"github.com/jonboulle/clockwork"
)

var Now = time.Now
var Since = time.Since

// Clock where Now() always returns a greater value than the previous return value
type AlwaysIncreasingClock struct {
clockwork.FakeClock
}

func NewAlwaysIncreasingClock() AlwaysIncreasingClock {
return AlwaysIncreasingClock{
FakeClock: clockwork.NewFakeClock(),
}
}

func (c AlwaysIncreasingClock) Now() time.Time {
c.FakeClock.Advance(time.Nanosecond)
return c.FakeClock.Now()
}

func (c AlwaysIncreasingClock) Since(t time.Time) time.Duration {
// ensure that internal c.FakeClock.Now() will return a greater value
c.FakeClock.Advance(time.Nanosecond)
return c.FakeClock.Since(t)
}
1 change: 1 addition & 0 deletions receiver/filelogreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions receiver/otlpjsonfilereceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
Expand Down

0 comments on commit 6a4d7cd

Please sign in to comment.