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

     1  package kafka_test
     2  
     3  import (
     4  	. "github.com/moleculer-go/goemitter"
     5  	"github.com/moleculer-go/moleculer"
     6  	"github.com/segmentio/kafka-go"
     7  
     8  	"github.com/moleculer-go/moleculer/registry"
     9  	log "github.com/sirupsen/logrus"
    10  )
    11  
    12  var commonTopics = []string{
    13  	"MOL.DISCONNECT",
    14  	"MOL.DISCOVER",
    15  	"MOL.HEARTBEAT",
    16  	"MOL.INFO",
    17  	"MOL.PING",
    18  
    19  	"MOL.DISCONNECT.user_node",
    20  	"MOL.DISCONNECT.profile_node",
    21  	"MOL.DISCONNECT.node1",
    22  	"MOL.DISCOVER.user_node",
    23  	"MOL.DISCOVER.profile_node",
    24  	"MOL.DISCOVER.node1",
    25  	"MOL.EVENT.user_node",
    26  	"MOL.EVENT.profile_node",
    27  	"MOL.EVENT.node1",
    28  	"MOL.HEARTBEAT.user_node",
    29  	"MOL.HEARTBEAT.profile_node",
    30  	"MOL.HEARTBEAT.node1",
    31  	"MOL.INFO.user_node",
    32  	"MOL.INFO.profile_node",
    33  	"MOL.INFO.node1",
    34  	"MOL.PING.user_node",
    35  	"MOL.PING.profile_node",
    36  	"MOL.PING.node1",
    37  	"MOL.PONG.user_node",
    38  	"MOL.PONG.profile_node",
    39  	"MOL.PONG.node1",
    40  	"MOL.REQ.user_node",
    41  	"MOL.REQ.profile_node",
    42  	"MOL.REQ.node1",
    43  	"MOL.RES.user_node",
    44  	"MOL.RES.profile_node",
    45  	"MOL.RES.node1",
    46  }
    47  
    48  func CreateCommonTopics(kafkaHost string) {
    49  	conn, err := kafka.Dial("tcp", kafkaHost)
    50  	if err != nil {
    51  		panic(err.Error())
    52  	}
    53  	defer conn.Close()
    54  	var topicConfigs []kafka.TopicConfig
    55  	for _, topic := range commonTopics {
    56  		topicConfigs = append(topicConfigs, kafka.TopicConfig{
    57  			Topic:             topic,
    58  			NumPartitions:     1,
    59  			ReplicationFactor: 1,
    60  		})
    61  	}
    62  	err = conn.CreateTopics(topicConfigs...)
    63  	if err != nil {
    64  		panic(err.Error())
    65  	}
    66  }
    67  
    68  var logger = log.WithField("unit test pkg", "transit_test")
    69  
    70  func CreateLogger(name string, value string) *log.Entry {
    71  	return logger.WithField(name, value)
    72  }
    73  
    74  var localNode = registry.CreateNode("unit-test-node", true, logger)
    75  
    76  func BrokerDelegates() *moleculer.BrokerDelegates {
    77  	localBus := Construct()
    78  	broker := &moleculer.BrokerDelegates{
    79  		LocalNode: func() moleculer.Node {
    80  			return localNode
    81  		},
    82  		Logger: CreateLogger,
    83  		Bus: func() *Emitter {
    84  			return localBus
    85  		}}
    86  	return broker
    87  }
    88  
    89  func userService() moleculer.ServiceSchema {
    90  	return moleculer.ServiceSchema{
    91  		Name: "user",
    92  		Actions: []moleculer.Action{
    93  			{
    94  				Name: "update",
    95  				Handler: func(context moleculer.Context, params moleculer.Payload) interface{} {
    96  					list := params.StringArray()
    97  					list = append(list, "user update")
    98  					return list
    99  				},
   100  			},
   101  			{
   102  				Name: "read",
   103  				Handler: func(context moleculer.Context, params moleculer.Payload) interface{} {
   104  					list := params.StringArray()
   105  					return list
   106  				},
   107  			},
   108  		},
   109  	}
   110  }
   111  
   112  func profileService() moleculer.ServiceSchema {
   113  	return moleculer.ServiceSchema{
   114  		Name:         "profile",
   115  		Dependencies: []string{"user"},
   116  		Actions: []moleculer.Action{
   117  			{
   118  				Name: "update",
   119  				Handler: func(context moleculer.Context, params moleculer.Payload) interface{} {
   120  					paramsList := params.StringArray()
   121  
   122  					var payload []interface{}
   123  					userUpdate := <-context.Call("user.update", payload)
   124  
   125  					userList := userUpdate.StringArray()
   126  					for _, item := range paramsList {
   127  						userList = append(userList, item)
   128  					}
   129  
   130  					return userList
   131  				},
   132  			},
   133  		},
   134  	}
   135  }