github.com/moleculer-go/moleculer@v0.3.3/service/service_test.go (about) 1 package service_test 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/moleculer-go/moleculer/test" 8 9 log "github.com/sirupsen/logrus" 10 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 14 "github.com/moleculer-go/moleculer" 15 "github.com/moleculer-go/moleculer/service" 16 ) 17 18 var logger = log.WithField("Unit Test", true) 19 20 type MathService struct { 21 } 22 23 func (s MathService) Name() string { 24 return "math" 25 } 26 27 func (s *MathService) Add(params moleculer.Payload) int { 28 return params.Get("a").Int() + params.Get("b").Int() 29 } 30 31 func (s *MathService) Sub(a int, b int) int { 32 return a - b 33 } 34 35 var _ = Describe("moleculer/service", func() { 36 37 moonMixIn := moleculer.Mixin{ 38 Name: "moon", 39 Settings: map[string]interface{}{ 40 "craters": true, 41 "round": true, 42 }, 43 Metadata: map[string]interface{}{ 44 "resolution": "high", 45 }, Actions: []moleculer.Action{ 46 { 47 Name: "tide", 48 Handler: func(ctx moleculer.Context, params moleculer.Payload) interface{} { 49 return "tide influence in the oceans" 50 }, 51 }, 52 }, 53 Events: []moleculer.Event{ 54 { 55 Name: "earth.rotates", 56 Handler: func(ctx moleculer.Context, params moleculer.Payload) { 57 fmt.Println("update tide in relation to the moon") 58 }, 59 }, 60 { 61 Name: "moon.isClose", 62 Handler: func(ctx moleculer.Context, params moleculer.Payload) { 63 fmt.Println("rise the tide !") 64 }, 65 }, 66 }, 67 } 68 69 serviceSchema := moleculer.ServiceSchema{ 70 Name: "earth", 71 Version: "0.2", 72 Settings: map[string]interface{}{ 73 "dinosauros": true, 74 "round": false, 75 }, 76 Metadata: map[string]interface{}{ 77 "star-system": "sun", 78 }, 79 Mixins: []moleculer.Mixin{moonMixIn}, 80 Actions: []moleculer.Action{ 81 { 82 Name: "rotate", 83 Handler: func(ctx moleculer.Context, params moleculer.Payload) interface{} { 84 return "Hellow Leleu ;) I'm rotating ..." 85 }, 86 }, 87 }, 88 Events: []moleculer.Event{ 89 { 90 Name: "earth.rotates", 91 Handler: func(ctx moleculer.Context, params moleculer.Payload) { 92 fmt.Println("spining spining spining") 93 }, 94 }, 95 }, 96 } 97 98 It("Should merge and overwrite existing actions", func() { 99 100 svcCreatedCalled := false 101 serviceSchema.Created = func(svc moleculer.ServiceSchema, log *log.Entry) { 102 svcCreatedCalled = true 103 } 104 svcStartedCalled := false 105 serviceSchema.Started = func(ctx moleculer.BrokerContext, svc moleculer.ServiceSchema) { 106 svcStartedCalled = true 107 } 108 svcStoppedCalled := false 109 serviceSchema.Stopped = func(ctx moleculer.BrokerContext, svc moleculer.ServiceSchema) { 110 svcStoppedCalled = true 111 } 112 113 mixCreatedCalled := false 114 serviceSchema.Mixins[0].Created = func(svc moleculer.ServiceSchema, log *log.Entry) { 115 mixCreatedCalled = true 116 } 117 mixStartedCalled := false 118 serviceSchema.Mixins[0].Started = func(ctx moleculer.BrokerContext, svc moleculer.ServiceSchema) { 119 mixStartedCalled = true 120 } 121 mixStoppedCalled := false 122 serviceSchema.Mixins[0].Stopped = func(ctx moleculer.BrokerContext, svc moleculer.ServiceSchema) { 123 mixStoppedCalled = true 124 } 125 126 svc := service.FromSchema(serviceSchema, test.DelegatesWithId("test")) 127 name := svc.Name() 128 Expect(name).Should(Equal(serviceSchema.Name)) 129 Expect(name).Should(Not(Equal(moonMixIn.Name))) 130 131 Expect(len(svc.Actions())).Should(Equal(2)) 132 Expect(svc.Actions()[0].Name()).Should(Equal("rotate")) 133 Expect(svc.Actions()[1].Name()).Should(Equal("tide")) 134 135 Expect(len(svc.Events())).Should(Equal(2)) 136 Expect(svc.Events()[0].Name()).Should(Equal("earth.rotates")) 137 Expect(svc.Events()[1].Name()).Should(Equal("moon.isClose")) 138 139 Expect(len(svc.Settings())).Should(Equal(3)) 140 Expect(svc.Settings()["craters"]).Should(Equal(true)) 141 Expect(svc.Settings()["dinosauros"]).Should(Equal(true)) 142 Expect(svc.Settings()["round"]).Should(Equal(false)) 143 144 svc.Start(nil) 145 svc.Stop(nil) 146 time.Sleep(time.Millisecond * 100) 147 Expect(svcCreatedCalled).Should(BeTrue()) 148 Expect(svcStartedCalled).Should(BeTrue()) 149 Expect(svcStoppedCalled).Should(BeTrue()) 150 Expect(mixCreatedCalled).Should(BeTrue()) 151 Expect(mixStartedCalled).Should(BeTrue()) 152 Expect(mixStoppedCalled).Should(BeTrue()) 153 }) 154 155 It("Should publish a service that is an object (not an schema)", func() { 156 math := MathService{} 157 svc, err := service.FromObject(math, test.DelegatesWithId("test")) 158 Expect(err).Should(BeNil()) 159 Expect(svc.Name()).Should(Equal(math.Name())) 160 }) 161 162 })