56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||
|
|
||
|
package worker;
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
"log"
|
||
|
"feudal/message"
|
||
|
)
|
||
|
|
||
|
type testClockWorker struct {
|
||
|
AbstractContext
|
||
|
ticker Runner
|
||
|
recipients []message.Sender
|
||
|
}
|
||
|
|
||
|
func (w *testClockWorker) Receive(m message.Envelope) {
|
||
|
switch m.Body().(type) {
|
||
|
case *message.SubscribeToClockTick:
|
||
|
w.Subscribe(m.Sender())
|
||
|
case *message.ClockTick:
|
||
|
for _,r := range(w.recipients) {
|
||
|
r.Send(m)
|
||
|
}
|
||
|
default:
|
||
|
log.Fatalln("unexpected message", m.Body())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (w *testClockWorker) Subscribe(recipient message.Sender) {
|
||
|
w.recipients = append(w.recipients, recipient)
|
||
|
}
|
||
|
|
||
|
func (w *testClockWorker) Stop() {
|
||
|
w.ticker.Stop()
|
||
|
w.AbstractContext.Queue.Stop()
|
||
|
}
|
||
|
|
||
|
func TestNewClock(t *testing.T) {
|
||
|
nw := &testClockWorker{ ticker: NewClock(500) }
|
||
|
|
||
|
subscriber := NewInterrogator()
|
||
|
|
||
|
nw.Init(nw, "clocktest")
|
||
|
nw.Start(DefaultDispatcher(), nw)
|
||
|
nw.ticker.Start(DefaultDispatcher(), nw)
|
||
|
|
||
|
nw.Send(message.New(&message.SubscribeToClockTick{}, subscriber))
|
||
|
tick := <- subscriber
|
||
|
switch tick.Body().(type) {
|
||
|
case *message.ClockTick:
|
||
|
default:
|
||
|
t.Errorf("invalid message")
|
||
|
}
|
||
|
}
|