github.com/moleculer-go/moleculer@v0.3.3/transit/pubsub/pubsub_test.go (about) 1 package pubsub 2 3 import ( 4 bus "github.com/moleculer-go/goemitter" 5 "github.com/moleculer-go/moleculer" 6 "github.com/moleculer-go/moleculer/serializer" 7 "github.com/moleculer-go/moleculer/service" 8 "github.com/moleculer-go/moleculer/test" 9 "github.com/moleculer-go/moleculer/transit" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 log "github.com/sirupsen/logrus" 13 ) 14 15 var logLevel = "ERROR" 16 17 var _ = Describe("PubSub Internals", func() { 18 19 It("Should return the number of neighbours", func() { 20 21 pubsub := PubSub{knownNeighbours: map[string]int64{ 22 "x": int64(10), 23 "y": int64(10), 24 "z": int64(10), 25 }} 26 27 Expect(pubsub.neighbours()).Should(BeEquivalentTo(3)) 28 }) 29 30 It("onServiceAdded should call broadcastNodeInfo for local service", func() { 31 localNode := test.NodeMock{ID: "test", ExportAsMapResult: map[string]interface{}{}} 32 mockT := &mockTransporter{} 33 svc := service.Service{} 34 svc.SetNodeID(localNode.GetID()) 35 pubsub := PubSub{ 36 isConnected: true, 37 serializer: &serializer.JSONSerializer{}, 38 broker: &moleculer.BrokerDelegates{ 39 LocalNode: func() moleculer.Node { 40 return &localNode 41 }, 42 InstanceID: func() string { 43 return "instance-id" 44 }, 45 }, 46 transport: mockT, 47 brokerStarted: true, 48 } 49 pubsub.onServiceAdded(svc.Summary()) 50 Expect(mockT.PublishCalled).Should(BeTrue()) 51 }) 52 53 It("onServiceAdded shouldn't call broadcastNodeInfo for remote service", func() { 54 localNode := test.NodeMock{ID: "test", ExportAsMapResult: map[string]interface{}{}} 55 mockT := &mockTransporter{} 56 svc := service.Service{} 57 svc.SetNodeID("test-remote") 58 pubsub := PubSub{ 59 isConnected: true, 60 serializer: &serializer.JSONSerializer{}, 61 broker: &moleculer.BrokerDelegates{ 62 LocalNode: func() moleculer.Node { 63 return &localNode 64 }, 65 }, 66 transport: mockT, 67 brokerStarted: true, 68 } 69 pubsub.onServiceAdded(svc.Summary()) 70 Expect(mockT.PublishCalled).Should(BeFalse()) 71 }) 72 73 It("onBrokerStarted should call broadcastNodeInfo", func() { 74 localNode := test.NodeMock{ID: "test", ExportAsMapResult: map[string]interface{}{}} 75 mockT := &mockTransporter{} 76 pubsub := PubSub{ 77 isConnected: true, 78 serializer: &serializer.JSONSerializer{}, 79 broker: &moleculer.BrokerDelegates{ 80 LocalNode: func() moleculer.Node { 81 return &localNode 82 }, 83 InstanceID: func() string { 84 return "instance-id" 85 }, 86 }, 87 transport: mockT, 88 } 89 pubsub.onBrokerStarted() 90 Expect(mockT.PublishCalled).Should(BeTrue()) 91 }) 92 93 It("should create a new pubsub Create()", func() { 94 localNode := test.NodeMock{ID: "test", ExportAsMapResult: map[string]interface{}{}} 95 pubsub := Create(&moleculer.BrokerDelegates{ 96 Logger: func(name string, value string) *log.Entry { return log.WithField(name, value) }, 97 LocalNode: func() moleculer.Node { 98 return &localNode 99 }, 100 Bus: func() *bus.Emitter { 101 return bus.Construct() 102 }, 103 }) 104 Expect(pubsub).ShouldNot(BeNil()) 105 }) 106 107 It("should find a pending request by nodeID)", func() { 108 //TODO 109 }) 110 }) 111 112 type mockTransporter struct { 113 SubscribeCalled bool 114 PublishCalled bool 115 } 116 117 func (t *mockTransporter) Connect() chan error { 118 return nil 119 } 120 121 func (t *mockTransporter) Disconnect() chan error { 122 return nil 123 } 124 125 func (t *mockTransporter) Subscribe(command string, nodeID string, handler transit.TransportHandler) { 126 t.SubscribeCalled = true 127 } 128 func (t *mockTransporter) Publish(command, nodeID string, message moleculer.Payload) { 129 t.PublishCalled = true 130 } 131 132 func (t *mockTransporter) SetPrefix(string) { 133 } 134 135 func (t *mockTransporter) SetNodeID(string) { 136 } 137 138 func (t *mockTransporter) SetSerializer(serializer.Serializer) { 139 }