-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
document how to pass arguments to actor creation. closes: #96
- Loading branch information
Showing
6 changed files
with
189 additions
and
23 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
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
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
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
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) {} |
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/anthdm/hollywood/actor" | ||
) | ||
|
||
type monitor struct { | ||
crashes int | ||
starts int | ||
stops int | ||
deadletters int | ||
msg string | ||
} | ||
|
||
type query struct { | ||
crashes int | ||
starts int | ||
stops int | ||
deadletters int | ||
} | ||
|
||
func newMonitor() actor.Receiver { | ||
return &monitor{} | ||
|
||
} | ||
func (m *monitor) Receive(c *actor.Context) { | ||
switch c.Message().(type) { | ||
case actor.Initialized: | ||
c.Engine().Subscribe(c.PID()) | ||
case actor.ActorRestartedEvent: | ||
m.crashes++ | ||
case actor.ActorStartedEvent: | ||
m.starts++ | ||
case actor.ActorStoppedEvent: | ||
m.stops++ | ||
case actor.DeadLetterEvent: | ||
m.deadletters++ | ||
case query: | ||
c.Respond(query{ | ||
crashes: m.crashes, | ||
starts: m.starts, | ||
stops: m.stops, | ||
deadletters: m.deadletters, | ||
}) | ||
} | ||
|
||
} | ||
|
||
type customMessage struct{} | ||
type unstableActor struct { | ||
restarts int | ||
spawnTime time.Time | ||
} | ||
|
||
func newUnstableActor() actor.Receiver { | ||
return &unstableActor{} | ||
} | ||
func (m *unstableActor) Receive(c *actor.Context) { | ||
switch c.Message().(type) { | ||
case actor.Initialized: | ||
m.spawnTime = time.Now() | ||
case actor.Started: | ||
fmt.Println("actor started") | ||
case customMessage: | ||
if time.Since(m.spawnTime) > time.Second { // We should crash once per second. | ||
panic("my time has come") | ||
} | ||
|
||
} | ||
} | ||
|
||
func main() { | ||
e, _ := actor.NewEngine() | ||
// Spawn a monitor actor and then an unstable actor. | ||
monitor := e.Spawn(newMonitor, "monitor") | ||
ua := e.Spawn(newUnstableActor, "unstable_actor", actor.WithMaxRestarts(10000)) | ||
repeater := e.SendRepeat(ua, customMessage{}, time.Millisecond*20) | ||
time.Sleep(time.Second * 5) | ||
repeater.Stop() | ||
e.Poison(ua).Wait() | ||
res, err := e.Request(monitor, query{}, time.Second).Result() | ||
if err != nil { | ||
fmt.Println("Query", err) | ||
} | ||
q := res.(query) | ||
fmt.Printf("Observed %d crashes\n", q.crashes) | ||
fmt.Printf("Observed %d starts\n", q.starts) | ||
fmt.Printf("Observed %d stops\n", q.stops) | ||
fmt.Printf("Observed %d deadletters\n", q.deadletters) | ||
e.Poison(monitor).Wait() // the monitor will output stats on stop. | ||
fmt.Println("done") | ||
} |