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