github.com/chain5j/chain5j-pkg@v1.0.7/eventbus/network_bus_test.go (about)

     1  package EventBus
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestNewServer(t *testing.T) {
     8  	serverBus := NewServer(":2010", "/_server_bus_", New())
     9  	serverBus.Start()
    10  	if serverBus == nil || !serverBus.service.started {
    11  		t.Log("New server EventBus not created!")
    12  		t.Fail()
    13  	}
    14  	serverBus.Stop()
    15  }
    16  
    17  func TestNewClient(t *testing.T) {
    18  	clientBus := NewClient(":2015", "/_client_bus_", New())
    19  	clientBus.Start()
    20  	if clientBus == nil || !clientBus.service.started {
    21  		t.Log("New client EventBus not created!")
    22  		t.Fail()
    23  	}
    24  	clientBus.Stop()
    25  }
    26  
    27  func TestRegister(t *testing.T) {
    28  	serverPath := "/_server_bus_"
    29  	serverBus := NewServer(":2010", serverPath, New())
    30  
    31  	args := &SubscribeArg{serverBus.address, serverPath, PublishService, Subscribe, "topic"}
    32  	reply := new(bool)
    33  
    34  	serverBus.service.Register(args, reply)
    35  
    36  	if serverBus.eventBus.HasCallback("topic_topic") {
    37  		t.Fail()
    38  	}
    39  	if !serverBus.eventBus.HasCallback("topic") {
    40  		t.Fail()
    41  	}
    42  }
    43  
    44  func TestPushEvent(t *testing.T) {
    45  	clientBus := NewClient("localhost:2015", "/_client_bus_", New())
    46  
    47  	eventArgs := make([]interface{}, 1)
    48  	eventArgs[0] = 10
    49  
    50  	clientArg := &ClientArg{eventArgs, "topic"}
    51  	reply := new(bool)
    52  
    53  	fn := func(a int) {
    54  		if a != 10 {
    55  			t.Fail()
    56  		}
    57  	}
    58  
    59  	clientBus.eventBus.Subscribe("topic", fn)
    60  	clientBus.service.PushEvent(clientArg, reply)
    61  	if !(*reply) {
    62  		t.Fail()
    63  	}
    64  }
    65  
    66  func TestServerPublish(t *testing.T) {
    67  	serverBus := NewServer(":2020", "/_server_bus_b", New())
    68  	serverBus.Start()
    69  
    70  	fn := func(a int) {
    71  		if a != 10 {
    72  			t.Fail()
    73  		}
    74  	}
    75  
    76  	clientBus := NewClient(":2025", "/_client_bus_b", New())
    77  	clientBus.Start()
    78  
    79  	clientBus.Subscribe("topic", fn, ":2010", "/_server_bus_b")
    80  
    81  	serverBus.EventBus().Publish("topic", 10)
    82  
    83  	clientBus.Stop()
    84  	serverBus.Stop()
    85  }
    86  
    87  func TestNetworkBus(t *testing.T) {
    88  	networkBusA := NewNetworkBus(":2035", "/_net_bus_A")
    89  	networkBusA.Start()
    90  
    91  	networkBusB := NewNetworkBus(":2030", "/_net_bus_B")
    92  	networkBusB.Start()
    93  
    94  	fnA := func(a int) {
    95  		if a != 10 {
    96  			t.Fail()
    97  		}
    98  	}
    99  	networkBusA.Subscribe("topic-A", fnA, ":2030", "/_net_bus_B")
   100  	networkBusB.EventBus().Publish("topic-A", 10)
   101  
   102  	fnB := func(a int) {
   103  		if a != 20 {
   104  			t.Fail()
   105  		}
   106  	}
   107  	networkBusB.Subscribe("topic-B", fnB, ":2035", "/_net_bus_A")
   108  	networkBusA.EventBus().Publish("topic-B", 20)
   109  
   110  	networkBusA.Stop()
   111  	networkBusB.Stop()
   112  }