github.com/cloudwego/kitex@v0.9.0/pkg/event/bus_test.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package event
    18  
    19  import (
    20  	"sync"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/cloudwego/kitex/internal/test"
    25  )
    26  
    27  func TestWatch(t *testing.T) {
    28  	ebus := NewEventBus()
    29  
    30  	var triggered bool
    31  	var mu sync.RWMutex
    32  	ebus.Watch("trigger", func(event *Event) {
    33  		mu.Lock()
    34  		triggered = true
    35  		mu.Unlock()
    36  	})
    37  	mu.RLock()
    38  	test.Assert(t, !triggered)
    39  	mu.RUnlock()
    40  
    41  	ebus.Dispatch(&Event{Name: "not-trigger"})
    42  	time.Sleep(time.Millisecond)
    43  	mu.RLock()
    44  	test.Assert(t, !triggered)
    45  	mu.RUnlock()
    46  
    47  	ebus.Dispatch(&Event{Name: "trigger"})
    48  	time.Sleep(time.Millisecond)
    49  	mu.RLock()
    50  	test.Assert(t, triggered)
    51  	mu.RUnlock()
    52  }
    53  
    54  func TestUnWatch(t *testing.T) {
    55  	ebus := NewEventBus()
    56  
    57  	var triggered bool
    58  	var notTriggered bool
    59  	var mu sync.RWMutex
    60  	triggeredHandler := func(event *Event) {
    61  		mu.Lock()
    62  		triggered = true
    63  		mu.Unlock()
    64  	}
    65  	notTriggeredHandler := func(event *Event) {
    66  		mu.Lock()
    67  		notTriggered = true
    68  		mu.Unlock()
    69  	}
    70  
    71  	ebus.Watch("trigger", triggeredHandler)
    72  	ebus.Watch("trigger", notTriggeredHandler)
    73  	ebus.Unwatch("trigger", notTriggeredHandler)
    74  
    75  	ebus.Dispatch(&Event{Name: "trigger"})
    76  	time.Sleep(time.Millisecond)
    77  	mu.RLock()
    78  	test.Assert(t, triggered)
    79  	test.Assert(t, !notTriggered)
    80  	mu.RUnlock()
    81  }
    82  
    83  func TestDispatchAndWait(t *testing.T) {
    84  	ebus := NewEventBus()
    85  
    86  	var triggered bool
    87  	var mu sync.RWMutex
    88  	delayHandler := func(event *Event) {
    89  		time.Sleep(time.Second)
    90  		mu.Lock()
    91  		triggered = true
    92  		mu.Unlock()
    93  	}
    94  
    95  	ebus.Watch("trigger", delayHandler)
    96  
    97  	ebus.DispatchAndWait(&Event{Name: "trigger"})
    98  	mu.RLock()
    99  	test.Assert(t, triggered)
   100  	mu.RUnlock()
   101  }