Skip to content

Commit

Permalink
[chore][pkg/stanza] Use mock timers for flaky windows flash tests (#3…
Browse files Browse the repository at this point in the history
…4128)

**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.-->
Trying out to mock the actual clock in order to fix flaky tests on
windows.
ref:
#32715 (comment)

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

**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 18, 2024
1 parent 4301d25 commit d865b6e
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 8 deletions.
2 changes: 2 additions & 0 deletions cmd/otelcontribcol/go.sum

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

2 changes: 2 additions & 0 deletions cmd/oteltestbedcol/go.sum

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

2 changes: 2 additions & 0 deletions exporter/datadogexporter/go.sum

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

25 changes: 21 additions & 4 deletions pkg/stanza/fileconsumer/internal/reader/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ package reader
import (
"context"
"fmt"
"runtime"
"strings"
"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"
)

func TestFileReader_FingerprintUpdated(t *testing.T) {
Expand Down Expand Up @@ -189,9 +190,6 @@ func TestFingerprintChangeSize(t *testing.T) {
}

func TestFlushPeriodEOF(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping test on Windows; See https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/32715")
}
tempDir := t.TempDir()
temp := filetest.OpenTemp(t, tempDir)
// Create a long enough initial token, so the scanner can't read the whole file at once
Expand All @@ -209,6 +207,25 @@ 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 }()

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()
}
10 changes: 6 additions & 4 deletions pkg/stanza/flush/flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package flush // import "github.com/open-telemetry/opentelemetry-collector-contr
import (
"bufio"
"time"

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

type State struct {
Expand Down Expand Up @@ -40,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 = time.Now()
s.LastDataChange = now.Now()
s.LastDataLength = 0
return advance, token, err
}
Expand All @@ -53,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 = time.Now()
s.LastDataChange = now.Now()
s.LastDataLength = len(data)
return 0, nil, nil
}

// Flush timed out
if time.Since(s.LastDataChange) > period {
s.LastDataChange = time.Now()
s.LastDataChange = now.Now()
s.LastDataLength = 0
return len(data), data, nil
}
Expand All @@ -72,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: time.Now()}
s := &State{LastDataChange: now.Now()}
return s.Func(splitFunc, period)
}
1 change: 1 addition & 0 deletions pkg/stanza/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/expr-lang/expr v1.16.9
github.com/fsnotify/fsnotify v1.7.0
github.com/goccy/go-json v0.10.3
github.com/jonboulle/clockwork v0.4.0
github.com/jpillora/backoff v1.0.0
github.com/json-iterator/go v1.1.12
github.com/leodido/go-syslog/v4 v4.1.0
Expand Down
2 changes: 2 additions & 0 deletions pkg/stanza/go.sum

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

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

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

import "time"

var Now = time.Now
2 changes: 2 additions & 0 deletions receiver/filelogreceiver/go.sum

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

2 changes: 2 additions & 0 deletions receiver/otlpjsonfilereceiver/go.sum

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

0 comments on commit d865b6e

Please sign in to comment.