protocol_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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, nil)
  26. c1 := NewConnection("c1", br, aw, nil, nil)
  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 := &TestModel{}
  39. m1 := &TestModel{}
  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, nil)
  45. NewConnection("c1", br, eaw, m1, nil)
  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 := &TestModel{data: []byte("response data")}
  61. m1 := &TestModel{}
  62. ar, aw := io.Pipe()
  63. br, bw := io.Pipe()
  64. eaw := &ErrPipe{PipeWriter: *aw, max: i, err: e}
  65. ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e}
  66. NewConnection("c0", ar, ebw, m0, nil)
  67. c1 := NewConnection("c1", br, eaw, m1, nil)
  68. d, err := c1.Request("default", "tn", 1234, 3456, []byte("hashbytes"))
  69. if err == e || err == ErrClosed {
  70. t.Logf("Error at %d+%d bytes", i, j)
  71. if !m1.closed {
  72. t.Error("c1 not closed")
  73. }
  74. time.Sleep(1 * time.Millisecond)
  75. if !m0.closed {
  76. t.Error("c0 not closed")
  77. }
  78. continue
  79. }
  80. if err != nil {
  81. t.Error(err)
  82. }
  83. if string(d) != "response data" {
  84. t.Errorf("Incorrect response data %q", string(d))
  85. }
  86. if m0.repo != "default" {
  87. t.Error("Incorrect repo %q", m0.repo)
  88. }
  89. if m0.name != "tn" {
  90. t.Error("Incorrect name %q", m0.name)
  91. }
  92. if m0.offset != 1234 {
  93. t.Error("Incorrect offset %d", m0.offset)
  94. }
  95. if m0.size != 3456 {
  96. t.Error("Incorrect size %d", m0.size)
  97. }
  98. if string(m0.hash) != "hashbytes" {
  99. t.Error("Incorrect hash %q", m0.hash)
  100. }
  101. t.Logf("Pass at %d+%d bytes", i, j)
  102. pass = true
  103. }
  104. }
  105. if !pass {
  106. t.Error("Never passed")
  107. }
  108. }
  109. func TestVersionErr(t *testing.T) {
  110. m0 := &TestModel{}
  111. m1 := &TestModel{}
  112. ar, aw := io.Pipe()
  113. br, bw := io.Pipe()
  114. c0 := NewConnection("c0", ar, bw, m0, nil)
  115. NewConnection("c1", br, aw, m1, nil)
  116. c0.mwriter.writeHeader(header{
  117. version: 2,
  118. msgID: 0,
  119. msgType: 0,
  120. })
  121. c0.flush()
  122. if !m1.closed {
  123. t.Error("Connection should close due to unknown version")
  124. }
  125. }
  126. func TestTypeErr(t *testing.T) {
  127. m0 := &TestModel{}
  128. m1 := &TestModel{}
  129. ar, aw := io.Pipe()
  130. br, bw := io.Pipe()
  131. c0 := NewConnection("c0", ar, bw, m0, nil)
  132. NewConnection("c1", br, aw, m1, nil)
  133. c0.mwriter.writeHeader(header{
  134. version: 0,
  135. msgID: 0,
  136. msgType: 42,
  137. })
  138. c0.flush()
  139. if !m1.closed {
  140. t.Error("Connection should close due to unknown message type")
  141. }
  142. }
  143. func TestClose(t *testing.T) {
  144. m0 := &TestModel{}
  145. m1 := &TestModel{}
  146. ar, aw := io.Pipe()
  147. br, bw := io.Pipe()
  148. c0 := NewConnection("c0", ar, bw, m0, nil)
  149. NewConnection("c1", br, aw, m1, nil)
  150. c0.close(nil)
  151. ok := c0.isClosed()
  152. if !ok {
  153. t.Fatal("Connection should be closed")
  154. }
  155. // None of these should panic, some should return an error
  156. ok = c0.ping()
  157. if ok {
  158. t.Error("Ping should not return true")
  159. }
  160. c0.Index("default", nil)
  161. c0.Index("default", nil)
  162. _, err := c0.Request("default", "foo", 0, 0, nil)
  163. if err == nil {
  164. t.Error("Request should return an error")
  165. }
  166. }