From d865b6e7c113dcad1cbb3259eb7cb08855a48fb7 Mon Sep 17 00:00:00 2001 From: Chris Mark Date: Thu, 18 Jul 2024 22:00:46 +0300 Subject: [PATCH] [chore][pkg/stanza] Use mock timers for flaky windows flash tests (#34128) **Description:** Trying out to mock the actual clock in order to fix flaky tests on windows. ref: https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/32715#issuecomment-2207635386 **Link to tracking Issue:** https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/32715 **Testing:** **Documentation:** --------- Signed-off-by: ChrsMark --- cmd/otelcontribcol/go.sum | 2 ++ cmd/oteltestbedcol/go.sum | 2 ++ exporter/datadogexporter/go.sum | 2 ++ .../internal/reader/reader_test.go | 25 ++++++++++++++++--- pkg/stanza/flush/flush.go | 10 +++++--- pkg/stanza/go.mod | 1 + pkg/stanza/go.sum | 2 ++ pkg/stanza/internal/now/now.go | 8 ++++++ receiver/filelogreceiver/go.sum | 2 ++ receiver/otlpjsonfilereceiver/go.sum | 2 ++ 10 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 pkg/stanza/internal/now/now.go diff --git a/cmd/otelcontribcol/go.sum b/cmd/otelcontribcol/go.sum index 725998e4d12f..843bfef7c45f 100644 --- a/cmd/otelcontribcol/go.sum +++ b/cmd/otelcontribcol/go.sum @@ -1733,6 +1733,8 @@ github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901/go.mod h1:Z86h9 github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= +github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= diff --git a/cmd/oteltestbedcol/go.sum b/cmd/oteltestbedcol/go.sum index c8c79c3cecd1..121bcece6730 100644 --- a/cmd/oteltestbedcol/go.sum +++ b/cmd/oteltestbedcol/go.sum @@ -399,6 +399,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= +github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= diff --git a/exporter/datadogexporter/go.sum b/exporter/datadogexporter/go.sum index aae3bf2943d7..31b50bbe3f6a 100644 --- a/exporter/datadogexporter/go.sum +++ b/exporter/datadogexporter/go.sum @@ -640,6 +640,8 @@ github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHW github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= +github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= diff --git a/pkg/stanza/fileconsumer/internal/reader/reader_test.go b/pkg/stanza/fileconsumer/internal/reader/reader_test.go index 97160e4ffc5f..521aab3bf9d6 100644 --- a/pkg/stanza/fileconsumer/internal/reader/reader_test.go +++ b/pkg/stanza/fileconsumer/internal/reader/reader_test.go @@ -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) { @@ -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 @@ -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() +} diff --git a/pkg/stanza/flush/flush.go b/pkg/stanza/flush/flush.go index afa03fe92f4d..9c3c4926e902 100644 --- a/pkg/stanza/flush/flush.go +++ b/pkg/stanza/flush/flush.go @@ -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 { @@ -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 } @@ -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 } @@ -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) } diff --git a/pkg/stanza/go.mod b/pkg/stanza/go.mod index d37495e8d096..53d0417b97db 100644 --- a/pkg/stanza/go.mod +++ b/pkg/stanza/go.mod @@ -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 diff --git a/pkg/stanza/go.sum b/pkg/stanza/go.sum index 4f57ddbfd4a7..95c555512c90 100644 --- a/pkg/stanza/go.sum +++ b/pkg/stanza/go.sum @@ -32,6 +32,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY= github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= +github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= diff --git a/pkg/stanza/internal/now/now.go b/pkg/stanza/internal/now/now.go new file mode 100644 index 000000000000..17de420da57a --- /dev/null +++ b/pkg/stanza/internal/now/now.go @@ -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 diff --git a/receiver/filelogreceiver/go.sum b/receiver/filelogreceiver/go.sum index d8eb86fe0962..1a8b402e2dd8 100644 --- a/receiver/filelogreceiver/go.sum +++ b/receiver/filelogreceiver/go.sum @@ -30,6 +30,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY= github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= +github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= diff --git a/receiver/otlpjsonfilereceiver/go.sum b/receiver/otlpjsonfilereceiver/go.sum index e12afb66aae9..64d7e4f9ac4d 100644 --- a/receiver/otlpjsonfilereceiver/go.sum +++ b/receiver/otlpjsonfilereceiver/go.sum @@ -30,6 +30,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY= github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= +github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=