Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event/event: reset read deadline #27

Merged
merged 2 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ func readWithContext(ctx context.Context, conn net.Conn, buf []byte) (int, error
case <-ctx.Done():
// Set a short deadline to unblock the Read()
conn.SetReadDeadline(time.Now())
<-done // Make sure that the goroutine is done to avoid leaks
// Reset read deadline
defer conn.SetReadDeadline(time.Time{})
// Make sure that the goroutine is done to avoid leaks
<-done
return 0, ctx.Err()
}
}
Expand Down
25 changes: 21 additions & 4 deletions event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,38 @@ func TestSubscribe(t *testing.T) {
if os.Getenv("HYPRLAND_INSTANCE_SIGNATURE") == "" {
t.Skip("HYPRLAND_INSTANCE_SIGNATURE not set, skipping test")
}

c := MustClient()
defer c.Close()

// Make sure that we can exit a Subscribe loop by cancelling the
// context
ctx, cancel := context.WithTimeout(
context.Background(),
100*time.Millisecond,
)
defer cancel()

// Make sure that we can exit a Subscribe loop by cancelling the
// context
err := c.Subscribe(ctx, &DefaultEventHandler{}, AllEvents...)
cancel()

assert.Error(t, err)
assert.True(t, errors.Is(err, context.DeadlineExceeded))

// Make sure that we can call Subscribe again it can still be used,
// e.g.: the conn read deadline is not set otherwise it will exit
// immediatelly
ctx, cancel = context.WithCancel(context.Background())
go func() {
time.Sleep(100 * time.Millisecond)
cancel()
}()

start := time.Now()
err = c.Subscribe(ctx, &DefaultEventHandler{}, AllEvents...)
elapsed := time.Since(start)

assert.Error(t, err)
assert.True(t, errors.Is(err, context.Canceled))
assert.True(t, elapsed >= 100*time.Millisecond)
}

func TestProcessEvent(t *testing.T) {
Expand Down
Loading