Skip to content

Commit

Permalink
Add handling for sending and receiving events from the da.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwood committed Dec 31, 2023
1 parent bd38a5c commit 61623c3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
24 changes: 17 additions & 7 deletions da_events.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package zda

import "context"
import (
"context"
"github.com/shimmeringbee/logwrap"
)

type eventSender interface {
sendEvent(event interface{})
}

func (g *gateway) sendEvent(event interface{}) {
//TODO implement me
panic("implement me")
func (g *gateway) sendEvent(e interface{}) {
select {
case g.events <- e:
default:
g.logger.LogError(g.ctx, "failed to send event, channel buffer is full", logwrap.Datum("event", e))
}
}

func (g *gateway) ReadEvent(_ context.Context) (interface{}, error) {
//TODO implement me
panic("implement me")
func (g *gateway) ReadEvent(ctx context.Context) (interface{}, error) {
select {
case e := <-g.events:
return e, nil
case <-ctx.Done():
return nil, context.DeadlineExceeded
}
}
37 changes: 36 additions & 1 deletion da_events_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package zda

import "github.com/stretchr/testify/mock"
import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
"time"
)

type mockEventSender struct {
mock.Mock
Expand All @@ -9,3 +15,32 @@ type mockEventSender struct {
func (m *mockEventSender) sendEvent(event interface{}) {
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(), 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(), nil, nil).(*gateway)

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)
})
}
4 changes: 4 additions & 0 deletions gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func New(baseCtx context.Context, p zigbee.Provider, r ruleExecutor) da.Gateway

callbacks: callbacks.Create(),
ruleExecutor: r,

events: make(chan interface{}, 0xffff),
}

gw.WithGoLogger(log.New(os.Stderr, "", log.LstdFlags))
Expand All @@ -57,6 +59,8 @@ type gateway struct {

callbacks callbacks.AdderCaller
ruleExecutor ruleExecutor

events chan interface{}
}

func (g *gateway) Capabilities() []da.Capability {
Expand Down

0 comments on commit 61623c3

Please sign in to comment.