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

     1  package EventBus
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestNew(t *testing.T) {
     9  	bus := New()
    10  	if bus == nil {
    11  		t.Log("New EventBus not created!")
    12  		t.Fail()
    13  	}
    14  }
    15  
    16  func TestHasCallback(t *testing.T) {
    17  	bus := New()
    18  	bus.Subscribe("topic", func() {})
    19  	if bus.HasCallback("topic_topic") {
    20  		t.Fail()
    21  	}
    22  	if !bus.HasCallback("topic") {
    23  		t.Fail()
    24  	}
    25  }
    26  
    27  func TestSubscribe(t *testing.T) {
    28  	bus := New()
    29  	if bus.Subscribe("topic", func() {}) != nil {
    30  		t.Fail()
    31  	}
    32  	if bus.Subscribe("topic", "String") == nil {
    33  		t.Fail()
    34  	}
    35  }
    36  
    37  func TestSubscribeOnce(t *testing.T) {
    38  	bus := New()
    39  	if bus.SubscribeOnce("topic", func() {}) != nil {
    40  		t.Fail()
    41  	}
    42  	if bus.SubscribeOnce("topic", "String") == nil {
    43  		t.Fail()
    44  	}
    45  }
    46  
    47  func TestSubscribeOnceAndManySubscribe(t *testing.T) {
    48  	bus := New()
    49  	event := "topic"
    50  	flag := 0
    51  	fn := func() { flag += 1 }
    52  	bus.SubscribeOnce(event, fn)
    53  	bus.Subscribe(event, fn)
    54  	bus.Subscribe(event, fn)
    55  	bus.Publish(event)
    56  
    57  	if flag != 3 {
    58  		t.Fail()
    59  	}
    60  }
    61  
    62  func TestUnsubscribe(t *testing.T) {
    63  	bus := New()
    64  	handler := func() {}
    65  	bus.Subscribe("topic", handler)
    66  	if bus.Unsubscribe("topic", handler) != nil {
    67  		t.Fail()
    68  	}
    69  	if bus.Unsubscribe("topic", handler) == nil {
    70  		t.Fail()
    71  	}
    72  }
    73  
    74  type handler struct {
    75  	val int
    76  }
    77  
    78  func (h *handler) Handle() {
    79  	h.val++
    80  }
    81  
    82  func TestUnsubscribeMethod(t *testing.T) {
    83  	bus := New()
    84  	h := &handler{val: 0}
    85  
    86  	bus.Subscribe("topic", h.Handle)
    87  	bus.Publish("topic")
    88  	if bus.Unsubscribe("topic", h.Handle) != nil {
    89  		t.Fail()
    90  	}
    91  	if bus.Unsubscribe("topic", h.Handle) == nil {
    92  		t.Fail()
    93  	}
    94  	bus.Publish("topic")
    95  	bus.WaitAsync()
    96  
    97  	if h.val != 1 {
    98  		t.Fail()
    99  	}
   100  }
   101  
   102  func TestPublish(t *testing.T) {
   103  	bus := New()
   104  	bus.Subscribe("topic", func(a int, err error) {
   105  		if a != 10 {
   106  			t.Fail()
   107  		}
   108  
   109  		if err != nil {
   110  			t.Fail()
   111  		}
   112  	})
   113  	bus.Publish("topic", 10, nil)
   114  }
   115  
   116  func TestSubcribeOnceAsync(t *testing.T) {
   117  	results := make([]int, 0)
   118  
   119  	bus := New()
   120  	bus.SubscribeOnceAsync("topic", func(a int, out *[]int) {
   121  		*out = append(*out, a)
   122  	})
   123  
   124  	bus.Publish("topic", 10, &results)
   125  	bus.Publish("topic", 10, &results)
   126  
   127  	bus.WaitAsync()
   128  
   129  	if len(results) != 1 {
   130  		t.Fail()
   131  	}
   132  
   133  	if bus.HasCallback("topic") {
   134  		t.Fail()
   135  	}
   136  }
   137  
   138  func TestSubscribeAsyncTransactional(t *testing.T) {
   139  	results := make([]int, 0)
   140  
   141  	bus := New()
   142  	bus.SubscribeAsync("topic", func(a int, out *[]int, dur string) {
   143  		sleep, _ := time.ParseDuration(dur)
   144  		time.Sleep(sleep)
   145  		*out = append(*out, a)
   146  	}, true)
   147  
   148  	bus.Publish("topic", 1, &results, "1s")
   149  	bus.Publish("topic", 2, &results, "0s")
   150  
   151  	bus.WaitAsync()
   152  
   153  	if len(results) != 2 {
   154  		t.Fail()
   155  	}
   156  
   157  	if results[0] != 1 || results[1] != 2 {
   158  		t.Fail()
   159  	}
   160  }
   161  
   162  func TestSubscribeAsync(t *testing.T) {
   163  	results := make(chan int)
   164  
   165  	bus := New()
   166  	bus.SubscribeAsync("topic", func(a int, out chan<- int) {
   167  		out <- a
   168  	}, false)
   169  
   170  	bus.Publish("topic", 1, results)
   171  	bus.Publish("topic", 2, results)
   172  
   173  	numResults := 0
   174  
   175  	go func() {
   176  		for _ = range results {
   177  			numResults++
   178  		}
   179  	}()
   180  
   181  	bus.WaitAsync()
   182  	println(2)
   183  
   184  	time.Sleep(10 * time.Millisecond)
   185  
   186  	// todo race detected during execution of test
   187  	//if numResults != 2 {
   188  	//	t.Fail()
   189  	//}
   190  }