github.com/moleculer-go/moleculer@v0.3.3/transit/nats/common_test.go (about) 1 package nats_test 2 3 import ( 4 . "github.com/moleculer-go/goemitter" 5 "github.com/moleculer-go/moleculer" 6 7 "github.com/moleculer-go/moleculer/broker" 8 "github.com/moleculer-go/moleculer/registry" 9 log "github.com/sirupsen/logrus" 10 ) 11 12 var logger = log.WithField("unit test pkg", "transit_test") 13 14 func CreateLogger(name string, value string) *log.Entry { 15 return logger.WithField(name, value) 16 } 17 18 var localNode = registry.CreateNode("unit-test-node", true, logger) 19 20 func BrokerDelegates() *moleculer.BrokerDelegates { 21 localBus := Construct() 22 broker := &moleculer.BrokerDelegates{ 23 LocalNode: func() moleculer.Node { 24 return localNode 25 }, 26 Logger: CreateLogger, 27 Bus: func() *Emitter { 28 return localBus 29 }} 30 return broker 31 } 32 33 func userService() moleculer.ServiceSchema { 34 return moleculer.ServiceSchema{ 35 Name: "user", 36 Actions: []moleculer.Action{ 37 { 38 Name: "update", 39 Handler: func(context moleculer.Context, params moleculer.Payload) interface{} { 40 list := params.StringArray() 41 list = append(list, "user update") 42 return list 43 }, 44 }, 45 }, 46 } 47 } 48 49 func contactService() moleculer.ServiceSchema { 50 return moleculer.ServiceSchema{ 51 Name: "contact", 52 Actions: []moleculer.Action{ 53 { 54 Name: "update", 55 Handler: func(context moleculer.Context, params moleculer.Payload) interface{} { 56 list := params.StringArray() 57 list = append(list, "contact update") 58 return list 59 }, 60 }, 61 }, 62 } 63 } 64 65 func profileService() moleculer.ServiceSchema { 66 return moleculer.ServiceSchema{ 67 Name: "profile", 68 Dependencies: []string{"user", "contact"}, 69 Actions: []moleculer.Action{ 70 { 71 Name: "update", 72 Handler: func(context moleculer.Context, params moleculer.Payload) interface{} { 73 paramsList := params.StringArray() 74 75 contactList := <-context.Call("contact.update", []string{"profile update"}) 76 userUpdate := <-context.Call("user.update", contactList) 77 78 userList := userUpdate.StringArray() 79 for _, item := range paramsList { 80 userList = append(userList, item) 81 } 82 83 return userList 84 }, 85 }, 86 }, 87 } 88 } 89 90 func stopBrokers(brokers ...*broker.ServiceBroker) { 91 for _, bkr := range brokers { 92 bkr.Stop() 93 } 94 }