protocol_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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)
  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*BlockSize {
  93. t.Error("Incorrect offset %d", m0.offset)
  94. }
  95. if m0.size != BlockSize {
  96. t.Error("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.Error("Never passed")
  104. }
  105. }
  106. func TestVersionErr(t *testing.T) {
  107. m0 := &TestModel{}
  108. m1 := &TestModel{}
  109. ar, aw := io.Pipe()
  110. br, bw := io.Pipe()
  111. c0 := NewConnection("c0", ar, bw, m0, nil)
  112. NewConnection("c1", br, aw, m1, nil)
  113. c0.xw.WriteUint32(encodeHeader(header{
  114. version: 2,
  115. msgID: 0,
  116. msgType: 0,
  117. }))
  118. c0.flush()
  119. if !m1.closed {
  120. t.Error("Connection should close due to unknown version")
  121. }
  122. }
  123. func TestTypeErr(t *testing.T) {
  124. m0 := &TestModel{}
  125. m1 := &TestModel{}
  126. ar, aw := io.Pipe()
  127. br, bw := io.Pipe()
  128. c0 := NewConnection("c0", ar, bw, m0, nil)
  129. NewConnection("c1", br, aw, m1, nil)
  130. c0.xw.WriteUint32(encodeHeader(header{
  131. version: 0,
  132. msgID: 0,
  133. msgType: 42,
  134. }))
  135. c0.flush()
  136. if !m1.closed {
  137. t.Error("Connection should close due to unknown message type")
  138. }
  139. }
  140. func TestClose(t *testing.T) {
  141. m0 := &TestModel{}
  142. m1 := &TestModel{}
  143. ar, aw := io.Pipe()
  144. br, bw := io.Pipe()
  145. c0 := NewConnection("c0", ar, bw, m0, nil)
  146. NewConnection("c1", br, aw, m1, nil)
  147. c0.close(nil)
  148. ok := c0.isClosed()
  149. if !ok {
  150. t.Fatal("Connection should be closed")
  151. }
  152. // None of these should panic, some should return an error
  153. ok = c0.ping()
  154. if ok {
  155. t.Error("Ping should not return true")
  156. }
  157. c0.Index("default", nil)
  158. c0.Index("default", nil)
  159. _, err := c0.Request("default", "foo", 0)
  160. if err == nil {
  161. t.Error("Request should return an error")
  162. }
  163. }