context_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package context
  5. import (
  6. "fmt"
  7. "math/rand"
  8. "runtime"
  9. "sync"
  10. "testing"
  11. "time"
  12. )
  13. // otherContext is a Context that's not a *ctx. This lets us test code paths
  14. // that differ based on the underlying type of the Context.
  15. type otherContext struct {
  16. Context
  17. }
  18. func TestBackground(t *testing.T) {
  19. c := Background()
  20. if c == nil {
  21. t.Fatalf("Background returned nil")
  22. }
  23. select {
  24. case x := <-c.Done():
  25. t.Errorf("<-c.Done() == %v want nothing (it should block)", x)
  26. default:
  27. }
  28. if s := fmt.Sprint(c); s != "context.Background" {
  29. t.Errorf(`Background.String = %q want "context.Background"`, s)
  30. }
  31. }
  32. func TestTODO(t *testing.T) {
  33. c := TODO()
  34. if c == nil {
  35. t.Fatalf("TODO returned nil")
  36. }
  37. select {
  38. case x := <-c.Done():
  39. t.Errorf("<-c.Done() == %v want nothing (it should block)", x)
  40. default:
  41. }
  42. if s := fmt.Sprint(c); s != "context.TODO" {
  43. t.Errorf(`TODO.String = %q want "context.TODO"`, s)
  44. }
  45. }
  46. func TestWithCancel(t *testing.T) {
  47. c1, cancel := WithCancel(Background())
  48. o := otherContext{c1}
  49. c2 := newCtx(o, maybeCanceled)
  50. contexts := []Context{c1, o, c2}
  51. for i, c := range contexts {
  52. if d := c.Done(); d == nil {
  53. t.Errorf("c[%d].Done() == %v want non-nil", i, d)
  54. }
  55. if e := c.Err(); e != nil {
  56. t.Errorf("c[%d].Err() == %v want nil", i, e)
  57. }
  58. select {
  59. case x := <-c.Done():
  60. t.Errorf("<-c.Done() == %v want nothing (it should block)", x)
  61. default:
  62. }
  63. }
  64. cancel()
  65. time.Sleep(100 * time.Millisecond) // let cancellation propagate
  66. for i, c := range contexts {
  67. select {
  68. case <-c.Done():
  69. default:
  70. t.Errorf("<-c[%d].Done() blocked, but shouldn't have", i)
  71. }
  72. if e := c.Err(); e != Canceled {
  73. t.Errorf("c[%d].Err() == %v want %v", i, e, Canceled)
  74. }
  75. }
  76. }
  77. func TestParentFinishesChild(t *testing.T) {
  78. parent, cancel := WithCancel(Background())
  79. pctx := parent.(*ctx)
  80. child1 := newCtx(parent, maybeCanceled)
  81. child2 := newCtx(parent, neverCanceled)
  82. select {
  83. case x := <-parent.Done():
  84. t.Errorf("<-parent.Done() == %v want nothing (it should block)", x)
  85. case x := <-child1.Done():
  86. t.Errorf("<-child1.Done() == %v want nothing (it should block)", x)
  87. case x := <-child2.Done():
  88. t.Errorf("<-child2.Done() == %v want nothing (it should block)", x)
  89. default:
  90. }
  91. pctx.mu.Lock()
  92. if len(pctx.children) != 2 ||
  93. !pctx.children[child1] || child1.parent != pctx ||
  94. !pctx.children[child2] || child2.parent != pctx {
  95. t.Errorf("bad linkage: pctx.children = %v, child1.parent = %v, child2.parent = %v",
  96. pctx.children, child1.parent, child2.parent)
  97. }
  98. pctx.mu.Unlock()
  99. cancel()
  100. pctx.mu.Lock()
  101. if len(pctx.children) != 0 {
  102. t.Errorf("pctx.cancel didn't clear pctx.children = %v", pctx.children)
  103. }
  104. pctx.mu.Unlock()
  105. // parent and children should all be finished.
  106. select {
  107. case <-parent.Done():
  108. default:
  109. t.Errorf("<-parent.Done() blocked, but shouldn't have")
  110. }
  111. if e := parent.Err(); e != Canceled {
  112. t.Errorf("parent.Err() == %v want %v", e, Canceled)
  113. }
  114. select {
  115. case <-child1.Done():
  116. default:
  117. t.Errorf("<-child1.Done() blocked, but shouldn't have")
  118. }
  119. if e := child1.Err(); e != Canceled {
  120. t.Errorf("child1.Err() == %v want %v", e, Canceled)
  121. }
  122. select {
  123. case <-child2.Done():
  124. default:
  125. t.Errorf("<-child2.Done() blocked, but shouldn't have")
  126. }
  127. if e := child2.Err(); e != Canceled {
  128. t.Errorf("child2.Err() == %v want %v", e, Canceled)
  129. }
  130. // New should return a canceled context on a canceled parent.
  131. child3 := newCtx(parent, neverCanceled)
  132. select {
  133. case <-child3.Done():
  134. default:
  135. t.Errorf("<-child3.Done() blocked, but shouldn't have")
  136. }
  137. if e := child3.Err(); e != Canceled {
  138. t.Errorf("child3.Err() == %v want %v", e, Canceled)
  139. }
  140. }
  141. func TestChildFinishesFirst(t *testing.T) {
  142. for _, parentMayCancel := range []bool{neverCanceled, maybeCanceled} {
  143. parent := newCtx(nil, parentMayCancel)
  144. child, cancel := WithCancel(parent)
  145. pctx := parent
  146. cctx := child.(*ctx)
  147. select {
  148. case x := <-parent.Done():
  149. t.Errorf("<-parent.Done() == %v want nothing (it should block)", x)
  150. case x := <-child.Done():
  151. t.Errorf("<-child.Done() == %v want nothing (it should block)", x)
  152. default:
  153. }
  154. if cctx.parent != pctx {
  155. t.Errorf("bad linkage: cctx.parent = %v, parent = %v", cctx.parent, pctx)
  156. }
  157. if parentMayCancel {
  158. pctx.mu.Lock()
  159. if len(pctx.children) != 1 || !pctx.children[cctx] {
  160. t.Errorf("bad linkage: pctx.children = %v, cctx = %v", pctx.children, cctx)
  161. }
  162. pctx.mu.Unlock()
  163. }
  164. cancel()
  165. pctx.mu.Lock()
  166. if len(pctx.children) != 0 {
  167. t.Errorf("child.Cancel didn't remove self from pctx.children = %v", pctx.children)
  168. }
  169. pctx.mu.Unlock()
  170. // child should be finished.
  171. select {
  172. case <-child.Done():
  173. default:
  174. t.Errorf("<-child.Done() blocked, but shouldn't have")
  175. }
  176. if e := child.Err(); e != Canceled {
  177. t.Errorf("child.Err() == %v want %v", e, Canceled)
  178. }
  179. // parent should not be finished.
  180. select {
  181. case x := <-parent.Done():
  182. t.Errorf("<-parent.Done() == %v want nothing (it should block)", x)
  183. default:
  184. }
  185. if e := parent.Err(); e != nil {
  186. t.Errorf("parent.Err() == %v want nil", e)
  187. }
  188. }
  189. }
  190. func testDeadline(c Context, wait time.Duration, t *testing.T) {
  191. select {
  192. case <-time.After(wait):
  193. t.Fatalf("context should have timed out")
  194. case <-c.Done():
  195. }
  196. if e := c.Err(); e != DeadlineExceeded {
  197. t.Errorf("c.Err() == %v want %v", e, DeadlineExceeded)
  198. }
  199. }
  200. func TestDeadline(t *testing.T) {
  201. c, _ := WithDeadline(nil, time.Now().Add(100*time.Millisecond))
  202. testDeadline(c, 200*time.Millisecond, t)
  203. c, _ = WithDeadline(nil, time.Now().Add(100*time.Millisecond))
  204. o := otherContext{c}
  205. testDeadline(o, 200*time.Millisecond, t)
  206. c, _ = WithDeadline(nil, time.Now().Add(100*time.Millisecond))
  207. o = otherContext{c}
  208. c, _ = WithDeadline(o, time.Now().Add(300*time.Millisecond))
  209. testDeadline(c, 200*time.Millisecond, t)
  210. }
  211. func TestTimeout(t *testing.T) {
  212. c, _ := WithTimeout(nil, 100*time.Millisecond)
  213. testDeadline(c, 200*time.Millisecond, t)
  214. c, _ = WithTimeout(nil, 100*time.Millisecond)
  215. o := otherContext{c}
  216. testDeadline(o, 200*time.Millisecond, t)
  217. c, _ = WithTimeout(nil, 100*time.Millisecond)
  218. o = otherContext{c}
  219. c, _ = WithTimeout(o, 300*time.Millisecond)
  220. testDeadline(c, 200*time.Millisecond, t)
  221. }
  222. func TestCancelledTimeout(t *testing.T) {
  223. c, _ := WithTimeout(nil, 200*time.Millisecond)
  224. o := otherContext{c}
  225. c, cancel := WithTimeout(o, 400*time.Millisecond)
  226. cancel()
  227. time.Sleep(100 * time.Millisecond) // let cancellation propagate
  228. select {
  229. case <-c.Done():
  230. default:
  231. t.Errorf("<-c.Done() blocked, but shouldn't have")
  232. }
  233. if e := c.Err(); e != Canceled {
  234. t.Errorf("c.Err() == %v want %v", e, Canceled)
  235. }
  236. }
  237. type key1 int
  238. type key2 int
  239. var k1 = key1(1)
  240. var k2 = key2(1) // same int as k1, different type
  241. var k3 = key2(3) // same type as k2, different int
  242. func TestValues(t *testing.T) {
  243. check := func(c Context, nm, v1, v2, v3 string) {
  244. if v, ok := c.Value(k1).(string); ok == (len(v1) == 0) || v != v1 {
  245. t.Errorf(`%s.Value(k1).(string) = %q, %t want %q, %t`, nm, v, ok, v1, len(v1) != 0)
  246. }
  247. if v, ok := c.Value(k2).(string); ok == (len(v2) == 0) || v != v2 {
  248. t.Errorf(`%s.Value(k2).(string) = %q, %t want %q, %t`, nm, v, ok, v2, len(v2) != 0)
  249. }
  250. if v, ok := c.Value(k3).(string); ok == (len(v3) == 0) || v != v3 {
  251. t.Errorf(`%s.Value(k3).(string) = %q, %t want %q, %t`, nm, v, ok, v3, len(v3) != 0)
  252. }
  253. }
  254. c0 := Background()
  255. check(c0, "c0", "", "", "")
  256. c1 := WithValue(nil, k1, "c1k1")
  257. check(c1, "c1", "c1k1", "", "")
  258. c2 := WithValue(c1, k2, "c2k2")
  259. check(c2, "c2", "c1k1", "c2k2", "")
  260. c3 := WithValue(c2, k3, "c3k3")
  261. check(c3, "c2", "c1k1", "c2k2", "c3k3")
  262. c4 := WithValue(c3, k1, nil)
  263. check(c4, "c4", "", "c2k2", "c3k3")
  264. o0 := otherContext{Background()}
  265. check(o0, "o0", "", "", "")
  266. o1 := otherContext{WithValue(nil, k1, "c1k1")}
  267. check(o1, "o1", "c1k1", "", "")
  268. o2 := WithValue(o1, k2, "o2k2")
  269. check(o2, "o2", "c1k1", "o2k2", "")
  270. o3 := otherContext{c4}
  271. check(o3, "o3", "", "c2k2", "c3k3")
  272. o4 := WithValue(o3, k3, nil)
  273. check(o4, "o4", "", "c2k2", "")
  274. }
  275. func TestAllocs(t *testing.T) {
  276. bg := Background()
  277. for _, test := range []struct {
  278. desc string
  279. f func()
  280. limit float64
  281. gccgoLimit float64
  282. }{
  283. {
  284. desc: "Background()",
  285. f: func() { Background() },
  286. limit: 0,
  287. gccgoLimit: 0,
  288. },
  289. {
  290. desc: fmt.Sprintf("WithValue(bg, %v, nil)", k1),
  291. f: func() {
  292. c := WithValue(bg, k1, nil)
  293. c.Value(k1)
  294. },
  295. limit: 3,
  296. gccgoLimit: 3,
  297. },
  298. {
  299. desc: "WithTimeout(bg, 15*time.Millisecond)",
  300. f: func() {
  301. c, _ := WithTimeout(bg, 15*time.Millisecond)
  302. <-c.Done()
  303. },
  304. limit: 9,
  305. gccgoLimit: 13,
  306. },
  307. {
  308. desc: "WithCancel(bg)",
  309. f: func() {
  310. c, cancel := WithCancel(bg)
  311. cancel()
  312. <-c.Done()
  313. },
  314. limit: 7,
  315. gccgoLimit: 8,
  316. },
  317. {
  318. desc: "WithTimeout(bg, 100*time.Millisecond)",
  319. f: func() {
  320. c, cancel := WithTimeout(bg, 100*time.Millisecond)
  321. cancel()
  322. <-c.Done()
  323. },
  324. limit: 16,
  325. gccgoLimit: 25,
  326. },
  327. } {
  328. limit := test.limit
  329. if runtime.Compiler == "gccgo" {
  330. // gccgo does not yet do escape analysis.
  331. // TOOD(iant): Remove this when gccgo does do escape analysis.
  332. limit = test.gccgoLimit
  333. }
  334. if n := testing.AllocsPerRun(100, test.f); n > limit {
  335. t.Errorf("%s allocs = %f want %d", test.desc, n, int(limit))
  336. }
  337. }
  338. }
  339. func TestSimultaneousCancels(t *testing.T) {
  340. root, cancel := WithCancel(Background())
  341. m := map[Context]CancelFunc{root: cancel}
  342. q := []Context{root}
  343. // Create a tree of contexts.
  344. for len(q) != 0 && len(m) < 100 {
  345. parent := q[0]
  346. q = q[1:]
  347. for i := 0; i < 4; i++ {
  348. ctx, cancel := WithCancel(parent)
  349. m[ctx] = cancel
  350. q = append(q, ctx)
  351. }
  352. }
  353. // Start all the cancels in a random order.
  354. var wg sync.WaitGroup
  355. wg.Add(len(m))
  356. for _, cancel := range m {
  357. go func(cancel CancelFunc) {
  358. cancel()
  359. wg.Done()
  360. }(cancel)
  361. }
  362. // Wait on all the contexts in a random order.
  363. for ctx := range m {
  364. select {
  365. case <-ctx.Done():
  366. case <-time.After(1 * time.Second):
  367. buf := make([]byte, 10<<10)
  368. n := runtime.Stack(buf, true)
  369. t.Fatalf("timed out waiting for <-ctx.Done(); stacks:\n%s", buf[:n])
  370. }
  371. }
  372. // Wait for all the cancel functions to return.
  373. done := make(chan struct{})
  374. go func() {
  375. wg.Wait()
  376. close(done)
  377. }()
  378. select {
  379. case <-done:
  380. case <-time.After(1 * time.Second):
  381. buf := make([]byte, 10<<10)
  382. n := runtime.Stack(buf, true)
  383. t.Fatalf("timed out waiting for cancel functions; stacks:\n%s", buf[:n])
  384. }
  385. }
  386. func TestInterlockedCancels(t *testing.T) {
  387. parent, cancelParent := WithCancel(Background())
  388. child, cancelChild := WithCancel(parent)
  389. go func() {
  390. parent.Done()
  391. cancelChild()
  392. }()
  393. cancelParent()
  394. select {
  395. case <-child.Done():
  396. case <-time.After(1 * time.Second):
  397. buf := make([]byte, 10<<10)
  398. n := runtime.Stack(buf, true)
  399. t.Fatalf("timed out waiting for child.Done(); stacks:\n%s", buf[:n])
  400. }
  401. }
  402. func TestLayersCancel(t *testing.T) {
  403. testLayers(t, time.Now().UnixNano(), false)
  404. }
  405. func TestLayersTimeout(t *testing.T) {
  406. testLayers(t, time.Now().UnixNano(), true)
  407. }
  408. func testLayers(t *testing.T, seed int64, testTimeout bool) {
  409. rand.Seed(seed)
  410. errorf := func(format string, a ...interface{}) {
  411. t.Errorf(fmt.Sprintf("seed=%d: %s", seed, format), a...)
  412. }
  413. const (
  414. timeout = 200 * time.Millisecond
  415. minLayers = 30
  416. )
  417. type value int
  418. var (
  419. vals []*value
  420. cancels []CancelFunc
  421. numTimers int
  422. ctx = Background()
  423. )
  424. for i := 0; i < minLayers || numTimers == 0 || len(cancels) == 0 || len(vals) == 0; i++ {
  425. switch rand.Intn(3) {
  426. case 0:
  427. v := new(value)
  428. t.Logf("WithValue(%p, %p)", v, v)
  429. ctx = WithValue(ctx, v, v)
  430. vals = append(vals, v)
  431. case 1:
  432. var cancel CancelFunc
  433. t.Logf("WithCancel")
  434. ctx, cancel = WithCancel(ctx)
  435. cancels = append(cancels, cancel)
  436. case 2:
  437. var cancel CancelFunc
  438. t.Logf("WithTimeout")
  439. ctx, cancel = WithTimeout(ctx, timeout)
  440. cancels = append(cancels, cancel)
  441. numTimers++
  442. }
  443. }
  444. checkValues := func(when string) {
  445. for _, key := range vals {
  446. if val := ctx.Value(key).(*value); key != val {
  447. errorf("%s: ctx.Value(%p) = %p want %p", when, key, val, key)
  448. }
  449. }
  450. }
  451. select {
  452. case <-ctx.Done():
  453. errorf("ctx should not be canceled yet")
  454. default:
  455. }
  456. checkValues("before cancel")
  457. if testTimeout {
  458. select {
  459. case <-ctx.Done():
  460. case <-time.After(timeout + timeout/10):
  461. errorf("ctx should have timed out")
  462. }
  463. checkValues("after timeout")
  464. } else {
  465. cancel := cancels[rand.Intn(len(cancels))]
  466. cancel()
  467. select {
  468. case <-ctx.Done():
  469. default:
  470. errorf("ctx should be canceled")
  471. }
  472. checkValues("after cancel")
  473. }
  474. }