github.com/moleculer-go/moleculer@v0.3.3/registry/nodeService_test.go (about)

     1  package registry_test
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/moleculer-go/moleculer"
     8  	"github.com/moleculer-go/moleculer/test"
     9  	"github.com/moleculer-go/moleculer/transit/memory"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  func cleanupNode(in map[string]interface{}) map[string]interface{} {
    15  	if in == nil {
    16  		return nil
    17  	}
    18  	if len(in) == 0 {
    19  		return make(map[string]interface{})
    20  	}
    21  	in["ipList"] = []string{"100.100.0.100"}
    22  	in["hostname"] = "removed"
    23  	in["seq"] = "removed"
    24  	return in
    25  }
    26  
    27  func cleanupAction(ins []map[string]interface{}) []map[string]interface{} {
    28  	result := make([]map[string]interface{}, len(ins))
    29  	for index, item := range ins {
    30  		_, endpointsExists := item["endpoints"]
    31  		result[index] = map[string]interface{}{
    32  			"name":      item["name"],
    33  			"count":     "removed",
    34  			"hasLocal":  item["hasLocal"],
    35  			"available": item["available"],
    36  			"endpoints": endpointsExists,
    37  		}
    38  	}
    39  	return result
    40  }
    41  
    42  func first(list []map[string]interface{}) map[string]interface{} {
    43  	if list != nil && len(list) > 0 {
    44  		return list[0]
    45  	}
    46  	return nil
    47  }
    48  
    49  func orderEndpoints(list []map[string]interface{}) []map[string]interface{} {
    50  	result := make([]map[string]interface{}, len(list))
    51  	for idx, item := range list {
    52  		endpointsTemp, exists := item["endpoints"]
    53  		if exists {
    54  			endpoints := endpointsTemp.([]map[string]interface{})
    55  			item["endpoints"] = test.OrderMapArray(endpoints, "nodeID")
    56  		}
    57  		result[idx] = item
    58  	}
    59  	return result
    60  }
    61  
    62  func findBy(field, value string, list []moleculer.Payload) []map[string]interface{} {
    63  	result := make([]map[string]interface{}, 0)
    64  	for _, item := range list {
    65  		if item.Get(field).String() == value {
    66  			result = append(result, item.RawMap())
    67  		}
    68  	}
    69  	return result
    70  }
    71  
    72  var _ = Describe("nodeService", func() {
    73  	Describe("Local Service $node", func() {
    74  		harness := func(action string, scenario string, params map[string]interface{}, transformer func(interface{}) interface{}) func() {
    75  			label := fmt.Sprint(scenario, "-", action)
    76  			return func() {
    77  				mem := &memory.SharedMemory{}
    78  
    79  				printerBroker := createPrinterBroker(mem)
    80  				printerBroker.Start()
    81  
    82  				result := <-printerBroker.Call(action, params)
    83  				Expect(result.Exists()).Should(BeTrue())
    84  				Expect(snap.SnapshotMulti(fmt.Sprint(label, "printerBroker"), transformer(result))).Should(Succeed())
    85  
    86  				scannerBroker := createScannerBroker(mem)
    87  				scannerBroker.Start()
    88  				scannerBroker.WaitForNodes("node_printerBroker")
    89  				scannerBroker.WaitFor("printer")
    90  				time.Sleep(time.Millisecond)
    91  
    92  				result = <-scannerBroker.Call(action, params)
    93  				Expect(result.Exists()).Should(BeTrue())
    94  				Expect(snap.SnapshotMulti(fmt.Sprint(label, "scannerBroker"), transformer(result))).Should(Succeed())
    95  
    96  				cpuBroker := createCpuBroker(mem)
    97  				cpuBroker.Start()
    98  				cpuBroker.WaitForNodes("node_printerBroker", "node_scannerBroker")
    99  				cpuBroker.WaitFor("printer", "scanner")
   100  				time.Sleep(time.Millisecond)
   101  				result = <-cpuBroker.Call(action, params)
   102  				Expect(result.Exists()).Should(BeTrue())
   103  				Expect(snap.SnapshotMulti(fmt.Sprint(label, "cpuBroker"), transformer(result))).Should(Succeed()) //failed here perdeu o node priunter
   104  			}
   105  		}
   106  
   107  		Context("$node.list action", func() {
   108  
   109  			extractNodes := func(in interface{}) interface{} {
   110  				list := in.(moleculer.Payload).Array()
   111  				return map[string]map[string]interface{}{
   112  					"nodePrinterBroker": cleanupNode(first(findBy("id", "node_printerBroker", list))),
   113  					"nodeScannerBroker": cleanupNode(first(findBy("id", "node_scannerBroker", list))),
   114  					"nodeCpuBroker":     cleanupNode(first(findBy("id", "node_cpuBroker", list))),
   115  				}
   116  			}
   117  
   118  			extractServices := func(in interface{}) interface{} {
   119  				list := in.(moleculer.Payload).Array()
   120  				return [][]map[string]interface{}{
   121  					orderEndpoints(findBy("name", "printer", list)),
   122  					findBy("name", "scanner", list),
   123  					findBy("name", "cpu", list),
   124  					findBy("name", "$node", list),
   125  				}
   126  			}
   127  
   128  			extractActions := func(in interface{}) interface{} {
   129  				list := in.(moleculer.Payload).Array()
   130  				return [][]map[string]interface{}{
   131  					cleanupAction(findBy("name", "printer.print", list)),
   132  					cleanupAction(findBy("name", "scanner.scan", list)),
   133  					cleanupAction(findBy("name", "cpu.compute", list)),
   134  					cleanupAction(findBy("name", "$node.list", list)),
   135  					cleanupAction(findBy("name", "$node.services", list)),
   136  					cleanupAction(findBy("name", "$node.actions", list)),
   137  					cleanupAction(findBy("name", "$node.events", list)),
   138  				}
   139  			}
   140  
   141  			extractEvents := func(in interface{}) interface{} {
   142  				list := in.(moleculer.Payload).Array()
   143  				return [][]map[string]interface{}{
   144  					cleanupAction(findBy("name", "printer.printed", list)),
   145  					cleanupAction(findBy("name", "scanner.scanned", list)),
   146  				}
   147  			}
   148  
   149  			timeout := 4.0
   150  
   151  			It("$node.events - all false", harness("$node.events", "all-false", map[string]interface{}{
   152  				"withEndpoints": false,
   153  				"onlyAvailable": false,
   154  				"onlyLocal":     false,
   155  			}, extractEvents), timeout)
   156  
   157  			It("$node.events - all true", harness("$node.events", "all-true", map[string]interface{}{
   158  				"withEndpoints": true,
   159  				"onlyAvailable": true,
   160  				"onlyLocal":     true,
   161  			}, extractEvents), timeout)
   162  
   163  			It("$node.actions - all false", harness("$node.actions", "all-false", map[string]interface{}{
   164  				"withEndpoints": false,
   165  				"skipInternal":  false,
   166  				"onlyAvailable": false,
   167  				"onlyLocal":     false,
   168  			}, extractActions), timeout)
   169  
   170  			It("$node.actions - all true", harness("$node.actions", "all-true", map[string]interface{}{
   171  				"withEndpoints": true,
   172  				"skipInternal":  true,
   173  				"onlyAvailable": true,
   174  				"onlyLocal":     true,
   175  			}, extractActions), timeout)
   176  
   177  			It("$node.actions - withEndpoints", harness("$node.actions", "withEndpoints", map[string]interface{}{
   178  				"withEndpoints": true,
   179  				"skipInternal":  false,
   180  				"onlyAvailable": false,
   181  				"onlyLocal":     false,
   182  			}, extractActions), timeout)
   183  
   184  			It("$node.actions - skipInternal", harness("$node.actions", "skipInternal", map[string]interface{}{
   185  				"withEndpoints": false,
   186  				"skipInternal":  true,
   187  				"onlyAvailable": false,
   188  				"onlyLocal":     false,
   189  			}, extractActions), timeout)
   190  
   191  			It("$node.actions - onlyAvailable", harness("$node.actions", "onlyAvailable", map[string]interface{}{
   192  				"withEndpoints": false,
   193  				"skipInternal":  false,
   194  				"onlyAvailable": true,
   195  				"onlyLocal":     false,
   196  			}, extractActions), timeout)
   197  
   198  			It("$node.actions - onlyLocal", harness("$node.actions", "onlyLocal", map[string]interface{}{
   199  				"withEndpoints": false,
   200  				"skipInternal":  false,
   201  				"onlyAvailable": false,
   202  				"onlyLocal":     true,
   203  			}, extractActions), timeout)
   204  			It("$node.list with no services", harness("$node.list", "no-services", map[string]interface{}{
   205  				"withServices":  false,
   206  				"onlyAvailable": false,
   207  			}, extractNodes), timeout)
   208  
   209  			It("$node.list with services", harness("$node.list", "with-services", map[string]interface{}{
   210  				"withServices":  true,
   211  				"onlyAvailable": false,
   212  			}, extractNodes), timeout)
   213  
   214  			It("$node.services - all false", harness("$node.services", "all-false", map[string]interface{}{
   215  				"withEndpoints": false,
   216  				"withActions":   false,
   217  				"withEvents":    false,
   218  				"skipInternal":  false,
   219  				"onlyAvailable": false,
   220  				"onlyLocal":     false,
   221  			}, extractServices), timeout)
   222  
   223  			It("$node.services - all true", harness("$node.services", "all-true", map[string]interface{}{
   224  				"withEndpoints": true,
   225  				"withActions":   true,
   226  				"withEvents":    true,
   227  				"skipInternal":  true,
   228  				"onlyAvailable": true,
   229  				"onlyLocal":     true,
   230  			}, extractServices), timeout)
   231  
   232  			It("$node.services - withActions", harness("$node.services", "withActions", map[string]interface{}{
   233  				"withActions":   true,
   234  				"withEndpoints": false,
   235  				"withEvents":    false,
   236  				"skipInternal":  false,
   237  				"onlyAvailable": false,
   238  				"onlyLocal":     false,
   239  			}, extractServices), timeout)
   240  
   241  			It("$node.services - withEndpoints", harness("$node.services", "withEndpoints", map[string]interface{}{
   242  				"withActions":   false,
   243  				"withEndpoints": true,
   244  				"withEvents":    false,
   245  				"skipInternal":  false,
   246  				"onlyAvailable": false,
   247  				"onlyLocal":     false,
   248  			}, extractServices), 3)
   249  
   250  			It("$node.services - withEvents", harness("$node.services", "withEvents", map[string]interface{}{
   251  				"withActions":   false,
   252  				"withEndpoints": false,
   253  				"withEvents":    true,
   254  				"skipInternal":  false,
   255  				"onlyAvailable": false,
   256  				"onlyLocal":     false,
   257  			}, extractServices), timeout)
   258  
   259  			It("$node.services - skipInternal", harness("$node.services", "skipInternal", map[string]interface{}{
   260  				"withActions":   false,
   261  				"withEndpoints": false,
   262  				"withEvents":    false,
   263  				"skipInternal":  true,
   264  				"onlyAvailable": false,
   265  				"onlyLocal":     false,
   266  			}, extractServices), timeout)
   267  
   268  			It("$node.services - onlyAvailable", harness("$node.services", "onlyAvailable", map[string]interface{}{
   269  				"withActions":   false,
   270  				"withEndpoints": false,
   271  				"withEvents":    false,
   272  				"skipInternal":  false,
   273  				"onlyAvailable": true,
   274  				"onlyLocal":     false,
   275  			}, extractServices), timeout)
   276  
   277  			It("$node.services - onlyLocal", harness("$node.services", "onlyLocal", map[string]interface{}{
   278  				"withActions":   false,
   279  				"withEndpoints": false,
   280  				"withEvents":    false,
   281  				"skipInternal":  false,
   282  				"onlyAvailable": false,
   283  				"onlyLocal":     true,
   284  			}, extractServices), timeout)
   285  		})
   286  	})
   287  })
   288  
   289  func hasService(list []moleculer.Payload, name string) bool {
   290  	for _, p := range list {
   291  		if p.Get("name").String() == name {
   292  			return true
   293  		}
   294  	}
   295  	return false
   296  }