github.com/moleculer-go/moleculer@v0.3.3/middleware/middleware_test.go (about)

     1  package middleware
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/moleculer-go/moleculer/context"
     7  	"github.com/moleculer-go/moleculer/service"
     8  	"github.com/moleculer-go/moleculer/test"
     9  
    10  	"github.com/moleculer-go/moleculer"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  
    14  	bus "github.com/moleculer-go/goemitter"
    15  	log "github.com/sirupsen/logrus"
    16  )
    17  
    18  var logger = log.WithField("unit test pkg", "middleware_test")
    19  
    20  func createLogger(name string, value string) *log.Entry {
    21  	return logger.WithField(name, value)
    22  }
    23  
    24  func brokerDelegates(nodeID string) *moleculer.BrokerDelegates {
    25  	localBus := bus.Construct()
    26  	localNode := test.NodeMock{ID: nodeID}
    27  	broker := &moleculer.BrokerDelegates{
    28  		LocalNode: func() moleculer.Node {
    29  			return &localNode
    30  		},
    31  		Logger: createLogger,
    32  		Bus: func() *bus.Emitter {
    33  			return localBus
    34  		}}
    35  	return broker
    36  }
    37  
    38  var _ = Describe("Dispatcher", func() {
    39  
    40  	It("Add handlers and invoke them", func() {
    41  		dispatcher := Dispatcher(createLogger("midlewares", "dispatcher"))
    42  
    43  		hits := 0
    44  		handlers := make(map[string]moleculer.MiddlewareHandler)
    45  		handlers["serviceStarting"] = func(params interface{}, next func(...interface{})) {
    46  			service := params.(*service.Service)
    47  			fmt.Println("serviceStarting #1 -> service: ", service.Name())
    48  			hits = hits + 1
    49  			next()
    50  		}
    51  		dispatcher.Add(handlers)
    52  
    53  		handlers = make(map[string]moleculer.MiddlewareHandler)
    54  		handlers["serviceStarting"] = func(params interface{}, next func(...interface{})) {
    55  			service := params.(*service.Service)
    56  			fmt.Println("serviceStarting #2 -> service: ", service.Name())
    57  			hits = hits + 1
    58  			next()
    59  		}
    60  		dispatcher.Add(handlers)
    61  
    62  		svc := service.FromSchema(moleculer.ServiceSchema{Name: "serviceA"}, brokerDelegates("test-midlewares"))
    63  		resultSvc := dispatcher.CallHandlers("serviceStarting", svc)
    64  
    65  		Expect(hits).Should(Equal(2))
    66  		Expect(resultSvc).Should(BeEquivalentTo(svc))
    67  
    68  		hits = 0
    69  		handlers = make(map[string]moleculer.MiddlewareHandler)
    70  		handlers["beforeLocalAction"] = func(params interface{}, next func(...interface{})) {
    71  			context := params.(moleculer.BrokerContext)
    72  
    73  			hits = hits + 1
    74  			fmt.Println("beforeLocalAction -> context: ", context, " Payload: ", context.Payload())
    75  
    76  			context.SetTargetNodeID("changed id")
    77  
    78  			next(context)
    79  		}
    80  		dispatcher.Add(handlers)
    81  		initialContext := context.BrokerContext(brokerDelegates("test-midlewares"))
    82  		initialContext.SetTargetNodeID("initial id")
    83  		value := dispatcher.CallHandlers("beforeLocalAction", initialContext)
    84  		newContext := value.(moleculer.BrokerContext)
    85  		Expect(newContext.TargetNodeID()).Should(Equal("changed id"))
    86  		Expect(hits).Should(Equal(1))
    87  	})
    88  
    89  })