-
Notifications
You must be signed in to change notification settings - Fork 2
/
da_events_test.go
47 lines (36 loc) · 1.07 KB
/
da_events_test.go
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
package zda
import (
"context"
"github.com/shimmeringbee/persistence/impl/memory"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
"time"
)
type mockEventSender struct {
mock.Mock
}
func (m *mockEventSender) sendEvent(event any) {
m.Called(event)
}
func TestZigbeeGateway_ReadEvent(t *testing.T) {
t.Run("context which expires should result in error", func(t *testing.T) {
zgw := New(context.Background(), memory.New(), nil, nil)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
defer cancel()
_, err := zgw.ReadEvent(ctx)
assert.ErrorIs(t, err, context.DeadlineExceeded)
})
t.Run("sent events are received through ReadEvent", func(t *testing.T) {
zgw := New(context.Background(), memory.New(), nil, nil)
ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)
defer cancel()
expectedEvent := struct{}{}
go func() {
zgw.sendEvent(expectedEvent)
}()
actualEvent, err := zgw.ReadEvent(ctx)
assert.NoError(t, err)
assert.Equal(t, expectedEvent, actualEvent)
})
}