"runtime"
"strings"
"sync"
- "testing"
"time"
)
+type testingT interface {
+ Error(args ...interface{})
+ Errorf(format string, args ...interface{})
+ Fail()
+ FailNow()
+ Failed() bool
+ Fatal(args ...interface{})
+ Fatalf(format string, args ...interface{})
+ Log(args ...interface{})
+ Logf(format string, args ...interface{})
+ Name() string
+ Skip(args ...interface{})
+ SkipNow()
+ Skipf(format string, args ...interface{})
+ Skipped() bool
+}
+
// 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.
Context
}
-func TestBackground(t *testing.T) {
+func XTestBackground(t testingT) {
c := Background()
if c == nil {
t.Fatalf("Background returned nil")
}
}
-func TestTODO(t *testing.T) {
+func XTestTODO(t testingT) {
c := TODO()
if c == nil {
t.Fatalf("TODO returned nil")
}
}
-func TestWithCancel(t *testing.T) {
+func XTestWithCancel(t testingT) {
c1, cancel := WithCancel(Background())
if got, want := fmt.Sprint(c1), "context.Background.WithCancel"; got != want {
return ret
}
-func TestParentFinishesChild(t *testing.T) {
+func XTestParentFinishesChild(t testingT) {
// Context tree:
// parent -> cancelChild
// parent -> valueChild -> timerChild
}
}
-func TestChildFinishesFirst(t *testing.T) {
+func XTestChildFinishesFirst(t testingT) {
cancelable, stop := WithCancel(Background())
defer stop()
for _, parent := range []Context{Background(), cancelable} {
}
}
-func testDeadline(c Context, name string, failAfter time.Duration, t *testing.T) {
+func testDeadline(c Context, name string, failAfter time.Duration, t testingT) {
select {
case <-time.After(failAfter):
t.Fatalf("%s: context should have timed out", name)
}
}
-func TestDeadline(t *testing.T) {
+func XTestDeadline(t testingT) {
c, _ := WithDeadline(Background(), time.Now().Add(50*time.Millisecond))
if got, prefix := fmt.Sprint(c), "context.Background.WithDeadline("; !strings.HasPrefix(got, prefix) {
t.Errorf("c.String() = %q want prefix %q", got, prefix)
testDeadline(c, "WithDeadline+now", time.Second, t)
}
-func TestTimeout(t *testing.T) {
+func XTestTimeout(t testingT) {
c, _ := WithTimeout(Background(), 50*time.Millisecond)
if got, prefix := fmt.Sprint(c), "context.Background.WithDeadline("; !strings.HasPrefix(got, prefix) {
t.Errorf("c.String() = %q want prefix %q", got, prefix)
testDeadline(c, "WithTimeout+otherContext+WithTimeout", 2*time.Second, t)
}
-func TestCanceledTimeout(t *testing.T) {
+func XTestCanceledTimeout(t testingT) {
c, _ := WithTimeout(Background(), time.Second)
o := otherContext{c}
c, cancel := WithTimeout(o, 2*time.Second)
var k2 = key2(1) // same int as k1, different type
var k3 = key2(3) // same type as k2, different int
-func TestValues(t *testing.T) {
+func XTestValues(t testingT) {
check := func(c Context, nm, v1, v2, v3 string) {
if v, ok := c.Value(k1).(string); ok == (len(v1) == 0) || v != v1 {
t.Errorf(`%s.Value(k1).(string) = %q, %t want %q, %t`, nm, v, ok, v1, len(v1) != 0)
check(o4, "o4", "", "c2k2", "")
}
-func TestAllocs(t *testing.T) {
+func XTestAllocs(t testingT, testingShort func() bool, testingAllocsPerRun func(int, func()) float64) {
bg := Background()
for _, test := range []struct {
desc string
limit = test.gccgoLimit
}
numRuns := 100
- if testing.Short() {
+ if testingShort() {
numRuns = 10
}
- if n := testing.AllocsPerRun(numRuns, test.f); n > limit {
+ if n := testingAllocsPerRun(numRuns, test.f); n > limit {
t.Errorf("%s allocs = %f want %d", test.desc, n, int(limit))
}
}
}
-func TestSimultaneousCancels(t *testing.T) {
+func XTestSimultaneousCancels(t testingT) {
root, cancel := WithCancel(Background())
m := map[Context]CancelFunc{root: cancel}
q := []Context{root}
}
}
-func TestInterlockedCancels(t *testing.T) {
+func XTestInterlockedCancels(t testingT) {
parent, cancelParent := WithCancel(Background())
child, cancelChild := WithCancel(parent)
go func() {
}
}
-func TestLayersCancel(t *testing.T) {
+func XTestLayersCancel(t testingT) {
testLayers(t, time.Now().UnixNano(), false)
}
-func TestLayersTimeout(t *testing.T) {
+func XTestLayersTimeout(t testingT) {
testLayers(t, time.Now().UnixNano(), true)
}
-func testLayers(t *testing.T, seed int64, testTimeout bool) {
+func testLayers(t testingT, seed int64, testTimeout bool) {
rand.Seed(seed)
errorf := func(format string, a ...interface{}) {
t.Errorf(fmt.Sprintf("seed=%d: %s", seed, format), a...)
}
}
-func TestCancelRemoves(t *testing.T) {
+func XTestCancelRemoves(t testingT) {
checkChildren := func(when string, ctx Context, want int) {
if got := len(ctx.(*cancelCtx).children); got != want {
t.Errorf("%s: context has %d children, want %d", when, got, want)
checkChildren("after cancelling WithTimeout child", ctx, 0)
}
-func TestWithCancelCanceledParent(t *testing.T) {
+func XTestWithCancelCanceledParent(t testingT) {
parent, pcancel := WithCancel(Background())
pcancel()
}
}
-func TestWithValueChecksKey(t *testing.T) {
+func XTestWithValueChecksKey(t testingT) {
panicVal := recoveredValue(func() { WithValue(Background(), []byte("foo"), "bar") })
if panicVal == nil {
t.Error("expected panic")
return
}
-func TestDeadlineExceededSupportsTimeout(t *testing.T) {
+func XTestDeadlineExceededSupportsTimeout(t testingT) {
i, ok := DeadlineExceeded.(interface {
Timeout() bool
})
t.Fatal("wrong value for timeout")
}
}
-
-func BenchmarkContextCancelTree(b *testing.B) {
- depths := []int{1, 10, 100, 1000}
- for _, d := range depths {
- b.Run(fmt.Sprintf("depth=%d", d), func(b *testing.B) {
- b.Run("Root=Background", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- buildContextTree(Background(), d)
- }
- })
- b.Run("Root=OpenCanceler", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- ctx, cancel := WithCancel(Background())
- buildContextTree(ctx, d)
- cancel()
- }
- })
- b.Run("Root=ClosedCanceler", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- ctx, cancel := WithCancel(Background())
- cancel()
- buildContextTree(ctx, d)
- }
- })
- })
- }
-}
-
-func buildContextTree(root Context, depth int) {
- for d := 0; d < depth; d++ {
- root, _ = WithCancel(root)
- }
-}