From 901aa8cfd65884fe33ea718f9442c5a79f6594a0 Mon Sep 17 00:00:00 2001 From: Michal Rudowicz Date: Sun, 11 Feb 2024 11:51:41 +0100 Subject: [PATCH] Mock time functions --- .build.yml | 2 +- .gitignore | 1 + main.go | 12 +++++++++++- sender_worker.go | 12 ++++++------ sender_worker_test.go | 16 +++++++++++++--- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/.build.yml b/.build.yml index 487c5c5..0eb8cb6 100644 --- a/.build.yml +++ b/.build.yml @@ -22,7 +22,7 @@ tasks: env GOOS=linux GOARCH=arm64 go build -o alarm_bot.arm8 - test: | cd hswro-alarm-bot - go test + go test --count=10000 artifacts: - hswro-alarm-bot/alarm_bot.x86-64 - hswro-alarm-bot/alarm_bot.arm5 diff --git a/.gitignore b/.gitignore index c4c94c2..76f3ec6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ alarm_bot +.venv/ diff --git a/main.go b/main.go index 1e7efe0..2aa4ecf 100644 --- a/main.go +++ b/main.go @@ -34,10 +34,20 @@ func send_tg_message(tg_events chan GenericMessage, msg string, chat_ids []int64 } } +type RealSleeper struct { + duration time.Duration +} + +func (self RealSleeper) Sleep(ch chan<- interface{}) { + time.Sleep(self.duration) + ch <- nil +} + func main() { var ( wg sync.WaitGroup tg_events = make(chan GenericMessage) + sleeper = RealSleeper{time.Second * 15} ) satel_api_addr := flag.String("satel-addr", "", "Address that should be used to connect to the SATEL device") satel_api_port := flag.String("satel-port", "7094", "Port that should be used to connect to the SATEL device") @@ -71,7 +81,7 @@ func main() { } tgSender := TgSender{bot} - go tg_sender_worker(tg_events, tgSender, &wg, time.Second*15) + go tg_sender_worker(tg_events, tgSender, &wg, sleeper) for e, ok := <-s.Events; ok; e, ok = <-s.Events { send_tg_message(tg_events, fmt.Sprintln("Change from SATEL: ", "type", e.Type, "index", e.Index, "value", e.Value), chat_ids) } diff --git a/sender_worker.go b/sender_worker.go index 7cb1977..55713ba 100644 --- a/sender_worker.go +++ b/sender_worker.go @@ -3,7 +3,6 @@ package main import ( "strings" "sync" - "time" ) type GenericMessage struct { @@ -15,7 +14,11 @@ type Sender interface { Send(msg GenericMessage) error } -func tg_sender_worker(tg_events <-chan GenericMessage, s Sender, wg *sync.WaitGroup, messageNotMoreOftenThan time.Duration) { +type Sleeper interface { + Sleep(ch chan<- interface{}) +} + +func tg_sender_worker(tg_events <-chan GenericMessage, s Sender, wg *sync.WaitGroup, sleeper Sleeper) { wg.Add(1) defer wg.Done() messagesToSend := make(map[int64]*strings.Builder) @@ -38,10 +41,7 @@ loop: messagesToSend[ev.chat_id].WriteRune('\n') if !waitingStarted { waitingStarted = true - go func() { - time.Sleep(messageNotMoreOftenThan) - timeoutEvents <- nil - }() + sleeper.Sleep(timeoutEvents) } case <-timeoutEvents: waitingStarted = false diff --git a/sender_worker_test.go b/sender_worker_test.go index dad67dd..0ad381a 100644 --- a/sender_worker_test.go +++ b/sender_worker_test.go @@ -4,7 +4,6 @@ import ( "github.com/stretchr/testify/assert" "sync" "testing" - "time" ) type MockSender struct { @@ -16,16 +15,27 @@ func (self *MockSender) Send(msg GenericMessage) error { 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)} - go tg_sender_worker(testEvents, &mockSender, &wg, time.Millisecond) + 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"} - time.Sleep(time.Millisecond * 10) + *mockSleeper.ch <- nil testEvents <- GenericMessage{123, "test5"} close(testEvents) wg.Wait()