github.com/fafucoder/cilium@v1.6.11/pkg/contexthelpers/context_test.go (about) 1 // Copyright 2020 Authors of Cilium 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 // +build !privileged_tests 16 17 package contexthelpers 18 19 import ( 20 "context" 21 "testing" 22 "time" 23 24 "gopkg.in/check.v1" 25 ) 26 27 func Test(t *testing.T) { 28 check.TestingT(t) 29 } 30 31 type ContextSuite struct{} 32 33 var _ = check.Suite(&ContextSuite{}) 34 35 func (b *ContextSuite) TestConditionalTimeoutContext(c *check.C) { 36 ctx, cancel, ch := NewConditionalTimeoutContext(context.Background(), 10*time.Millisecond) 37 c.Assert(ctx, check.Not(check.IsNil)) 38 c.Assert(cancel, check.Not(check.IsNil)) 39 c.Assert(ch, check.Not(check.IsNil)) 40 41 // validate that the context is being cancelled due to the 10 42 // millisecond timeout specified 43 select { 44 case <-ctx.Done(): 45 case <-time.After(time.Second): 46 c.Errorf("conditional timeout was not triggered") 47 } 48 49 ctx, cancel, ch = NewConditionalTimeoutContext(context.Background(), 10*time.Millisecond) 50 // report success via the channel 51 ch <- true 52 close(ch) 53 54 // validate that the context is not being cancelled as success has been 55 // reported 56 select { 57 case <-ctx.Done(): 58 c.Errorf("context cancelled despite reporting success") 59 case <-time.After(100 * time.Millisecond): 60 } 61 62 cancel() 63 }