-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |