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.

130 lines
2.7 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. // Read and Write should fail.
  88. if n, err := p.Write([]byte("abc")); err != errClosedPipeWrite || n != 0 {
  89. t.Errorf("Write(abc) after close\ngot %v, %v\nwant 0, %v", n, err, errClosedPipeWrite)
  90. }
  91. if n, err := p.Read(make([]byte, 1)); err == nil || n != 0 {
  92. t.Errorf("Read() after close\ngot %v, nil\nwant 0, %v", n, errClosedPipeWrite)
  93. }
  94. }
  95. func TestPipeBreakWithError(t *testing.T) {
  96. p := &pipe{b: new(bytes.Buffer)}
  97. io.WriteString(p, "foo")
  98. a := errors.New("test err")
  99. p.BreakWithError(a)
  100. all, err := ioutil.ReadAll(p)
  101. if string(all) != "" {
  102. t.Errorf("read bytes = %q; want empty string", all)
  103. }
  104. if err != a {
  105. t.Logf("read error = %v, %v", err, a)
  106. }
  107. if p.b != nil {
  108. t.Errorf("buffer should be nil after BreakWithError")
  109. }
  110. // Write should succeed silently.
  111. if n, err := p.Write([]byte("abc")); err != nil || n != 3 {
  112. t.Errorf("Write(abc) after break\ngot %v, %v\nwant 0, nil", n, err)
  113. }
  114. if p.b != nil {
  115. t.Errorf("buffer should be nil after Write")
  116. }
  117. // Read should fail.
  118. if n, err := p.Read(make([]byte, 1)); err == nil || n != 0 {
  119. t.Errorf("Read() after close\ngot %v, nil\nwant 0, not nil", n)
  120. }
  121. }