Skip to content

Commit

Permalink
Basic inbox test (#100)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
perbu authored Dec 9, 2023
1 parent 003e117 commit edb94a8
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions actor/inbox_test.go
Original file line number Diff line number Diff line change
@@ -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) {}

0 comments on commit edb94a8

Please sign in to comment.