67 lines
2.3 KiB
Go
67 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/probakowski/go-satel"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestSatelEventTypeFiltering(t *testing.T) {
|
|
testEvents := make(chan satel.Event)
|
|
receivedEvents := make([]satel.Event, 0)
|
|
wg := sync.WaitGroup{}
|
|
|
|
go func() {
|
|
wg.Add(1)
|
|
for e := range FilterByType(testEvents, []satel.ChangeType{satel.ArmedPartition, satel.PartitionFireAlarm}) {
|
|
receivedEvents = append(receivedEvents, e)
|
|
}
|
|
wg.Done()
|
|
}()
|
|
|
|
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true}
|
|
testEvents <- satel.Event{Type: satel.DoorOpened, Index: 2, Value: true}
|
|
testEvents <- satel.Event{Type: satel.PartitionAlarm, Index: 3, Value: true}
|
|
testEvents <- satel.Event{Type: satel.PartitionFireAlarm, Index: 4, Value: true}
|
|
testEvents <- satel.Event{Type: satel.TroublePart1, Index: 5, Value: true}
|
|
testEvents <- satel.Event{Type: satel.ZoneTamper, Index: 6, Value: true}
|
|
|
|
close(testEvents)
|
|
wg.Wait()
|
|
|
|
assert.Len(t, receivedEvents, 2)
|
|
assert.Contains(t, receivedEvents, satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true})
|
|
assert.Contains(t, receivedEvents, satel.Event{Type: satel.PartitionFireAlarm, Index: 4, Value: true})
|
|
}
|
|
|
|
func TestSatelLastSeenFiltering(t *testing.T) {
|
|
testEvents := make(chan satel.Event)
|
|
receivedEvents := make([]satel.Event, 0)
|
|
wg := sync.WaitGroup{}
|
|
|
|
go func() {
|
|
wg.Add(1)
|
|
for e := range FilterByLastSeen(testEvents) {
|
|
receivedEvents = append(receivedEvents, e)
|
|
}
|
|
wg.Done()
|
|
}()
|
|
|
|
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true}
|
|
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 2, Value: true}
|
|
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true}
|
|
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: false}
|
|
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 2, Value: true}
|
|
testEvents <- satel.Event{Type: satel.ArmedPartition, Index: 1, Value: false}
|
|
|
|
close(testEvents)
|
|
wg.Wait()
|
|
|
|
assert.Len(t, receivedEvents, 3)
|
|
assert.Contains(t, receivedEvents, satel.Event{Type: satel.ArmedPartition, Index: 1, Value: true})
|
|
assert.Contains(t, receivedEvents, satel.Event{Type: satel.ArmedPartition, Index: 2, Value: true})
|
|
assert.Contains(t, receivedEvents, satel.Event{Type: satel.ArmedPartition, Index: 1, Value: false})
|
|
}
|