protocol_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package protocol
  2. import (
  3. "errors"
  4. "io"
  5. "testing"
  6. "testing/quick"
  7. "time"
  8. )
  9. func TestHeaderFunctions(t *testing.T) {
  10. f := func(ver, id, typ int) bool {
  11. ver = int(uint(ver) % 16)
  12. id = int(uint(id) % 4096)
  13. typ = int(uint(typ) % 256)
  14. h0 := header{ver, id, typ}
  15. h1 := decodeHeader(encodeHeader(h0))
  16. return h0 == h1
  17. }
  18. if err := quick.Check(f, nil); err != nil {
  19. t.Error(err)
  20. }
  21. }
  22. func TestPing(t *testing.T) {
  23. ar, aw := io.Pipe()
  24. br, bw := io.Pipe()
  25. c0 := NewConnection("c0", ar, bw, nil).(wireFormatConnection).next.(*rawConnection)
  26. c1 := NewConnection("c1", br, aw, nil).(wireFormatConnection).next.(*rawConnection)
  27. if ok := c0.ping(); !ok {
  28. t.Error("c0 ping failed")
  29. }
  30. if ok := c1.ping(); !ok {
  31. t.Error("c1 ping failed")
  32. }
  33. }
  34. func TestPingErr(t *testing.T) {
  35. e := errors.New("something broke")
  36. for i := 0; i < 12; i++ {
  37. for j := 0; j < 12; j++ {
  38. m0 := newTestModel()
  39. m1 := newTestModel()
  40. ar, aw := io.Pipe()
  41. br, bw := io.Pipe()
  42. eaw := &ErrPipe{PipeWriter: *aw, max: i, err: e}
  43. ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e}
  44. c0 := NewConnection("c0", ar, ebw, m0).(wireFormatConnection).next.(*rawConnection)
  45. NewConnection("c1", br, eaw, m1)
  46. res := c0.ping()
  47. if (i < 4 || j < 4) && res {
  48. t.Errorf("Unexpected ping success; i=%d, j=%d", i, j)
  49. } else if (i >= 8 && j >= 8) && !res {
  50. t.Errorf("Unexpected ping fail; i=%d, j=%d", i, j)
  51. }
  52. }
  53. }
  54. }
  55. // func TestRequestResponseErr(t *testing.T) {
  56. // e := errors.New("something broke")
  57. // var pass bool
  58. // for i := 0; i < 48; i++ {
  59. // for j := 0; j < 38; j++ {
  60. // m0 := newTestModel()
  61. // m0.data = []byte("response data")
  62. // m1 := newTestModel()
  63. // ar, aw := io.Pipe()
  64. // br, bw := io.Pipe()
  65. // eaw := &ErrPipe{PipeWriter: *aw, max: i, err: e}
  66. // ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e}
  67. // NewConnection("c0", ar, ebw, m0, nil)
  68. // c1 := NewConnection("c1", br, eaw, m1, nil).(wireFormatConnection).next.(*rawConnection)
  69. // d, err := c1.Request("default", "tn", 1234, 5678)
  70. // if err == e || err == ErrClosed {
  71. // t.Logf("Error at %d+%d bytes", i, j)
  72. // if !m1.isClosed() {
  73. // t.Fatal("c1 not closed")
  74. // }
  75. // if !m0.isClosed() {
  76. // t.Fatal("c0 not closed")
  77. // }
  78. // continue
  79. // }
  80. // if err != nil {
  81. // t.Fatal(err)
  82. // }
  83. // if string(d) != "response data" {
  84. // t.Fatalf("Incorrect response data %q", string(d))
  85. // }
  86. // if m0.repo != "default" {
  87. // t.Fatalf("Incorrect repo %q", m0.repo)
  88. // }
  89. // if m0.name != "tn" {
  90. // t.Fatalf("Incorrect name %q", m0.name)
  91. // }
  92. // if m0.offset != 1234 {
  93. // t.Fatalf("Incorrect offset %d", m0.offset)
  94. // }
  95. // if m0.size != 5678 {
  96. // t.Fatalf("Incorrect size %d", m0.size)
  97. // }
  98. // t.Logf("Pass at %d+%d bytes", i, j)
  99. // pass = true
  100. // }
  101. // }
  102. // if !pass {
  103. // t.Fatal("Never passed")
  104. // }
  105. // }
  106. func TestVersionErr(t *testing.T) {
  107. m0 := newTestModel()
  108. m1 := newTestModel()
  109. ar, aw := io.Pipe()
  110. br, bw := io.Pipe()
  111. c0 := NewConnection("c0", ar, bw, m0).(wireFormatConnection).next.(*rawConnection)
  112. NewConnection("c1", br, aw, m1)
  113. c0.xw.WriteUint32(encodeHeader(header{
  114. version: 2,
  115. msgID: 0,
  116. msgType: 0,
  117. }))
  118. c0.flush()
  119. if !m1.isClosed() {
  120. t.Error("Connection should close due to unknown version")
  121. }
  122. }
  123. func TestTypeErr(t *testing.T) {
  124. m0 := newTestModel()
  125. m1 := newTestModel()
  126. ar, aw := io.Pipe()
  127. br, bw := io.Pipe()
  128. c0 := NewConnection("c0", ar, bw, m0).(wireFormatConnection).next.(*rawConnection)
  129. NewConnection("c1", br, aw, m1)
  130. c0.xw.WriteUint32(encodeHeader(header{
  131. version: 0,
  132. msgID: 0,
  133. msgType: 42,
  134. }))
  135. c0.flush()
  136. if !m1.isClosed() {
  137. t.Error("Connection should close due to unknown message type")
  138. }
  139. }
  140. func TestClose(t *testing.T) {
  141. m0 := newTestModel()
  142. m1 := newTestModel()
  143. ar, aw := io.Pipe()
  144. br, bw := io.Pipe()
  145. c0 := NewConnection("c0", ar, bw, m0).(wireFormatConnection).next.(*rawConnection)
  146. NewConnection("c1", br, aw, m1)
  147. c0.close <- nil
  148. select {
  149. case <-c0.closed:
  150. case <-time.After(1 * time.Second):
  151. t.Fatal("Did not close within a second")
  152. }
  153. if !c0.isClosed() {
  154. t.Fatal("Connection should be closed")
  155. }
  156. if !m0.isClosed() {
  157. t.Fatal("Connection should be closed")
  158. }
  159. // None of these should panic, some should return an error
  160. if c0.ping() {
  161. t.Error("Ping should not return true")
  162. }
  163. c0.Index("default", nil)
  164. c0.Index("default", nil)
  165. if _, err := c0.Request("default", "foo", 0, 0); err == nil {
  166. t.Error("Request should return an error")
  167. }
  168. }