github.com/moleculer-go/moleculer@v0.3.3/transit/kafka/kafka_test.go (about)

     1  package kafka_test
     2  
     3  import (
     4  	"os"
     5  	"time"
     6  
     7  	"github.com/moleculer-go/moleculer"
     8  	"github.com/moleculer-go/moleculer/broker"
     9  	"github.com/moleculer-go/moleculer/context"
    10  	"github.com/moleculer-go/moleculer/payload"
    11  	"github.com/moleculer-go/moleculer/serializer"
    12  	"github.com/moleculer-go/moleculer/transit/kafka"
    13  	"github.com/moleculer-go/moleculer/transit/nats"
    14  	"github.com/moleculer-go/moleculer/util"
    15  
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  func kafkaTestHost() string {
    21  	env := os.Getenv("KAFKA_HOST")
    22  	if env == "" {
    23  		return "127.0.0.1"
    24  	}
    25  	return env
    26  }
    27  
    28  var KafkaTestHost = kafkaTestHost()
    29  
    30  var _ = Describe("Test Kafka Transit", func() {
    31  	brokerDelegates := BrokerDelegates()
    32  	contextA := context.BrokerContext(brokerDelegates)
    33  
    34  	stringSize := 50
    35  	arraySize := 100
    36  	var longList []interface{}
    37  	for i := 0; i < arraySize; i++ {
    38  		randomString := util.RandomString(stringSize)
    39  		longList = append(longList, randomString)
    40  	}
    41  
    42  	logLevel := "fatal"
    43  	kafkaHost := KafkaTestHost + ":9092"
    44  	kafkakUrl := "kafka://" + kafkaHost
    45  
    46  	CreateCommonTopics(kafkaHost)
    47  
    48  	time.Sleep(time.Second * 5)
    49  
    50  	var userBroker, profileBroker *broker.ServiceBroker
    51  	BeforeSuite(func() {
    52  
    53  		userBroker = broker.New(&moleculer.Config{
    54  			LogLevel:    logLevel,
    55  			Transporter: kafkakUrl,
    56  			DiscoverNodeID: func() string {
    57  				return "user_node"
    58  			},
    59  			RequestTimeout: 5 * time.Second,
    60  		})
    61  		userBroker.Publish(userService())
    62  
    63  		profileBroker = broker.New(&moleculer.Config{
    64  			LogLevel:    logLevel,
    65  			Transporter: kafkakUrl,
    66  			DiscoverNodeID: func() string {
    67  				return "profile_node"
    68  			},
    69  			RequestTimeout: 5 * time.Second,
    70  		})
    71  		profileBroker.Publish(profileService())
    72  
    73  		userBroker.Start()
    74  		profileBroker.Start()
    75  		profileBroker.WaitFor("user")
    76  		profileBroker.WaitForActions("user.update", "user.read")
    77  
    78  		time.Sleep(time.Second * 5)
    79  	})
    80  
    81  	AfterSuite(func() {
    82  		userBroker.Stop()
    83  		profileBroker.Stop()
    84  	})
    85  
    86  	Describe("Remote Calls", func() {
    87  
    88  		It("should make a remote call from profile broker a to user broker", func() {
    89  
    90  			resultUpdate := <-profileBroker.Call("user.update", longList)
    91  			Expect(resultUpdate.Error()).Should(Succeed())
    92  			Expect(len(resultUpdate.StringArray())).Should(Equal(arraySize + 1))
    93  
    94  			resultRead := <-profileBroker.Call("user.read", longList)
    95  			Expect(resultRead.Error()).Should(Succeed())
    96  			Expect(len(resultRead.StringArray())).Should(Equal(arraySize))
    97  		})
    98  
    99  		It("Should not fail on double disconnect", func() {
   100  			logger := contextA.Logger()
   101  			var serializer serializer.Serializer = serializer.CreateJSONSerializer(logger)
   102  			options := kafka.KafkaOptions{
   103  				Url:        kafkakUrl,
   104  				Name:       "test-cluster",
   105  				Logger:     logger,
   106  				Serializer: serializer,
   107  			}
   108  			transporter := kafka.CreateKafkaTransporter(options)
   109  			transporter.SetPrefix("MOL")
   110  			Expect(<-transporter.Connect()).Should(Succeed())
   111  			Expect(<-transporter.Disconnect()).Should(Succeed())
   112  			Expect(<-transporter.Disconnect()).Should(Succeed())
   113  		})
   114  
   115  		It("Should fail Subscribe() and Publish() when is not connected", func() {
   116  			transporter := kafka.CreateKafkaTransporter(kafka.KafkaOptions{})
   117  			Expect(func() { transporter.Subscribe("", "", func(moleculer.Payload) {}) }).Should(Panic())
   118  			Expect(func() { transporter.Publish("", "", payload.Empty()) }).Should(Panic())
   119  		})
   120  
   121  		It("Should fail to connect", func() {
   122  			logger := contextA.Logger()
   123  			var serializer serializer.Serializer = serializer.CreateJSONSerializer(logger)
   124  			options := nats.NATSOptions{
   125  				URL:        "some ivalid URL",
   126  				Name:       "test-cluster",
   127  				Logger:     logger,
   128  				Serializer: serializer,
   129  				ValidateMsg: func(msg moleculer.Payload) bool {
   130  					return true
   131  				},
   132  			}
   133  			transporter := nats.CreateNatsTransporter(options)
   134  			transporter.SetPrefix("MOL")
   135  			Expect(<-transporter.Connect()).ShouldNot(Succeed())
   136  		})
   137  
   138  		It("Should connect, subscribe, publish and disconnect", func() {
   139  			logger := contextA.Logger()
   140  			serializer := serializer.CreateJSONSerializer(logger)
   141  			options := kafka.KafkaOptions{
   142  				Url:        kafkakUrl,
   143  				Name:       "test-cluster",
   144  				Logger:     logger,
   145  				Serializer: serializer,
   146  			}
   147  
   148  			params := map[string]string{
   149  				"name":     "John",
   150  				"lastName": "Snow",
   151  			}
   152  
   153  			actionName := "some.service.action"
   154  			actionContext := contextA.ChildActionContext(actionName, payload.New(params))
   155  
   156  			node := "node1"
   157  			topicName := "REQ"
   158  
   159  			transporter := kafka.CreateKafkaTransporter(options)
   160  			transporter.SetSerializer(serializer)
   161  			transporter.SetPrefix("MOL")
   162  			transporter.SetNodeID(node)
   163  			Expect(<-transporter.Connect()).Should(Succeed())
   164  
   165  			received := make(chan bool)
   166  			done := make(chan bool)
   167  
   168  			transporter.Subscribe(topicName, node, func(message moleculer.Payload) {
   169  
   170  				contextMap := serializer.PayloadToContextMap(message)
   171  
   172  				newContext := context.ActionContext(brokerDelegates, contextMap)
   173  				Expect(newContext.ActionName()).Should(Equal(actionName))
   174  				contextParams := newContext.Payload()
   175  				Expect(contextParams.Get("name").String()).Should(Equal("John"))
   176  				Expect(contextParams.Get("lastName").String()).Should(Equal("Snow"))
   177  
   178  				close(done)
   179  				received <- true
   180  			})
   181  
   182  			contextMap := actionContext.AsMap()
   183  			contextMap["sender"] = "someone"
   184  			msg, _ := serializer.MapToPayload(&contextMap)
   185  
   186  			transporter.Publish(topicName, node, msg)
   187  
   188  			Eventually(done, "5s").Should(BeClosed())
   189  			Expect(<-received).Should(Equal(true))
   190  
   191  			Expect(<-transporter.Disconnect()).Should(Succeed())
   192  		})
   193  	})
   194  
   195  })