Skip to content

Commit

Permalink
Minor improvements, testing. (#99)
Browse files Browse the repository at this point in the history
* delete empty file.

* use maxint to reduce the chance of collisions.

* expand test to test timeout.
  • Loading branch information
perbu authored Dec 9, 2023
1 parent 7b29e83 commit 629e47e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
41 changes: 27 additions & 14 deletions actor/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,22 +295,35 @@ func TestPoison(t *testing.T) {
}

func TestRequestResponse(t *testing.T) {
type responseEvent struct {
d time.Duration
}
e, err := NewEngine()
require.NoError(t, err)
pid := e.Spawn(NewTestProducer(t, func(t *testing.T, ctx *Context) {
if msg, ok := ctx.Message().(string); ok {
assert.Equal(t, "foo", msg)
ctx.Respond("bar")
assert.NoError(t, err)
a := e.SpawnFunc(func(c *Context) {
switch c.Message().(type) {
case responseEvent:
d := c.Message().(responseEvent).d
time.Sleep(d)
c.Respond("foo")
}
}), "dummy")
resp := e.Request(pid, "foo", time.Millisecond)
res, err := resp.Result()
assert.Nil(t, err)
assert.Equal(t, "bar", res)
// Response PID should be nil here. This is because
// the actual response process that will handle this RPC
// is deregistered. Test that it is actually cleaned up.
assert.Nil(t, e.Registry.get(resp.pid))
}, "actor_a")
t.Run("should timeout", func(t *testing.T) {
resp := e.Request(a, responseEvent{d: time.Millisecond * 2}, 1*time.Millisecond)
_, err := resp.Result()
assert.Error(t, err)
assert.Nil(t, e.Registry.get(resp.pid))

})
t.Run("should not timeout", func(t *testing.T) {
for i := 0; i < 200; i++ {
resp := e.Request(a, responseEvent{d: time.Microsecond * 1}, time.Millisecond*100)
res, err := resp.Result()
assert.NoError(t, err)
assert.Equal(t, "foo", res)
assert.Nil(t, e.Registry.get(resp.pid))
}
})
}

// 56 ns/op
Expand Down
3 changes: 2 additions & 1 deletion actor/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actor

import (
"context"
"math"
"math/rand"
"strconv"
"sync"
Expand All @@ -20,7 +21,7 @@ func NewResponse(e *Engine, timeout time.Duration) *Response {
engine: e,
result: make(chan any, 1),
timeout: timeout,
pid: NewPID(e.address, "response", strconv.Itoa(rand.Intn(100000))),
pid: NewPID(e.address, "response", strconv.Itoa(rand.Intn(math.MaxInt32))),
}
}

Expand Down

0 comments on commit 629e47e

Please sign in to comment.