File: timeout.go

package info (click to toggle)
mtail 3.2.24-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,384 kB
  • sloc: yacc: 647; makefile: 226; sh: 78; lisp: 77; awk: 17
file content (67 lines) | stat: -rw-r--r-- 1,789 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2019 Google Inc. All Rights Reserved.
// This file is available under the Apache license.
package testutil

import (
	"os"
	"runtime"
	"testing"
	"time"

	"github.com/golang/glog"
)

// DoOrTimeout runs a check function every interval until deadline, unless the
// check returns true.  The check should return false otherwise. If the check
// returns an error the check is immediately failed.
func DoOrTimeout(do func() (bool, error), deadline, interval time.Duration) (bool, error) {
	timeout := time.After(deadline)
	ticker := time.NewTicker(interval)
	defer ticker.Stop()
	for {
		select {
		case <-timeout:
			return false, nil
		case <-ticker.C:
			glog.V(2).Infof("tick")
			ok, err := do()
			glog.V(2).Infof("ok, err: %v %v", ok, err)
			if err != nil {
				return false, err
			}
			if ok {
				return true, nil
			}
			// otherwise wait and retry
		}
	}
}

// TimeoutTest returns a test function that executes f with a timeout, If the
// test does not complete in time the test is failed.  This lets us set a
// per-test timeout instead of the global `go test -timeout` coarse timeout.
func TimeoutTest(timeout time.Duration, f func(t *testing.T)) func(t *testing.T) {
	// Raise the timeout if we're run under the race detector.
	timeout *= RaceDetectorMultiplier
	// If we're in a CI environment, raise the timeout by 10x.  This mimics the
	// timeout global flag set in the Makefile.
	if os.Getenv("CI") == "true" {
		timeout *= 10
	}
	return func(t *testing.T) {
		t.Helper()
		done := make(chan bool)
		go func() {
			t.Helper()
			defer close(done)
			f(t)
		}()
		select {
		case <-time.After(timeout):
			buf := make([]byte, 1<<20)
			stacklen := runtime.Stack(buf, true)
			t.Fatalf("timed out after %s\n%s", timeout, buf[:stacklen])
		case <-done:
		}
	}
}