Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Add a timeout for the emitter unit tests
Browse files Browse the repository at this point in the history
This will help us to diagnose any deadlocks. The unit tests should
pass within the 2 second limit.

Resolves: #555 (comment)
  • Loading branch information
ankur22 committed Oct 6, 2022
1 parent 1e6535c commit 22fefb2
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions common/event_emitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"sync"
"testing"
"time"

"github.com/chromedp/cdproto"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -145,7 +146,7 @@ func TestBaseEventEmitter(t *testing.T) {
eventName := "AtomicIntEvent"
maxInt := 100

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
emitter := NewBaseEventEmitter(ctx)
ch := make(chan Event)
emitter.on(ctx, []string{eventName}, ch)
Expand All @@ -158,11 +159,16 @@ func TestBaseEventEmitter(t *testing.T) {
defer wg.Done()

for expectedI != maxInt {
e := <-ch
var e Event
select {
case e = <-ch:
case <-ctx.Done():
assert.FailNow(t, "context cancelled before test completed, deadlock?")
}

i, ok := e.data.(int)
if !ok {
assert.Fail(t, "unexpected type read from channel", e.data)
assert.FailNow(t, "unexpected type read from channel", e.data)
}

assert.Equal(t, eventName, e.typ)
Expand All @@ -181,6 +187,12 @@ func TestBaseEventEmitter(t *testing.T) {
defer wg.Done()

for i := 0; i < maxInt; i++ {
select {
case <-ctx.Done():
assert.FailNow(t, "context cancelled before test completed, deadlock?")
default:
}

i := i
emitter.emit(eventName, i)
}
Expand All @@ -207,7 +219,7 @@ func TestBaseEventEmitter(t *testing.T) {
eventName2 := "AtomicIntEvent2"
maxInt := 100

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
emitter := NewBaseEventEmitter(ctx)
ch := make(chan Event)
emitter.on(ctx, []string{eventName1, eventName2}, ch)
Expand All @@ -224,19 +236,24 @@ func TestBaseEventEmitter(t *testing.T) {
break
}

e := <-ch
var e Event
select {
case e = <-ch:
case <-ctx.Done():
assert.FailNow(t, "context cancelled before test completed, deadlock?")
}

switch e.typ {
case eventName1:
i, ok := e.data.(int)
if !ok {
assert.Fail(t, "unexpected type read from channel", e.data)
assert.FailNow(t, "unexpected type read from channel", e.data)
}
emitter.emit(eventName2, i)
case eventName2:
expectedI2++
default:
assert.Fail(t, "unexpected event type received")
assert.FailNow(t, "unexpected event type received")
}
}

Expand All @@ -250,6 +267,12 @@ func TestBaseEventEmitter(t *testing.T) {
defer wg.Done()

for i := 0; i < maxInt; i++ {
select {
case <-ctx.Done():
assert.FailNow(t, "context cancelled before test completed, deadlock?")
default:
}

i := i
emitter.emit(eventName1, i)
}
Expand Down

0 comments on commit 22fefb2

Please sign in to comment.