[go: nahoru, domu]

Skip to content

Commit

Permalink
Implement Forward.
Browse files Browse the repository at this point in the history
  • Loading branch information
potterdai committed Feb 24, 2018
1 parent 23232b9 commit c0ef81a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions actor/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func (m *mockContext) Tell(pid *PID, message interface{}) {
m.Called()
}

func (m *mockContext) Forward(pid *PID) {
m.Called()
}

func (m *mockContext) Request(pid *PID, message interface{}) {
m.Called()
}
Expand Down
3 changes: 3 additions & 0 deletions actor/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type Context interface {
//Tell sends a message to the given PID
Tell(pid *PID, message interface{})

//Forward forwards current message to the given PID
Forward(pid *PID)

//Request sends a message to the given PID and also provides a Sender PID
Request(pid *PID, message interface{})

Expand Down
9 changes: 9 additions & 0 deletions actor/local_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ func (ctx *localContext) Tell(pid *PID, message interface{}) {
ctx.sendUserMessage(pid, message)
}

func (ctx *localContext) Forward(pid *PID) {
if msg, ok := ctx.Message().(SystemMessage); ok {
// SystemMessage cannot be forwarded
plog.Error("SystemMessage cannot be forwarded", log.Message(msg))
return
}
ctx.sendUserMessage(pid, ctx.message)
}

func (ctx *localContext) sendUserMessage(pid *PID, message interface{}) {
if ctx.outboundMiddleware != nil {
if env, ok := message.(*MessageEnvelope); ok {
Expand Down
32 changes: 32 additions & 0 deletions actor/local_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,38 @@ func TestLocalContext_Respond(t *testing.T) {
eventstream.Unsubscribe(deadLetterSubscriber)
}

func TestLocalContext_Forward(t *testing.T) {

// Defined a respond actor
// It simply respond the string message
responder := Spawn(FromFunc(func(ctx Context) {
switch m := ctx.Message().(type) {
case string:
ctx.Respond(fmt.Sprintf("Got a string: %s", m))
}
}))

// Defined a forwarder actor
// It simply forward the string message to responder
forwarder := Spawn(FromFunc(func(ctx Context) {
switch ctx.Message().(type) {
case string:
ctx.Forward(responder)
}
}))

// Send a message to the responder using Request
// The responder should send something back.
timeout := 3 * time.Millisecond
res, err := forwarder.RequestFuture("hello", timeout).Result()
assert.Nil(t, err)
assert.NotNil(t, res)

resStr, ok := res.(string)
assert.True(t, ok)
assert.Equal(t, "Got a string: hello", resStr)
}

func BenchmarkLocalContext_ProcessMessageWithMiddleware(b *testing.B) {
var m interface{} = 1

Expand Down

0 comments on commit c0ef81a

Please sign in to comment.