Skip to content

Commit 4f429e5

Browse files
authored
[chore][fileconsumer] Fix flush test (#37598)
1 parent 0c69e17 commit 4f429e5

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

pkg/stanza/fileconsumer/internal/reader/reader_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,30 @@ func TestFlushPeriodEOF(t *testing.T) {
196196
_, err := temp.WriteString(string(content))
197197
require.NoError(t, err)
198198

199-
// Make sure FlushPeriod is small, so it is guaranteed to expire
200-
f, sink := testFactory(t, withFlushPeriod(5*time.Nanosecond))
199+
flushPeriod := time.Millisecond
200+
f, sink := testFactory(t, withFlushPeriod(flushPeriod))
201201
fp, err := f.NewFingerprint(temp)
202202
require.NoError(t, err)
203203
r, err := f.NewReader(temp, fp)
204204
require.NoError(t, err)
205205
assert.Equal(t, int64(0), r.Offset)
206206

207-
internaltime.Now = internaltime.NewAlwaysIncreasingClock().Now
208-
defer func() { internaltime.Now = time.Now }()
207+
clock := internaltime.NewAlwaysIncreasingClock()
208+
internaltime.Now = clock.Now
209+
internaltime.Since = clock.Since
210+
defer func() {
211+
internaltime.Now = time.Now
212+
internaltime.Since = time.Since
213+
}()
209214

215+
// First ReadToEnd should not emit only the terminated token
210216
r.ReadToEnd(context.Background())
211-
sink.ExpectTokens(t, content[0:aContentLength], []byte{'b'})
217+
sink.ExpectToken(t, content[0:aContentLength])
218+
219+
// Advance time past the flush period
220+
clock.Advance(2 * flushPeriod)
221+
222+
// Second ReadToEnd should emit the unterminated token because of flush timeout
223+
r.ReadToEnd(context.Background())
224+
sink.ExpectToken(t, []byte{'b'})
212225
}

pkg/stanza/flush/flush.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ type State struct {
1515
LastDataLength int
1616
}
1717

18-
func (s *State) Copy() *State {
19-
if s == nil {
20-
return nil
21-
}
22-
return &State{
23-
LastDataChange: s.LastDataChange,
24-
LastDataLength: s.LastDataLength,
25-
}
26-
}
27-
2818
// Func wraps a bufio.SplitFunc with a timer.
2919
// When the timer expires, an incomplete token may be returned.
3020
// The timer will reset any time the data parameter changes.
@@ -61,7 +51,7 @@ func (s *State) Func(splitFunc bufio.SplitFunc, period time.Duration) bufio.Spli
6151
}
6252

6353
// Flush timed out
64-
if time.Since(s.LastDataChange) > period {
54+
if internaltime.Since(s.LastDataChange) > period {
6555
s.LastDataChange = internaltime.Now()
6656
s.LastDataLength = 0
6757
return len(data), data, nil

0 commit comments

Comments
 (0)