github.com/moleculer-go/moleculer@v0.3.3/registry/actionCatalog_test.go (about) 1 package registry_test 2 3 import ( 4 "github.com/moleculer-go/moleculer" 5 "github.com/moleculer-go/moleculer/registry" 6 "github.com/moleculer-go/moleculer/service" 7 "github.com/moleculer-go/moleculer/strategy" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 log "github.com/sirupsen/logrus" 11 ) 12 13 var _ = Describe("Actions Catalog", func() { 14 logger := log.WithField("unit test pkg", "registry_test") 15 strategy := strategy.RandomStrategy{} 16 params := moleculer.ObjectSchema{nil} 17 node1 := registry.CreateNode("node-test-1", true, logger) 18 node2 := registry.CreateNode("node-test-2", true, logger) 19 handler := func(ctx moleculer.Context, params moleculer.Payload) interface{} { 20 return "default action result" 21 } 22 bankCreditAction := service.CreateServiceAction("bank", "credit", handler, params) 23 24 Describe("Invoking Actions ", func() { 25 It("Should find next action by name", func() { 26 27 msg := "message from action" 28 catalog := registry.CreateActionCatalog(logger) 29 peopleCreate := func(ctx moleculer.Context, params moleculer.Payload) interface{} { 30 return msg 31 } 32 testService := service.Service{} 33 testService.SetNodeID(node1.GetID()) 34 testAction := service.CreateServiceAction("people", "create", peopleCreate, params) 35 36 catalog.Add(testAction, &testService, true) 37 38 actionName := "people.create" 39 actionEntry := catalog.Next(actionName, strategy) 40 Expect(actionEntry).Should(Not(BeNil())) 41 42 }) 43 }) 44 45 Describe("Actions Catalog - Add, Next and NextEndpointFromNode", func() { 46 //broker := CreateBroker() 47 It("Should create a ActionCatalog and should be size 0", func() { 48 49 catalog := registry.CreateActionCatalog(logger) 50 51 Expect(catalog).Should(Not(BeNil())) 52 53 //Expect(catalog.Size()).Should(Equal(0)) 54 55 }) 56 57 It("Should add a local action to Action Catalog", func() { 58 59 catalog := registry.CreateActionCatalog(logger) 60 61 nextActionEntry := catalog.Next("bank.credit", strategy) 62 Expect(nextActionEntry).Should(BeNil()) 63 testService := service.Service{} 64 testService.SetNodeID(node1.GetID()) 65 catalog.Add(bankCreditAction, &testService, true) 66 67 nextActionEntry = catalog.Next("bank.credit", strategy) 68 Expect(nextActionEntry).Should(Not(BeNil())) 69 Expect(nextActionEntry.IsLocal()).Should(Equal(true)) 70 }) 71 72 It("Should add actions and return using Next and NextEndpointFromNode", func() { 73 74 catalog := registry.CreateActionCatalog(logger) 75 76 nextAction := catalog.Next("bank.credit", strategy) 77 Expect(nextAction).Should(BeNil()) 78 79 testService := service.Service{} 80 testService.SetNodeID(node1.GetID()) 81 catalog.Add(bankCreditAction, &testService, true) 82 83 nextAction = catalog.Next("bank.credit", strategy) 84 Expect(nextAction).Should(Not(BeNil())) 85 Expect(nextAction.IsLocal()).Should(Equal(true)) 86 87 nextAction = catalog.Next("user.signUp", strategy) 88 Expect(nextAction).Should(BeNil()) 89 90 catalog.Add(service.CreateServiceAction("user", "signUp", handler, params), &testService, true) 91 92 nextAction = catalog.Next("user.signUp", strategy) 93 Expect(nextAction).Should(Not(BeNil())) 94 Expect(nextAction.IsLocal()).Should(Equal(true)) 95 96 testService.SetNodeID(node2.GetID()) 97 catalog.Add(service.CreateServiceAction("user", "signUp", handler, params), &testService, false) 98 99 //local action on node 1 100 nextAction = catalog.NextFromNode("user.signUp", node1.GetID()) 101 Expect(nextAction).Should(Not(BeNil())) 102 Expect(nextAction.IsLocal()).Should(Equal(true)) 103 104 //remote action on node 2 105 nextAction = catalog.NextFromNode("user.signUp", node2.GetID()) 106 Expect(nextAction).Should(Not(BeNil())) 107 Expect(nextAction.IsLocal()).Should(Equal(false)) 108 109 //invalid node id 110 nextAction = catalog.NextFromNode("user.signUp", "invalid node id") 111 Expect(nextAction).Should(BeNil()) 112 }) 113 114 }) 115 116 })