You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

110 lines
1.9 KiB

  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 http2
  5. import (
  6. "bytes"
  7. "errors"
  8. "io"
  9. "io/ioutil"
  10. "testing"
  11. )
  12. func TestPipeClose(t *testing.T) {
  13. var p pipe
  14. p.b = new(bytes.Buffer)
  15. a := errors.New("a")
  16. b := errors.New("b")
  17. p.CloseWithError(a)
  18. p.CloseWithError(b)
  19. _, err := p.Read(make([]byte, 1))
  20. if err != a {
  21. t.Errorf("err = %v want %v", err, a)
  22. }
  23. }
  24. func TestPipeDoneChan(t *testing.T) {
  25. var p pipe
  26. done := p.Done()
  27. select {
  28. case <-done:
  29. t.Fatal("done too soon")
  30. default:
  31. }
  32. p.CloseWithError(io.EOF)
  33. select {
  34. case <-done:
  35. default:
  36. t.Fatal("should be done")
  37. }
  38. }
  39. func TestPipeDoneChan_ErrFirst(t *testing.T) {
  40. var p pipe
  41. p.CloseWithError(io.EOF)
  42. done := p.Done()
  43. select {
  44. case <-done:
  45. default:
  46. t.Fatal("should be done")
  47. }
  48. }
  49. func TestPipeDoneChan_Break(t *testing.T) {
  50. var p pipe
  51. done := p.Done()
  52. select {
  53. case <-done:
  54. t.Fatal("done too soon")
  55. default:
  56. }
  57. p.BreakWithError(io.EOF)
  58. select {
  59. case <-done:
  60. default:
  61. t.Fatal("should be done")
  62. }
  63. }
  64. func TestPipeDoneChan_Break_ErrFirst(t *testing.T) {
  65. var p pipe
  66. p.BreakWithError(io.EOF)
  67. done := p.Done()
  68. select {
  69. case <-done:
  70. default:
  71. t.Fatal("should be done")
  72. }
  73. }
  74. func TestPipeCloseWithError(t *testing.T) {
  75. p := &pipe{b: new(bytes.Buffer)}
  76. const body = "foo"
  77. io.WriteString(p, body)
  78. a := errors.New("test error")
  79. p.CloseWithError(a)
  80. all, err := ioutil.ReadAll(p)
  81. if string(all) != body {
  82. t.Errorf("read bytes = %q; want %q", all, body)
  83. }
  84. if err != a {
  85. t.Logf("read error = %v, %v", err, a)
  86. }
  87. }
  88. func TestPipeBreakWithError(t *testing.T) {
  89. p := &pipe{b: new(bytes.Buffer)}
  90. io.WriteString(p, "foo")
  91. a := errors.New("test err")
  92. p.BreakWithError(a)
  93. all, err := ioutil.ReadAll(p)
  94. if string(all) != "" {
  95. t.Errorf("read bytes = %q; want empty string", all)
  96. }
  97. if err != a {
  98. t.Logf("read error = %v, %v", err, a)
  99. }
  100. }