From edb94a8784ff8b484eb0b002555f83f0e1abd543 Mon Sep 17 00:00:00 2001 From: Per Buer Date: Sat, 9 Dec 2023 15:01:19 +0100 Subject: [PATCH] Basic inbox test (#100) * Add tests for Scheduler and Inbox in actor package new tests for the Scheduler and Inbox functionality. It's not much, but it's a start. * replace sleep with Gosched. Shave a millisecond off the test time. :-) * nitpick. --- actor/inbox_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/actor/inbox_test.go b/actor/inbox_test.go index 9046234..205b0aa 100644 --- a/actor/inbox_test.go +++ b/actor/inbox_test.go @@ -1 +1,57 @@ package actor + +import ( + "runtime" + "sync" + "sync/atomic" + "testing" + "time" +) + +func TestScheduler(t *testing.T) { + var executed atomic.Bool + scheduler := NewScheduler(10) + scheduler.Schedule(func() { + executed.Store(true) + }) + runtime.Gosched() + if !executed.Load() { + t.Errorf("Expected the function to be executed") + } +} + +func TestInboxSendAndProcess(t *testing.T) { + inbox := NewInbox(10) + processedMessages := make(chan Envelope, 10) + mockProc := MockProcesser{ + processFunc: func(envelopes []Envelope) { + for _, e := range envelopes { + processedMessages <- e + } + }, + } + inbox.Start(mockProc) + msg := Envelope{} + inbox.Send(msg) + select { + case <-processedMessages: // Message processed + case <-time.After(time.Millisecond): + t.Errorf("Message was not processed in time") + } + + inbox.Stop() +} + +type MockProcesser struct { + processFunc func([]Envelope) +} + +func (m MockProcesser) Start() {} +func (m MockProcesser) PID() *PID { + return nil +} +func (m MockProcesser) Send(*PID, any, *PID) {} +func (m MockProcesser) Invoke(envelopes []Envelope) { + m.processFunc(envelopes) +} +func (m MockProcesser) Shutdown(_ *sync.WaitGroup) {}