github.com/moleculer-go/moleculer@v0.3.3/broker/broker_test.go (about)

     1  package broker_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/moleculer-go/moleculer/transit/memory"
     8  	log "github.com/sirupsen/logrus"
     9  
    10  	"github.com/moleculer-go/moleculer"
    11  	"github.com/moleculer-go/moleculer/broker"
    12  
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Broker", func() {
    18  
    19  	It("Should make a local call and return results", func() {
    20  		actionResult := "abra cadabra"
    21  		service := moleculer.ServiceSchema{
    22  			Name: "do",
    23  			Actions: []moleculer.Action{
    24  				moleculer.Action{
    25  					Name: "stuff",
    26  					Handler: func(ctx moleculer.Context, params moleculer.Payload) interface{} {
    27  						return actionResult
    28  					},
    29  				},
    30  			},
    31  		}
    32  
    33  		broker := broker.New(&moleculer.Config{
    34  			LogLevel: "ERROR",
    35  		})
    36  		broker.Publish(service)
    37  		broker.Start()
    38  
    39  		result := <-broker.Call("do.stuff", 1)
    40  
    41  		fmt.Printf("Results from action: %s", result)
    42  
    43  		Expect(result.Value()).Should(Equal(actionResult))
    44  
    45  	})
    46  
    47  	It("Should make a local call, call should panic and returned paylod should contain the error", func() {
    48  		service := moleculer.ServiceSchema{
    49  			Name: "do",
    50  			Actions: []moleculer.Action{
    51  				moleculer.Action{
    52  					Name: "panic",
    53  					Handler: func(ctx moleculer.Context, params moleculer.Payload) interface{} {
    54  						if params.Bool() {
    55  							panic(errors.New("some random error..."))
    56  						}
    57  						return "no panic"
    58  					},
    59  				},
    60  			},
    61  		}
    62  		mem := &memory.SharedMemory{}
    63  		baseConfig := &moleculer.Config{
    64  			LogLevel: "error",
    65  			TransporterFactory: func() interface{} {
    66  				transport := memory.Create(log.WithField("transport", "memory"), mem)
    67  				return &transport
    68  			},
    69  		}
    70  		bkrConfig := &moleculer.Config{
    71  			DiscoverNodeID: func() string { return "do-broker" },
    72  		}
    73  		bkr := broker.New(baseConfig, bkrConfig)
    74  		bkr.Publish(service)
    75  		bkr.Start()
    76  
    77  		result := <-bkr.Call("do.panic", true)
    78  
    79  		Expect(result.IsError()).Should(Equal(true))
    80  		Expect(result.Error().Error()).Should(BeEquivalentTo("some random error..."))
    81  
    82  		service = moleculer.ServiceSchema{
    83  			Name:         "remote",
    84  			Dependencies: []string{"do"},
    85  			Actions: []moleculer.Action{
    86  				moleculer.Action{
    87  					Name: "panic",
    88  					Handler: func(ctx moleculer.Context, params moleculer.Payload) interface{} {
    89  						result := <-ctx.Call("do.panic", params)
    90  						ctx.Logger().Debug("params: ", params, " result: ", result.Value())
    91  						if result.IsError() {
    92  							panic(result.Error())
    93  						}
    94  						return result
    95  					},
    96  				},
    97  			},
    98  		}
    99  		bkrConfig = &moleculer.Config{
   100  			DiscoverNodeID: func() string { return "remote-broker" },
   101  		}
   102  		bkrRemote := broker.New(baseConfig, bkrConfig)
   103  		bkrRemote.Publish(service)
   104  		bkrRemote.Start()
   105  
   106  		bkrRemote.WaitFor("do")
   107  		result = <-bkrRemote.Call("remote.panic", true)
   108  
   109  		Expect(result.IsError()).Should(Equal(true))
   110  		Expect(result.Error().Error()).Should(BeEquivalentTo("some random error..."))
   111  
   112  		bkr.WaitFor("remote")
   113  		result = <-bkr.Call("remote.panic", false)
   114  
   115  		Expect(result.IsError()).Should(Equal(false))
   116  		Expect(result.String()).Should(BeEquivalentTo("no panic"))
   117  
   118  		bkrRemote.Stop()
   119  		bkr.Stop()
   120  	})
   121  
   122  	It("Should call multiple local calls (in chain)", func() {
   123  
   124  		actionResult := "step 1 done ! -> step 2: step 2 done ! -> magic: Just magic !!!"
   125  		service := moleculer.ServiceSchema{
   126  			Name: "machine",
   127  			Actions: []moleculer.Action{
   128  				moleculer.Action{
   129  					Name: "step1",
   130  					Handler: func(ctx moleculer.Context, params moleculer.Payload) interface{} {
   131  						step2Result := <-ctx.Call("machine.step2", 0)
   132  						return fmt.Sprintf("step 1 done ! -> step 2: %s", step2Result.String())
   133  					},
   134  				},
   135  				moleculer.Action{
   136  					Name: "step2",
   137  					Handler: func(ctx moleculer.Context, params moleculer.Payload) interface{} {
   138  						magicResult := <-ctx.Call("machine.magic", 0)
   139  						return fmt.Sprintf("step 2 done ! -> magic: %s", magicResult.String())
   140  					},
   141  				},
   142  				moleculer.Action{
   143  					Name: "magic",
   144  					Handler: func(ctx moleculer.Context, params moleculer.Payload) interface{} {
   145  						ctx.Emit("magic.happened, params", "Always !")
   146  						return "Just magic !!!"
   147  					},
   148  				},
   149  			},
   150  		}
   151  
   152  		broker := broker.New(&moleculer.Config{
   153  			LogLevel: "ERROR",
   154  		})
   155  		broker.Publish(service)
   156  		broker.Start()
   157  
   158  		result := <-broker.Call("machine.step1", 1)
   159  
   160  		fmt.Printf("Results from action: %s", result)
   161  
   162  		Expect(result.Value()).Should(Equal(actionResult))
   163  	})
   164  
   165  	It("Should make a local call and return results (service schema as pointer)", func() {
   166  		actionResult := "abra cadabra"
   167  		service := &moleculer.ServiceSchema{
   168  			Name: "do",
   169  			Actions: []moleculer.Action{
   170  				moleculer.Action{
   171  					Name: "stuff",
   172  					Handler: func(ctx moleculer.Context, params moleculer.Payload) interface{} {
   173  						return actionResult
   174  					},
   175  				},
   176  			},
   177  		}
   178  
   179  		broker := broker.New(&moleculer.Config{
   180  			LogLevel: "ERROR",
   181  		})
   182  		broker.Publish(service)
   183  		broker.Start()
   184  
   185  		result := <-broker.Call("do.stuff", 1)
   186  
   187  		fmt.Printf("Results from action: %s", result)
   188  
   189  		Expect(result.Value()).Should(Equal(actionResult))
   190  
   191  	})
   192  })