...

Source file src/go.etcd.io/etcd/pkg/v3/wait/wait_test.go

Documentation: go.etcd.io/etcd/pkg/v3/wait

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package wait
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  	"time"
    21  )
    22  
    23  func TestWait(t *testing.T) {
    24  	const eid = 1
    25  	wt := New()
    26  	ch := wt.Register(eid)
    27  	wt.Trigger(eid, "foo")
    28  	v := <-ch
    29  	if g, w := fmt.Sprintf("%v (%T)", v, v), "foo (string)"; g != w {
    30  		t.Errorf("<-ch = %v, want %v", g, w)
    31  	}
    32  
    33  	if g := <-ch; g != nil {
    34  		t.Errorf("unexpected non-nil value: %v (%T)", g, g)
    35  	}
    36  }
    37  
    38  func TestRegisterDupPanic(t *testing.T) {
    39  	const eid = 1
    40  	wt := New()
    41  	ch1 := wt.Register(eid)
    42  
    43  	panicC := make(chan struct{}, 1)
    44  
    45  	func() {
    46  		defer func() {
    47  			if r := recover(); r != nil {
    48  				panicC <- struct{}{}
    49  			}
    50  		}()
    51  		wt.Register(eid)
    52  	}()
    53  
    54  	select {
    55  	case <-panicC:
    56  	case <-time.After(1 * time.Second):
    57  		t.Errorf("failed to receive panic")
    58  	}
    59  
    60  	wt.Trigger(eid, "foo")
    61  	<-ch1
    62  }
    63  
    64  func TestTriggerDupSuppression(t *testing.T) {
    65  	const eid = 1
    66  	wt := New()
    67  	ch := wt.Register(eid)
    68  	wt.Trigger(eid, "foo")
    69  	wt.Trigger(eid, "bar")
    70  
    71  	v := <-ch
    72  	if g, w := fmt.Sprintf("%v (%T)", v, v), "foo (string)"; g != w {
    73  		t.Errorf("<-ch = %v, want %v", g, w)
    74  	}
    75  
    76  	if g := <-ch; g != nil {
    77  		t.Errorf("unexpected non-nil value: %v (%T)", g, g)
    78  	}
    79  }
    80  
    81  func TestIsRegistered(t *testing.T) {
    82  	wt := New()
    83  
    84  	wt.Register(0)
    85  	wt.Register(1)
    86  	wt.Register(2)
    87  
    88  	for i := uint64(0); i < 3; i++ {
    89  		if !wt.IsRegistered(i) {
    90  			t.Errorf("event ID %d isn't registered", i)
    91  		}
    92  	}
    93  
    94  	if wt.IsRegistered(4) {
    95  		t.Errorf("event ID 4 shouldn't be registered")
    96  	}
    97  
    98  	wt.Trigger(0, "foo")
    99  	if wt.IsRegistered(0) {
   100  		t.Errorf("event ID 0 is already triggered, shouldn't be registered")
   101  	}
   102  }
   103  

View as plain text