github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/internal/testingutil/sleep.go (about)

     1  // Copyright 2019 The ChromiumOS Authors
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package testingutil
     6  
     7  import (
     8  	"context"
     9  	"time"
    10  
    11  	"go.chromium.org/tast/core/errors"
    12  )
    13  
    14  // Sleep implements testing.Sleep.
    15  func Sleep(ctx context.Context, d time.Duration) error {
    16  	tm := time.NewTimer(d)
    17  	defer tm.Stop()
    18  
    19  	select {
    20  	case <-tm.C:
    21  		return nil
    22  	case <-ctx.Done():
    23  		return errors.Wrap(ctx.Err(), "sleep interrupted")
    24  	}
    25  }