Skip to content

Commit

Permalink
Added Stop tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anthdm committed Nov 30, 2023
1 parent 566790c commit ffa66c8
Showing 1 changed file with 50 additions and 26 deletions.
76 changes: 50 additions & 26 deletions actor/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,6 @@ import (
"github.com/stretchr/testify/require"
)

// TODO: How to test this thing huh? Should have bookkeeping counters
// in the processor or the inbox..
func TestPoisonShouldFlush(t *testing.T) {
e := NewEngine()
pid := e.SpawnFunc(func(c *Context) {
switch c.Message().(type) {
case Started:
fmt.Println("started")
case Stopped:
fmt.Println("stopped")
}
}, "foo")

go func() {
i := 0
for {
e.Send(pid, i)
i++
}
}()

time.Sleep(time.Second)
e.Poison(pid)
time.Sleep(time.Second * 2)
}

type tick struct{}
type tickReceiver struct {
ticks int
Expand Down Expand Up @@ -208,6 +182,56 @@ func TestSpawn(t *testing.T) {
wg.Wait()
}

func TestStopWaitGroup(t *testing.T) {
var (
e = NewEngine()
wg = sync.WaitGroup{}
x = int32(0)
)
wg.Add(1)

pid := e.SpawnFunc(func(c *Context) {
switch c.Message().(type) {
case Started:
wg.Done()
case Stopped:
atomic.AddInt32(&x, 1)
}
}, "foo")
wg.Wait()

pwg := &sync.WaitGroup{}
e.Stop(pid, pwg)
pwg.Wait()
assert.Equal(t, int32(1), atomic.LoadInt32(&x))
}

func TestStop(t *testing.T) {
var (
e = NewEngine()
wg = sync.WaitGroup{}
)
for i := 0; i < 4; i++ {
wg.Add(1)
tag := strconv.Itoa(i)
pid := e.SpawnFunc(func(c *Context) {
switch c.Message().(type) {
case Started:
wg.Done()
case Stopped:
}
}, "foo", WithTags(tag))

wg.Wait()
stopwg := &sync.WaitGroup{}
e.Stop(pid, stopwg)
stopwg.Wait()
// When a process is poisoned it should be removed from the registry.
// Hence, we should get the dead letter process here.
assert.Equal(t, e.deadLetter, e.Registry.get(pid))
}
}

func TestPoisonWaitGroup(t *testing.T) {
var (
e = NewEngine()
Expand Down

0 comments on commit ffa66c8

Please sign in to comment.