2024-02-10 23:13:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-02-11 21:48:05 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
2024-02-10 23:13:31 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
2024-02-11 21:48:05 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-02-10 23:13:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MockSender struct {
|
|
|
|
messages []GenericMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *MockSender) Send(msg GenericMessage) error {
|
|
|
|
self.messages = append(self.messages, msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-11 10:51:41 +00:00
|
|
|
type MockSleeper struct {
|
2024-02-11 13:56:03 +00:00
|
|
|
ch *chan<- interface{}
|
|
|
|
callCount int
|
2024-02-11 10:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *MockSleeper) Sleep(ch chan<- interface{}) {
|
|
|
|
if self.ch == nil {
|
|
|
|
self.ch = &ch
|
|
|
|
}
|
2024-02-11 13:56:03 +00:00
|
|
|
self.callCount += 1
|
2024-02-11 10:51:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-18 16:28:32 +00:00
|
|
|
type FakeChatId struct {
|
|
|
|
FakeId int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self FakeChatId) GetTgIds() *[]int64 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-10 23:13:31 +00:00
|
|
|
func TestMessageThrottling(t *testing.T) {
|
|
|
|
testEvents := make(chan GenericMessage)
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
mockSender := MockSender{make([]GenericMessage, 0)}
|
2024-02-11 13:56:03 +00:00
|
|
|
mockSleeper := MockSleeper{nil, 0}
|
2024-02-18 00:44:10 +00:00
|
|
|
Consume(SendToTg(tgSenderWorker(testEvents, &wg, &mockSleeper, log.New(io.Discard, "", log.Ltime)),
|
|
|
|
&mockSender, &wg, log.New(io.Discard, "", log.Ltime)))
|
2024-02-18 16:28:32 +00:00
|
|
|
testEvents <- GenericMessage{TgChatId{123}, []MsgContent{StringMsgContent{"test1"}}}
|
|
|
|
testEvents <- GenericMessage{TgChatId{124}, []MsgContent{StringMsgContent{"test3"}}}
|
|
|
|
testEvents <- GenericMessage{TgChatId{123}, []MsgContent{StringMsgContent{"test2"}}}
|
|
|
|
testEvents <- GenericMessage{TgChatId{124}, []MsgContent{StringMsgContent{"test4"}}}
|
|
|
|
testEvents <- GenericMessage{FakeChatId{123}, []MsgContent{StringMsgContent{"testFake"}}}
|
2024-02-11 13:56:03 +00:00
|
|
|
assert.Equal(t, 1, mockSleeper.callCount)
|
2024-02-11 10:51:41 +00:00
|
|
|
*mockSleeper.ch <- nil
|
2024-02-11 13:56:03 +00:00
|
|
|
assert.Equal(t, 1, mockSleeper.callCount)
|
2024-02-18 16:28:32 +00:00
|
|
|
testEvents <- GenericMessage{TgChatId{123}, []MsgContent{StringMsgContent{"test5"}}}
|
2024-02-10 23:13:31 +00:00
|
|
|
close(testEvents)
|
2024-02-10 23:24:11 +00:00
|
|
|
wg.Wait()
|
2024-02-11 13:56:03 +00:00
|
|
|
assert.Equal(t, 2, mockSleeper.callCount)
|
2024-02-10 23:13:31 +00:00
|
|
|
|
2024-02-18 16:28:32 +00:00
|
|
|
assert.Len(t, mockSender.messages, 4)
|
|
|
|
assert.Contains(t, mockSender.messages, GenericMessage{TgChatId{123}, []MsgContent{StringMsgContent{"test1"}, StringMsgContent{"test2"}}})
|
|
|
|
assert.Contains(t, mockSender.messages, GenericMessage{TgChatId{124}, []MsgContent{StringMsgContent{"test3"}, StringMsgContent{"test4"}}})
|
|
|
|
assert.Contains(t, mockSender.messages, GenericMessage{FakeChatId{123}, []MsgContent{StringMsgContent{"testFake"}}})
|
2024-02-10 23:13:31 +00:00
|
|
|
}
|