Skip to content

Commit

Permalink
event/event_test: add test for read deadline bug
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Sep 1, 2024
1 parent 4292eee commit 739703c
Showing 1 changed file with 21 additions and 4 deletions.
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

0 comments on commit 739703c

Please sign in to comment.