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

39 lines
936 B
Go

package main
import (
"github.com/stretchr/testify/assert"
"sync"
"testing"
"time"
)
type MockSender struct {
messages []GenericMessage
}
func (self *MockSender) Send(msg GenericMessage) error {
self.messages = append(self.messages, msg)
return nil
}
func TestMessageThrottling(t *testing.T) {
testEvents := make(chan GenericMessage)
wg := sync.WaitGroup{}
mockSender := MockSender{make([]GenericMessage, 0)}
go tg_sender_worker(testEvents, &mockSender, &wg, time.Millisecond)
testEvents <- GenericMessage{123, "test1"}
testEvents <- GenericMessage{124, "test3"}
testEvents <- GenericMessage{123, "test2"}
testEvents <- GenericMessage{124, "test4"}
time.Sleep(time.Millisecond * 10)
testEvents <- GenericMessage{123, "test5"}
time.Sleep(time.Millisecond * 10)
close(testEvents)
assert.Len(t, mockSender.messages, 3)
assert.Contains(t, mockSender.messages, GenericMessage{123, "test1\ntest2\n"})
wg.Wait()
}