Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions libpod/events/logfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,15 @@ func (e EventLogFile) Read(ctx context.Context, options ReadOptions) error {
return err
}
go func() {
time.Sleep(time.Until(untilTime))
if err := t.Stop(); err != nil {
logrus.Errorf("Stopping logger: %v", err)
timer := time.NewTimer(time.Until(untilTime))
defer timer.Stop()
select {
case <-timer.C:
if err := t.Stop(); err != nil {
logrus.Errorf("Stopping logger: %v", err)
}
case <-ctx.Done():
return
}
}()
}
Expand Down
41 changes: 41 additions & 0 deletions libpod/events/logfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
package events

import (
"context"
"os"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -172,3 +174,42 @@ func TestRenameLog(t *testing.T) {
require.NoError(t, os.Remove(target.Name()))
require.Equal(t, beforeRename, afterRename)
}

func TestReadUntilContextCancelled(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unit test doesn't add value, please remove

tmp, err := os.CreateTemp(t.TempDir(), "logfile-test-")
require.NoError(t, err)
defer tmp.Close()

e := EventLogFile{
options: EventerOptions{
LogFilePath: tmp.Name(),
},
}

ctx, cancel := context.WithCancel(context.Background())
eventChan := make(chan ReadResult)
options := ReadOptions{
EventChannel: eventChan,
Until: time.Now().Add(24 * time.Hour).Format(time.RFC3339),
Stream: true,
}

readErrChan := make(chan error, 1)
go func() {
readErrChan <- e.Read(ctx, options)
}()

// Give Read time to initialize and spawn the until timer goroutine
time.Sleep(50 * time.Millisecond)

// Cancel context (simulating client disconnect / Ctrl+C)
cancel()

select {
case <-readErrChan:
// Read returned as expected on context cancellation
case <-time.After(2 * time.Second):
t.Fatal("EventLogFile.Read did not return after context cancellation")
}
}