Skip to content

Commit

Permalink
[chore][pkg/stanza] Fix tests which leave files open (#28569)
Browse files Browse the repository at this point in the history
`fileconsumer.Manager.poll` leaves files open so that the next poll
cycle can use them. In tests, the direct use of this function should be
accompanied by explicit action to clean up the files.
  • Loading branch information
djaglowski committed Oct 24, 2023
1 parent 4bd587e commit 7daa7a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/stanza/fileconsumer/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,20 @@ func (m *Manager) Start(persister operator.Persister) error {
return nil
}

// Stop will stop the file monitoring process
func (m *Manager) Stop() error {
m.cancel()
m.wg.Wait()
func (m *Manager) closeFiles() {
for _, r := range m.previousPollFiles {
r.Close()
}
for _, r := range m.knownFiles {
r.Close()
}
}

// Stop will stop the file monitoring process
func (m *Manager) Stop() error {
m.cancel()
m.wg.Wait()
m.closeFiles()
m.cancel = nil
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/stanza/fileconsumer/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func buildTestManager(t *testing.T, cfg *Config, opts ...testManagerOption) (*Ma
}
input, err := cfg.Build(testutil.Logger(t), testEmitFunc(tmc.emitChan))
require.NoError(t, err)
t.Cleanup(func() {
input.closeFiles()
})
return input, tmc.emitChan
}

Expand Down

0 comments on commit 7daa7a3

Please sign in to comment.