1
0
Fork 0
hswro-alarm-bot/filters_test.go

325 lines
10 KiB
Go
Raw Permalink Normal View History

2024-02-13 22:20:15 +00:00
package main
import (
2024-02-15 19:06:06 +00:00
"io"
"log"
"os"
2024-02-13 22:20:15 +00:00
"sync"
"testing"
2024-03-04 20:11:34 +00:00
"git.sr.ht/~michalr/go-satel"
2024-02-13 22:20:15 +00:00
"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)
2024-03-10 09:12:04 +00:00
for e := range FilterByTypeOrIndex(testEvents, &wg, []satel.ChangeType{satel.ArmedPartition, satel.PartitionFireAlarm}, []int{}) {
2024-02-13 22:20:15 +00:00
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
testEvents <- makeTestSatelEvent(satel.DoorOpened, 2, true)
testEvents <- makeTestSatelEvent(satel.PartitionAlarm, 3, true)
testEvents <- makeTestSatelEvent(satel.PartitionFireAlarm, 4, true)
testEvents <- makeTestSatelEvent(satel.TroublePart1, 5, true)
testEvents <- makeTestSatelEvent(satel.ZoneTamper, 6, true)
2024-02-13 22:20:15 +00:00
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, 2)
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, true))
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.PartitionFireAlarm, 4, true))
2024-02-13 22:20:15 +00:00
}
2024-02-14 20:34:16 +00:00
2024-02-18 17:44:08 +00:00
func TestSatelEventTypeFiltering_NoAllowedEventTypesMeansAllAreAllowed(t *testing.T) {
testEvents := make(chan satel.Event)
receivedEvents := make([]satel.Event, 0)
wg := sync.WaitGroup{}
go func() {
wg.Add(1)
2024-03-10 09:12:04 +00:00
for e := range FilterByTypeOrIndex(testEvents, &wg, []satel.ChangeType{}, []int{}) {
2024-02-18 17:44:08 +00:00
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
for index, ct := range SUPPORTED_CHANGE_TYPES {
testEvents <- makeTestSatelEvent(ct, index, true)
2024-02-18 17:44:08 +00:00
}
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, len(SUPPORTED_CHANGE_TYPES))
for index, ct := range SUPPORTED_CHANGE_TYPES {
assert.Contains(t, receivedEvents, makeTestSatelEvent(ct, index, true))
2024-02-18 17:44:08 +00:00
}
}
2024-02-18 18:00:49 +00:00
func TestSatelIndexFiltering(t *testing.T) {
testEvents := make(chan satel.Event)
receivedEvents := make([]satel.Event, 0)
wg := sync.WaitGroup{}
go func() {
wg.Add(1)
2024-03-10 09:12:04 +00:00
for e := range FilterByTypeOrIndex(testEvents, &wg, []satel.ChangeType{}, []int{1, 3}) {
2024-02-18 18:00:49 +00:00
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
testEvents <- makeTestSatelEvent(satel.DoorOpened, 2, true)
testEvents <- makeTestSatelEvent(satel.PartitionAlarm, 3, true)
testEvents <- makeTestSatelEvent(satel.PartitionFireAlarm, 4, true)
testEvents <- makeTestSatelEvent(satel.TroublePart1, 5, true)
testEvents <- makeTestSatelEvent(satel.ZoneTamper, 6, true)
2024-02-18 18:00:49 +00:00
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, 2)
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, true))
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.PartitionAlarm, 3, true))
2024-02-18 18:00:49 +00:00
}
func TestSatelIndexFiltering_NoAllowedEventTypesMeansAllAreAllowed(t *testing.T) {
testEvents := make(chan satel.Event)
receivedEvents := make([]satel.Event, 0)
wg := sync.WaitGroup{}
myReasonableMaxIndex := 100 // I wanted to use math.MaxInt at first, but it's kind of a waste of time here
go func() {
wg.Add(1)
2024-03-10 09:12:04 +00:00
for e := range FilterByTypeOrIndex(testEvents, &wg, []satel.ChangeType{}, []int{}) {
2024-02-18 18:00:49 +00:00
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
for i := 0; i < myReasonableMaxIndex; i++ {
testEvents <- makeTestSatelEvent(satel.ArmedPartition, i, true)
2024-02-18 18:00:49 +00:00
}
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, myReasonableMaxIndex)
for i := 0; i < myReasonableMaxIndex; i++ {
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, i, true))
2024-02-18 18:00:49 +00:00
}
}
2024-02-14 20:34:16 +00:00
func TestSatelLastSeenFiltering(t *testing.T) {
2024-02-15 19:06:06 +00:00
f, err := os.CreateTemp("", "TestSatelLastSeenFiltering")
assert.NoError(t, err)
tempFileName := f.Name()
assert.NoError(t, f.Close())
defer os.Remove(f.Name())
2024-02-14 20:34:16 +00:00
testEvents := make(chan satel.Event)
receivedEvents := make([]satel.Event, 0)
wg := sync.WaitGroup{}
fakeLog := log.New(io.Discard, "", log.Ltime)
ds := MakeDataStore(fakeLog, tempFileName, []int{})
2024-02-14 20:34:16 +00:00
go func() {
wg.Add(1)
2024-03-10 09:12:04 +00:00
for e := range FilterByLastSeen(testEvents, &wg, &ds, fakeLog) {
2024-02-14 20:34:16 +00:00
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
2024-02-14 20:34:16 +00:00
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, 3)
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, true))
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 2, true))
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, false))
2024-02-14 20:34:16 +00:00
}
2024-02-15 19:06:06 +00:00
func TestSatelLastSeenFilteringWithPersistence(t *testing.T) {
f, err := os.CreateTemp("", "TestSatelLastSeenFilteringWithPersistence")
assert.NoError(t, err)
tempFileName := f.Name()
assert.NoError(t, f.Close())
defer os.Remove(f.Name())
testEvents := make(chan satel.Event)
receivedEvents := make([]satel.Event, 0)
wg := sync.WaitGroup{}
fakeLog := log.New(io.Discard, "", log.Ltime)
ds := MakeDataStore(fakeLog, tempFileName, []int{})
2024-02-15 19:06:06 +00:00
go func() {
wg.Add(1)
2024-03-10 09:12:04 +00:00
for e := range FilterByLastSeen(testEvents, &wg, &ds, fakeLog) {
2024-02-15 19:06:06 +00:00
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
2024-02-15 19:06:06 +00:00
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, 3)
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, true))
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 2, true))
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, false))
2024-02-15 19:06:06 +00:00
testEvents = make(chan satel.Event)
receivedEvents = make([]satel.Event, 0)
ds = MakeDataStore(fakeLog, tempFileName, []int{})
2024-02-15 19:06:06 +00:00
go func() {
wg.Add(1)
2024-03-10 09:12:04 +00:00
for e := range FilterByLastSeen(testEvents, &wg, &ds, fakeLog) {
2024-02-15 19:06:06 +00:00
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
receivedEvents = make([]satel.Event, 0)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, false)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 1, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
testEvents <- makeTestSatelEvent(satel.ArmedPartition, 2, true)
2024-02-15 19:06:06 +00:00
close(testEvents)
wg.Wait()
assert.Len(t, receivedEvents, 1)
assert.Contains(t, receivedEvents, makeTestSatelEvent(satel.ArmedPartition, 1, true))
2024-02-15 19:06:06 +00:00
}
2024-03-10 09:52:27 +00:00
type MockSleeper struct {
ch *chan<- interface{}
callCount int
}
func (self *MockSleeper) Sleep(ch chan<- interface{}) {
if self.ch == nil {
self.ch = &ch
}
self.callCount += 1
}
func TestThrottle(t *testing.T) {
testEvents := make(chan GenericMessage)
receivedEvents := make([]GenericMessage, 0)
2024-03-10 09:52:27 +00:00
wg := sync.WaitGroup{}
fakeLog := log.New(io.Discard, "", log.Ltime)
mockSleeper := MockSleeper{nil, 0}
var (
tplMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true}
tplMessageTest2 = satel.BasicEventElement{Type: satel.ZoneViolation, Index: 2, Value: true}
tplMessageTest3 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: false}
tplMessageTest4 = satel.BasicEventElement{Type: satel.ZoneViolation, Index: 2, Value: false}
)
2024-03-10 09:52:27 +00:00
go func() {
wg.Add(1)
for e := range Throttle(testEvents, &wg, &mockSleeper, fakeLog) {
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest1}}
testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest2}}
testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest3}}
2024-03-10 09:52:27 +00:00
*mockSleeper.ch <- nil
testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest4}}
2024-03-10 09:52:27 +00:00
close(testEvents)
wg.Wait()
assert.Equal(t, 2, mockSleeper.callCount)
assert.Len(t, receivedEvents, 2)
assert.Contains(t, receivedEvents[0].Messages, tplMessageTest2)
assert.Contains(t, receivedEvents[0].Messages, tplMessageTest3)
assert.Len(t, receivedEvents[0].Messages, 2)
assert.Contains(t, receivedEvents[1].Messages, tplMessageTest4)
assert.Len(t, receivedEvents[1].Messages, 1)
2024-03-10 09:52:27 +00:00
}
func makeMassiveEvent(element satel.BasicEventElement, numElements int) GenericMessage {
retval := GenericMessage{TgChatId{123}, make([]satel.BasicEventElement, 0)}
for i := 0; i < numElements; i++ {
retval.Messages = append(retval.Messages, element)
}
return retval
}
func TestThrottle_ManyMessagesInOneEvent(t *testing.T) {
testEvents := make(chan GenericMessage)
receivedEvents := make([]GenericMessage, 0)
wg := sync.WaitGroup{}
fakeLog := log.New(io.Discard, "", log.Ltime)
mockSleeper := MockSleeper{nil, 0}
var (
tplMessageTest1 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: true}
tplMessageTest2 = satel.BasicEventElement{Type: satel.ZoneViolation, Index: 2, Value: true}
tplMessageTest3 = satel.BasicEventElement{Type: satel.ArmedPartition, Index: 1, Value: false}
tplMessageTest4 = satel.BasicEventElement{Type: satel.ZoneViolation, Index: 2, Value: false}
)
go func() {
wg.Add(1)
for e := range Throttle(testEvents, &wg, &mockSleeper, fakeLog) {
receivedEvents = append(receivedEvents, e)
}
wg.Done()
}()
testEvents <- makeMassiveEvent(tplMessageTest1, 100)
testEvents <- makeMassiveEvent(tplMessageTest2, 100)
testEvents <- makeMassiveEvent(tplMessageTest3, 100)
*mockSleeper.ch <- nil
testEvents <- makeMassiveEvent(tplMessageTest4, 100)
close(testEvents)
wg.Wait()
assert.Equal(t, 2, mockSleeper.callCount)
assert.Len(t, receivedEvents, 2)
assert.Contains(t, receivedEvents[0].Messages, tplMessageTest2)
assert.Contains(t, receivedEvents[0].Messages, tplMessageTest3)
assert.Len(t, receivedEvents[0].Messages, 2)
assert.Contains(t, receivedEvents[1].Messages, tplMessageTest4)
assert.Len(t, receivedEvents[1].Messages, 1)
}