pipe_test.go 3.0 KB

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