github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/testutils/soon_test.go (about) 1 // Copyright 2016 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package testutils 12 13 import ( 14 "testing" 15 "time" 16 17 "github.com/cockroachdb/cockroach/pkg/util/timeutil" 18 "github.com/cockroachdb/errors" 19 ) 20 21 func TestSucceedsSoon(t *testing.T) { 22 // Try a method which always succeeds. 23 SucceedsSoon(t, func() error { return nil }) 24 25 // Try a method which succeeds after a known duration. 26 start := timeutil.Now() 27 duration := time.Millisecond * 10 28 SucceedsSoon(t, func() error { 29 elapsed := timeutil.Since(start) 30 if elapsed > duration { 31 return nil 32 } 33 return errors.Errorf("%s elapsed, waiting until %s elapses", elapsed, duration) 34 }) 35 }