github.com/searKing/golang/go@v1.2.117/sync/event_test.go (about)

     1  // Copyright 2021 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package sync
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func TestEventHasFired(t *testing.T) {
    12  	e := NewEvent()
    13  	if e.HasFired() {
    14  		t.Fatal("e.HasFired() = true; want false")
    15  	}
    16  	if !e.Fire() {
    17  		t.Fatal("e.Fire() = false; want true")
    18  	}
    19  	if !e.HasFired() {
    20  		t.Fatal("e.HasFired() = false; want true")
    21  	}
    22  }
    23  
    24  func TestEventDoneChannel(t *testing.T) {
    25  	e := NewEvent()
    26  	select {
    27  	case <-e.Done():
    28  		t.Fatal("e.HasFired() = true; want false")
    29  	default:
    30  	}
    31  	if !e.Fire() {
    32  		t.Fatal("e.Fire() = false; want true")
    33  	}
    34  	select {
    35  	case <-e.Done():
    36  	default:
    37  		t.Fatal("e.HasFired() = false; want true")
    38  	}
    39  }
    40  
    41  func TestEventMultipleFires(t *testing.T) {
    42  	e := NewEvent()
    43  	if e.HasFired() {
    44  		t.Fatal("e.HasFired() = true; want false")
    45  	}
    46  	if !e.Fire() {
    47  		t.Fatal("e.Fire() = false; want true")
    48  	}
    49  	for i := 0; i < 3; i++ {
    50  		if !e.HasFired() {
    51  			t.Fatal("e.HasFired() = false; want true")
    52  		}
    53  		if e.Fire() {
    54  			t.Fatal("e.Fire() = true; want false")
    55  		}
    56  	}
    57  }