Mock time functions
This commit is contained in:
parent
9f67c17fc4
commit
901aa8cfd6
|
@ -22,7 +22,7 @@ tasks:
|
||||||
env GOOS=linux GOARCH=arm64 go build -o alarm_bot.arm8
|
env GOOS=linux GOARCH=arm64 go build -o alarm_bot.arm8
|
||||||
- test: |
|
- test: |
|
||||||
cd hswro-alarm-bot
|
cd hswro-alarm-bot
|
||||||
go test
|
go test --count=10000
|
||||||
artifacts:
|
artifacts:
|
||||||
- hswro-alarm-bot/alarm_bot.x86-64
|
- hswro-alarm-bot/alarm_bot.x86-64
|
||||||
- hswro-alarm-bot/alarm_bot.arm5
|
- hswro-alarm-bot/alarm_bot.arm5
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
alarm_bot
|
alarm_bot
|
||||||
|
.venv/
|
||||||
|
|
12
main.go
12
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() {
|
func main() {
|
||||||
var (
|
var (
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
tg_events = make(chan GenericMessage)
|
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_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")
|
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}
|
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 {
|
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)
|
send_tg_message(tg_events, fmt.Sprintln("Change from SATEL: ", "type", e.Type, "index", e.Index, "value", e.Value), chat_ids)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type GenericMessage struct {
|
type GenericMessage struct {
|
||||||
|
@ -15,7 +14,11 @@ type Sender interface {
|
||||||
Send(msg GenericMessage) error
|
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)
|
wg.Add(1)
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
messagesToSend := make(map[int64]*strings.Builder)
|
messagesToSend := make(map[int64]*strings.Builder)
|
||||||
|
@ -38,10 +41,7 @@ loop:
|
||||||
messagesToSend[ev.chat_id].WriteRune('\n')
|
messagesToSend[ev.chat_id].WriteRune('\n')
|
||||||
if !waitingStarted {
|
if !waitingStarted {
|
||||||
waitingStarted = true
|
waitingStarted = true
|
||||||
go func() {
|
sleeper.Sleep(timeoutEvents)
|
||||||
time.Sleep(messageNotMoreOftenThan)
|
|
||||||
timeoutEvents <- nil
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
case <-timeoutEvents:
|
case <-timeoutEvents:
|
||||||
waitingStarted = false
|
waitingStarted = false
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockSender struct {
|
type MockSender struct {
|
||||||
|
@ -16,16 +15,27 @@ func (self *MockSender) Send(msg GenericMessage) error {
|
||||||
return nil
|
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) {
|
func TestMessageThrottling(t *testing.T) {
|
||||||
testEvents := make(chan GenericMessage)
|
testEvents := make(chan GenericMessage)
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
mockSender := MockSender{make([]GenericMessage, 0)}
|
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{123, "test1"}
|
||||||
testEvents <- GenericMessage{124, "test3"}
|
testEvents <- GenericMessage{124, "test3"}
|
||||||
testEvents <- GenericMessage{123, "test2"}
|
testEvents <- GenericMessage{123, "test2"}
|
||||||
testEvents <- GenericMessage{124, "test4"}
|
testEvents <- GenericMessage{124, "test4"}
|
||||||
time.Sleep(time.Millisecond * 10)
|
*mockSleeper.ch <- nil
|
||||||
testEvents <- GenericMessage{123, "test5"}
|
testEvents <- GenericMessage{123, "test5"}
|
||||||
close(testEvents)
|
close(testEvents)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
Loading…
Reference in New Issue