go.etcd.io/etcd@v3.3.27+incompatible/integration/v3_barrier_test.go (about) 1 // Copyright 2016 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 integration 16 17 import ( 18 "testing" 19 "time" 20 21 "github.com/coreos/etcd/clientv3" 22 "github.com/coreos/etcd/contrib/recipes" 23 "github.com/coreos/etcd/pkg/testutil" 24 ) 25 26 func TestBarrierSingleNode(t *testing.T) { 27 defer testutil.AfterTest(t) 28 clus := NewClusterV3(t, &ClusterConfig{Size: 1}) 29 defer clus.Terminate(t) 30 testBarrier(t, 5, func() *clientv3.Client { return clus.clients[0] }) 31 } 32 33 func TestBarrierMultiNode(t *testing.T) { 34 defer testutil.AfterTest(t) 35 clus := NewClusterV3(t, &ClusterConfig{Size: 3}) 36 defer clus.Terminate(t) 37 testBarrier(t, 5, func() *clientv3.Client { return clus.RandClient() }) 38 } 39 40 func testBarrier(t *testing.T, waiters int, chooseClient func() *clientv3.Client) { 41 b := recipe.NewBarrier(chooseClient(), "test-barrier") 42 if err := b.Hold(); err != nil { 43 t.Fatalf("could not hold barrier (%v)", err) 44 } 45 if err := b.Hold(); err == nil { 46 t.Fatalf("able to double-hold barrier") 47 } 48 49 donec := make(chan struct{}) 50 for i := 0; i < waiters; i++ { 51 go func() { 52 br := recipe.NewBarrier(chooseClient(), "test-barrier") 53 if err := br.Wait(); err != nil { 54 t.Fatalf("could not wait on barrier (%v)", err) 55 } 56 donec <- struct{}{} 57 }() 58 } 59 60 select { 61 case <-donec: 62 t.Fatalf("barrier did not wait") 63 default: 64 } 65 66 if err := b.Release(); err != nil { 67 t.Fatalf("could not release barrier (%v)", err) 68 } 69 70 timerC := time.After(time.Duration(waiters*100) * time.Millisecond) 71 for i := 0; i < waiters; i++ { 72 select { 73 case <-timerC: 74 t.Fatalf("barrier timed out") 75 case <-donec: 76 } 77 } 78 }