github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/internal/grpcsync/event_test.go (about)

     1  /*
     2   *
     3   * Copyright 2018 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package grpcsync
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/hxx258456/ccgo/grpc/internal/grpctest"
    25  )
    26  
    27  type s struct {
    28  	grpctest.Tester
    29  }
    30  
    31  func Test(t *testing.T) {
    32  	grpctest.RunSubTests(t, s{})
    33  }
    34  
    35  func (s) TestEventHasFired(t *testing.T) {
    36  	e := NewEvent()
    37  	if e.HasFired() {
    38  		t.Fatal("e.HasFired() = true; want false")
    39  	}
    40  	if !e.Fire() {
    41  		t.Fatal("e.Fire() = false; want true")
    42  	}
    43  	if !e.HasFired() {
    44  		t.Fatal("e.HasFired() = false; want true")
    45  	}
    46  }
    47  
    48  func (s) TestEventDoneChannel(t *testing.T) {
    49  	e := NewEvent()
    50  	select {
    51  	case <-e.Done():
    52  		t.Fatal("e.HasFired() = true; want false")
    53  	default:
    54  	}
    55  	if !e.Fire() {
    56  		t.Fatal("e.Fire() = false; want true")
    57  	}
    58  	select {
    59  	case <-e.Done():
    60  	default:
    61  		t.Fatal("e.HasFired() = false; want true")
    62  	}
    63  }
    64  
    65  func (s) TestEventMultipleFires(t *testing.T) {
    66  	e := NewEvent()
    67  	if e.HasFired() {
    68  		t.Fatal("e.HasFired() = true; want false")
    69  	}
    70  	if !e.Fire() {
    71  		t.Fatal("e.Fire() = false; want true")
    72  	}
    73  	for i := 0; i < 3; i++ {
    74  		if !e.HasFired() {
    75  			t.Fatal("e.HasFired() = false; want true")
    76  		}
    77  		if e.Fire() {
    78  			t.Fatal("e.Fire() = true; want false")
    79  		}
    80  	}
    81  }