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

     1  package amqp
     2  
     3  import (
     4  	"github.com/moleculer-go/moleculer"
     5  	"github.com/moleculer-go/moleculer/broker"
     6  	"github.com/moleculer-go/moleculer/strategy"
     7  	log "github.com/sirupsen/logrus"
     8  	"github.com/streadway/amqp"
     9  	"os"
    10  	"strconv"
    11  	"time"
    12  )
    13  
    14  func amqpTestHost() string {
    15  	env := os.Getenv("AMQP_HOST")
    16  	if env == "" {
    17  		return "localhost"
    18  	}
    19  	return env
    20  }
    21  
    22  func purge(queues []string, exchanges []string, destroy bool) {
    23  	url := "amqp://" + amqpTestHost() + ":5672"
    24  
    25  	connection, err := amqp.Dial(url)
    26  	if err != nil {
    27  		panic(err)
    28  	}
    29  
    30  	channel, err := connection.Channel()
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  
    35  	if destroy {
    36  		for _, queue := range queues {
    37  			channel.QueueDelete(queue, false, false, false)
    38  		}
    39  
    40  		for _, exchange := range exchanges {
    41  			channel.ExchangeDelete(exchange, false, false)
    42  		}
    43  	} else {
    44  		for _, queue := range queues {
    45  			channel.QueuePurge(queue, false)
    46  		}
    47  	}
    48  
    49  	channel.Close()
    50  	connection.Close()
    51  }
    52  
    53  func merge(a, b map[string]interface{}) map[string]interface{} {
    54  	for key, value := range b {
    55  		a[key] = value
    56  	}
    57  
    58  	return a
    59  }
    60  
    61  func filter(logs *[]map[string]interface{}, t string) []map[string]interface{} {
    62  	var result []map[string]interface{}
    63  	for _, row := range *logs {
    64  		if row["type"].(string) == t {
    65  			result = append(result, row)
    66  		}
    67  	}
    68  
    69  	return result
    70  }
    71  
    72  func createNode(namespace, name string, service *moleculer.ServiceSchema) *broker.ServiceBroker {
    73  	logger := log.New()
    74  	logger.SetLevel(log.FatalLevel)
    75  
    76  	amqpConfig := AmqpOptions{
    77  		Url: []string{"amqp://" + amqpTestHost() + ":5672"},
    78  		Logger: logger.WithFields(log.Fields{
    79  			"Unit Test": true,
    80  			"transport": "amqp",
    81  		}),
    82  	}
    83  
    84  	b := broker.New(&moleculer.Config{
    85  		LogLevel: "Fatal",
    86  		// TODO: ignore, not implemented in broker
    87  		//Namespace: "test-" + namespace,
    88  		DiscoverNodeID: func() string {
    89  			return namespace + "-" + name
    90  		},
    91  		TransporterFactory: func() interface{} {
    92  			return CreateAmqpTransporter(amqpConfig)
    93  		},
    94  		StrategyFactory: func() interface{} {
    95  			return strategy.NewRoundRobinStrategy()
    96  		},
    97  	})
    98  
    99  	if service != nil {
   100  		b.Publish(*service)
   101  	}
   102  
   103  	return b
   104  }
   105  
   106  func createActionWorker(number int, logs *[]map[string]interface{}) *broker.ServiceBroker {
   107  	push := func(t string, params map[string]interface{}) {
   108  		*logs = append(*logs, merge(map[string]interface{}{
   109  			"type":      t,
   110  			"worker":    number,
   111  			"timestamp": time.Now(),
   112  		}, params))
   113  	}
   114  
   115  	service := moleculer.ServiceSchema{
   116  		Name: "test",
   117  		Actions: []moleculer.Action{
   118  			{
   119  				Name: "hello",
   120  				Handler: func(context moleculer.Context, params moleculer.Payload) interface{} {
   121  					delay := params.Get("delay").Int()
   122  
   123  					push("receive", params.RawMap())
   124  
   125  					time.Sleep(time.Millisecond * time.Duration(delay))
   126  
   127  					push("respond", params.RawMap())
   128  
   129  					return merge(map[string]interface{}{
   130  						"type":      "respond",
   131  						"worker":    number,
   132  						"timestamp": time.Now(),
   133  					}, params.RawMap())
   134  				},
   135  			},
   136  		},
   137  	}
   138  
   139  	return createNode("test-rpc", "worker"+strconv.Itoa(number), &service)
   140  }
   141  
   142  func createEmitWorker(name, serviceName string, logs *[]string) *broker.ServiceBroker {
   143  	var b *broker.ServiceBroker
   144  
   145  	service := moleculer.ServiceSchema{
   146  		Name: serviceName,
   147  		Events: []moleculer.Event{
   148  			{
   149  				Name: "hello.world2",
   150  				Handler: func(context moleculer.Context, params moleculer.Payload) {
   151  					if params.Get("testing").Bool() == true {
   152  						*logs = append(*logs, name)
   153  					}
   154  				},
   155  			},
   156  		},
   157  	}
   158  
   159  	b = createNode("test-emit", "event-"+name, &service)
   160  
   161  	return b
   162  }
   163  
   164  func createBroadcastWorker(name string, logs *[]string) *broker.ServiceBroker {
   165  	var b *broker.ServiceBroker
   166  
   167  	service := moleculer.ServiceSchema{
   168  		Name: name,
   169  		Events: []moleculer.Event{
   170  			{
   171  				Name: "hello.world",
   172  				Handler: func(context moleculer.Context, params moleculer.Payload) {
   173  					if params.Get("testing").Bool() == true {
   174  						*logs = append(*logs, name)
   175  					}
   176  				},
   177  			},
   178  		},
   179  	}
   180  
   181  	b = createNode("test-broadcast", "event-"+name, &service)
   182  
   183  	return b
   184  }