File: timeout_test.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 (56 lines) | stat: -rw-r--r-- 1,296 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
package testutil

import (
	"fmt"
	"testing"
	"time"
)

func TestDoOrTimeoutNeverOK(t *testing.T) {
	SkipIfShort(t)

	// Never return OK so timeout at 10ms.
	ok, err := DoOrTimeout(func() (bool, error) {
		return false, nil
	}, 10*time.Millisecond, time.Millisecond)
	if ok || err != nil {
		t.Errorf("Expected timeout (false, nil), got %v, %v", ok, err)
	}
}

func TestDoOrTimeoutAlwaysOK(t *testing.T) {
	// Always return OK.
	ok, err := DoOrTimeout(func() (bool, error) {
		return true, nil
	}, 10*time.Millisecond, time.Millisecond)
	if !ok || err != nil {
		t.Errorf("Expected OK, got %v, %v", ok, err)
	}
}

func TestDoOrTimeoutStallThenOK(t *testing.T) {
	SkipIfShort(t)

	// Stall for 5 ticks (50ms) and then return OK; timeout at 1s.
	i := 5
	ok, err := DoOrTimeout(func() (bool, error) {
		i--
		if i > 0 {
			return false, nil
		}
		return true, nil
	}, time.Second, 10*time.Millisecond)
	if !ok || err != nil {
		t.Errorf("Expected OK, got %v, %v", ok, err)
	}
}

func TestDoOrTimeoutAlwaysErr(t *testing.T) {
	// Return an error, should return false,err
	ok, err := DoOrTimeout(func() (bool, error) {
		return false, fmt.Errorf("oh no") // nolint:goerr113
	}, 1*time.Second, time.Millisecond)
	if ok || err == nil {
		t.Errorf("Expected error (false,!nil), got %v %v", ok, err)
	}
}