File: context_test.go

package info (click to toggle)
golang-github-juju-utils 0.0~git20171220.f38c0b0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,748 kB
  • sloc: makefile: 20
file content (96 lines) | stat: -rw-r--r-- 3,640 bytes parent folder | download | duplicates (2)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package utils_test

import (
	"fmt"
	"time"

	"golang.org/x/net/context"

	"github.com/juju/testing"
	jc "github.com/juju/testing/checkers"
	gc "gopkg.in/check.v1"

	"github.com/juju/utils"
)

type contextSuite struct{}

var _ = gc.Suite(&contextSuite{})

// Note: the logic in these tests was copied from the tests
// in the Go standard library.

func (*contextSuite) TestDeadline(c *gc.C) {
	clk := testing.NewClock(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC))
	ctx, cancel := utils.ContextWithDeadline(context.Background(), clk, clk.Now().Add(50*time.Millisecond))
	defer cancel()
	c.Assert(fmt.Sprint(ctx), gc.Equals, `context.Background.WithDeadline(2000-01-01 00:00:00.05 +0000 UTC [50ms])`)
	testContextDeadline(c, ctx, "WithDeadline", clk, 1, 50*time.Millisecond)

	ctx, cancel = utils.ContextWithDeadline(context.Background(), clk, clk.Now().Add(50*time.Millisecond))
	defer cancel()
	o := otherContext{ctx}
	testContextDeadline(c, o, "WithDeadline+otherContext", clk, 1, 50*time.Millisecond)

	ctx, cancel = utils.ContextWithDeadline(context.Background(), clk, clk.Now().Add(50*time.Millisecond))
	defer cancel()
	o = otherContext{ctx}
	ctx, _ = utils.ContextWithDeadline(o, clk, clk.Now().Add(4*time.Second))
	testContextDeadline(c, ctx, "WithDeadline+otherContext+WithDeadline", clk, 2, 50*time.Millisecond)

	ctx, cancel = utils.ContextWithDeadline(context.Background(), clk, clk.Now().Add(-time.Millisecond))
	defer cancel()
	testContextDeadline(c, ctx, "WithDeadline+inthepast", clk, 0, 0)

	ctx, cancel = utils.ContextWithDeadline(context.Background(), clk, clk.Now())
	testContextDeadline(c, ctx, "WithDeadline+now", clk, 0, 0)
}

func (*contextSuite) TestTimeout(c *gc.C) {
	clk := testing.NewClock(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC))
	ctx, _ := utils.ContextWithTimeout(context.Background(), clk, 50*time.Millisecond)
	c.Assert(fmt.Sprint(ctx), gc.Equals, `context.Background.WithDeadline(2000-01-01 00:00:00.05 +0000 UTC [50ms])`)
	testContextDeadline(c, ctx, "WithTimeout", clk, 1, 50*time.Millisecond)

	ctx, _ = utils.ContextWithTimeout(context.Background(), clk, 50*time.Millisecond)
	o := otherContext{ctx}
	testContextDeadline(c, o, "WithTimeout+otherContext", clk, 1, 50*time.Millisecond)

	ctx, _ = utils.ContextWithTimeout(context.Background(), clk, 50*time.Millisecond)
	o = otherContext{ctx}
	ctx, _ = utils.ContextWithTimeout(o, clk, 3*time.Second)
	testContextDeadline(c, ctx, "WithTimeout+otherContext+WithTimeout", clk, 2, 50*time.Millisecond)
}

func (*contextSuite) TestCanceledTimeout(c *gc.C) {
	clk := testing.NewClock(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC))
	ctx, _ := utils.ContextWithTimeout(context.Background(), clk, time.Second)
	o := otherContext{ctx}
	ctx, cancel := utils.ContextWithTimeout(o, clk, 2*time.Second)
	cancel()
	time.Sleep(100 * time.Millisecond) // let cancelation propagate
	select {
	case <-ctx.Done():
	default:
		c.Errorf("<-ctx.Done() blocked, but shouldn't have")
	}
	c.Assert(ctx.Err(), gc.Equals, context.Canceled)
}

func testContextDeadline(c *gc.C, ctx context.Context, name string, clk *testing.Clock, waiters int, failAfter time.Duration) {
	err := clk.WaitAdvance(failAfter, 0, waiters)
	c.Assert(err, jc.ErrorIsNil)
	select {
	case <-time.After(time.Second):
		c.Fatalf("%s: context should have timed out", name)
	case <-ctx.Done():
	}
	c.Assert(ctx.Err(), gc.Equals, context.DeadlineExceeded)
}

// otherContext is a Context that's not one of the types defined in context.go.
// This lets us test code paths that differ based on the underlying type of the
// Context.
type otherContext struct {
	context.Context
}