go.etcd.io/etcd@v3.3.27+incompatible/pkg/wait/wait_time_test.go (about) 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 "testing" 19 "time" 20 ) 21 22 func TestWaitTime(t *testing.T) { 23 wt := NewTimeList() 24 ch1 := wt.Wait(1) 25 wt.Trigger(2) 26 select { 27 case <-ch1: 28 default: 29 t.Fatalf("cannot receive from ch as expected") 30 } 31 32 ch2 := wt.Wait(4) 33 wt.Trigger(3) 34 select { 35 case <-ch2: 36 t.Fatalf("unexpected to receive from ch2") 37 default: 38 } 39 wt.Trigger(4) 40 select { 41 case <-ch2: 42 default: 43 t.Fatalf("cannot receive from ch2 as expected") 44 } 45 46 select { 47 // wait on a triggered deadline 48 case <-wt.Wait(4): 49 default: 50 t.Fatalf("unexpected blocking when wait on triggered deadline") 51 } 52 } 53 54 func TestWaitTestStress(t *testing.T) { 55 chs := make([]<-chan struct{}, 0) 56 wt := NewTimeList() 57 for i := 0; i < 10000; i++ { 58 chs = append(chs, wt.Wait(uint64(i))) 59 } 60 wt.Trigger(10000 + 1) 61 62 for _, ch := range chs { 63 select { 64 case <-ch: 65 case <-time.After(time.Second): 66 t.Fatalf("cannot receive from ch as expected") 67 } 68 } 69 } 70 71 func BenchmarkWaitTime(b *testing.B) { 72 wt := NewTimeList() 73 for i := 0; i < b.N; i++ { 74 wt.Wait(1) 75 } 76 } 77 78 func BenchmarkTriggerAnd10KWaitTime(b *testing.B) { 79 for i := 0; i < b.N; i++ { 80 wt := NewTimeList() 81 for j := 0; j < 10000; j++ { 82 wt.Wait(uint64(j)) 83 } 84 wt.Trigger(10000 + 1) 85 } 86 }