github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/middleware_chain_test.go (about) 1 package actor 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func middleware(called *int) ReceiverMiddleware { 10 return func(next ReceiverFunc) ReceiverFunc { 11 fn := func(ctx ReceiverContext, env *MessageEnvelope) { 12 env.Message = env.Message.(int) + 1 13 *called = env.Message.(int) 14 15 next(ctx, env) 16 } 17 return fn 18 } 19 } 20 21 func TestMakeReceiverMiddleware_CallsInCorrectOrder(t *testing.T) { 22 var c [3]int 23 24 r := []ReceiverMiddleware{ 25 middleware(&c[0]), 26 middleware(&c[1]), 27 middleware(&c[2]), 28 } 29 30 mc := &mockContext{} 31 32 env := &MessageEnvelope{ 33 Message: 0, 34 } 35 36 chain := makeReceiverMiddlewareChain(r, func(receiver ReceiverContext, env *MessageEnvelope) {}) 37 chain(mc, env) 38 39 assert.Equal(t, 1, c[0]) 40 assert.Equal(t, 2, c[1]) 41 assert.Equal(t, 3, c[2]) 42 } 43 44 func TestMakeInboundMiddleware_ReturnsNil(t *testing.T) { 45 assert.Nil(t, makeReceiverMiddlewareChain([]ReceiverMiddleware{}, func(_ ReceiverContext, _ *MessageEnvelope) {})) 46 }