Skip to content

Commit

Permalink
non-pointer events and tests (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthdm authored Dec 4, 2023
1 parent 2338c21 commit c089c0b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
33 changes: 32 additions & 1 deletion actor/event_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package actor

import (
fmt "fmt"
"github.com/stretchr/testify/assert"
"sync"
"testing"

"github.com/stretchr/testify/assert"
)

type CustomEvent struct {
Expand Down Expand Up @@ -42,9 +43,39 @@ func TestEventStreamLocal(t *testing.T) {
}

func TestEventStreamActorStartedEvent(t *testing.T) {
e, _ := NewEngine()
wg := sync.WaitGroup{}

wg.Add(1)
pidb := e.SpawnFunc(func(c *Context) {
switch msg := c.Message().(type) {
case ActorStartedEvent:
assert.Equal(t, msg.PID.ID, "a")
wg.Done()
}
}, "b")
e.Subscribe(pidb)

e.SpawnFunc(func(c *Context) {}, "a")
wg.Wait()
}

func TestEventStreamActorStoppedEvent(t *testing.T) {
e, _ := NewEngine()
wg := sync.WaitGroup{}

wg.Add(1)
a := e.SpawnFunc(func(c *Context) {}, "a")
pidb := e.SpawnFunc(func(c *Context) {
switch msg := c.Message().(type) {
case ActorStoppedEvent:
assert.Equal(t, msg.PID.ID, "a")
wg.Done()
}
}, "b")

e.Subscribe(pidb)
e.Poison(a).Wait()

wg.Wait()
}
4 changes: 2 additions & 2 deletions actor/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (p *process) Start() {

p.context.message = Started{}
applyMiddleware(recv.Receive, p.Opts.Middleware...)(p.context)
p.context.engine.BroadcastEvent(&ActorStartedEvent{PID: p.pid})
p.context.engine.BroadcastEvent(ActorStartedEvent{PID: p.pid})
p.logger.Debugw("actor started", "pid", p.pid)
// If we have messages in our buffer, invoke them.
if len(p.mbuffer) > 0 {
Expand Down Expand Up @@ -197,7 +197,7 @@ func (p *process) cleanup(wg *sync.WaitGroup) {
}
}
p.logger.Debugw("shutdown", "pid", p.pid)
p.context.engine.BroadcastEvent(&ActorStoppedEvent{PID: p.pid})
p.context.engine.BroadcastEvent(ActorStoppedEvent{PID: p.pid})
if wg != nil {
wg.Done()
}
Expand Down
3 changes: 3 additions & 0 deletions examples/eventstream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func main() {
fmt.Println("actor A started")
case MyCustomEvent:
fmt.Printf("actorA: event => %+v\n", msg)
// Get notified when other actors start.
case actor.ActorStartedEvent:
fmt.Printf("another actor started => %+v\n", msg.PID)
}
}, "actor_a")
// Subscribe the actor to the event stream from outside of the actor itself.
Expand Down

0 comments on commit c089c0b

Please sign in to comment.