summaryrefslogtreecommitdiff
path: root/internal/eventloop_events_test.go
blob: 45151640377ea138607b078378f087dd976e9e63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package internal

import (
	"context"
	"testing"
	"time"
)

func TestEventsStopsOnContextCancelWithoutRawData(t *testing.T) {
	el := mustNewEventLoop(t, eventLoopConfig{})
	rawCh := make(chan []byte)
	ctx, cancel := context.WithCancel(context.Background())
	out := el.events(ctx, rawCh)

	cancel()

	select {
	case _, ok := <-out:
		if ok {
			t.Fatal("expected output channel to be closed after cancellation")
		}
	case <-time.After(200 * time.Millisecond):
		t.Fatal("timed out waiting for output channel to close after cancellation")
	}
}

func TestEventsIgnoresEmptyRawPayload(t *testing.T) {
	el := mustNewEventLoop(t, eventLoopConfig{})
	rawCh := make(chan []byte, 1)
	ctx, cancel := context.WithCancel(context.Background())
	out := el.events(ctx, rawCh)

	rawCh <- nil

	select {
	case ep := <-out:
		t.Fatalf("expected no event for empty raw payload, got %#v", ep)
	case <-time.After(50 * time.Millisecond):
	}

	cancel()

	select {
	case _, ok := <-out:
		if ok {
			t.Fatal("expected output channel to be closed after cancellation")
		}
	case <-time.After(200 * time.Millisecond):
		t.Fatal("timed out waiting for output channel to close after cancellation")
	}
}