stream_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package rafthttp
  2. import (
  3. "errors"
  4. "net/http"
  5. "net/http/httptest"
  6. "reflect"
  7. "testing"
  8. "github.com/coreos/etcd/etcdserver/stats"
  9. "github.com/coreos/etcd/pkg/testutil"
  10. "github.com/coreos/etcd/pkg/types"
  11. "github.com/coreos/etcd/raft/raftpb"
  12. )
  13. // TestStreamWriterAttachOutgoingConn tests that outgoingConn can be attached
  14. // to streamWriter. After that, streamWriter can use it to send messages
  15. // continuously, and closes it when stopped.
  16. func TestStreamWriterAttachOutgoingConn(t *testing.T) {
  17. sw := startStreamWriter(&stats.FollowerStats{}, &fakeRaft{})
  18. // the expected initial state of streamWrite is not working
  19. if g := sw.isWorking(); g != false {
  20. t.Errorf("initial working status = %v, want false", g)
  21. }
  22. // repeatitive tests to ensure it can use latest connection
  23. var wfc *fakeWriteFlushCloser
  24. for i := 0; i < 3; i++ {
  25. prevwfc := wfc
  26. wfc = &fakeWriteFlushCloser{}
  27. sw.attach(&outgoingConn{t: streamTypeMessage, Writer: wfc, Flusher: wfc, Closer: wfc})
  28. testutil.ForceGosched()
  29. // previous attached connection should be closed
  30. if prevwfc != nil && prevwfc.closed != true {
  31. t.Errorf("#%d: close of previous connection = %v, want true", i, prevwfc.closed)
  32. }
  33. // starts working
  34. if g := sw.isWorking(); g != true {
  35. t.Errorf("#%d: working status = %v, want true", i, g)
  36. }
  37. sw.msgc <- raftpb.Message{}
  38. testutil.ForceGosched()
  39. // still working
  40. if g := sw.isWorking(); g != true {
  41. t.Errorf("#%d: working status = %v, want true", i, g)
  42. }
  43. if wfc.written == 0 {
  44. t.Errorf("#%d: failed to write to the underlying connection", i)
  45. }
  46. }
  47. sw.stop()
  48. // no longer in working status now
  49. if g := sw.isWorking(); g != false {
  50. t.Errorf("working status after stop = %v, want false", g)
  51. }
  52. if wfc.closed != true {
  53. t.Errorf("failed to close the underlying connection")
  54. }
  55. }
  56. // TestStreamWriterAttachBadOutgoingConn tests that streamWriter with bad
  57. // outgoingConn will close the outgoingConn and fall back to non-working status.
  58. func TestStreamWriterAttachBadOutgoingConn(t *testing.T) {
  59. sw := startStreamWriter(&stats.FollowerStats{}, &fakeRaft{})
  60. defer sw.stop()
  61. wfc := &fakeWriteFlushCloser{err: errors.New("blah")}
  62. sw.attach(&outgoingConn{t: streamTypeMessage, Writer: wfc, Flusher: wfc, Closer: wfc})
  63. sw.msgc <- raftpb.Message{}
  64. testutil.ForceGosched()
  65. // no longer working
  66. if g := sw.isWorking(); g != false {
  67. t.Errorf("working = %v, want false", g)
  68. }
  69. if wfc.closed != true {
  70. t.Errorf("failed to close the underlying connection")
  71. }
  72. }
  73. func TestStreamReaderDialRequest(t *testing.T) {
  74. for i, tt := range []streamType{streamTypeMsgApp, streamTypeMessage} {
  75. tr := &roundTripperRecorder{}
  76. sr := &streamReader{
  77. tr: tr,
  78. u: "http://localhost:7001",
  79. t: tt,
  80. from: types.ID(1),
  81. to: types.ID(2),
  82. cid: types.ID(1),
  83. msgAppTerm: 1,
  84. }
  85. sr.dial()
  86. req := tr.Request()
  87. var wurl string
  88. switch tt {
  89. case streamTypeMsgApp:
  90. wurl = "http://localhost:7001/raft/stream/1"
  91. case streamTypeMessage:
  92. wurl = "http://localhost:7001/raft/stream/message/1"
  93. }
  94. if req.URL.String() != wurl {
  95. t.Errorf("#%d: url = %s, want %s", i, req.URL.String(), wurl)
  96. }
  97. if w := "GET"; req.Method != w {
  98. t.Errorf("#%d: method = %s, want %s", i, req.Method, w)
  99. }
  100. if g := req.Header.Get("X-Etcd-Cluster-ID"); g != "1" {
  101. t.Errorf("#%d: header X-Etcd-Cluster-ID = %s, want 1", i, g)
  102. }
  103. if g := req.Header.Get("X-Raft-To"); g != "2" {
  104. t.Errorf("#%d: header X-Raft-To = %s, want 2", i, g)
  105. }
  106. if g := req.Header.Get("X-Raft-Term"); tt == streamTypeMsgApp && g != "1" {
  107. t.Errorf("#%d: header X-Raft-Term = %s, want 1", i, g)
  108. }
  109. }
  110. }
  111. // TestStreamReaderDialResult tests the result of the dial func call meets the
  112. // HTTP response received.
  113. func TestStreamReaderDialResult(t *testing.T) {
  114. tests := []struct {
  115. code int
  116. err error
  117. wok bool
  118. }{
  119. {0, errors.New("blah"), false},
  120. {http.StatusOK, nil, true},
  121. {http.StatusMethodNotAllowed, nil, false},
  122. {http.StatusNotFound, nil, false},
  123. {http.StatusPreconditionFailed, nil, false},
  124. }
  125. for i, tt := range tests {
  126. tr := newRespRoundTripper(tt.code, tt.err)
  127. sr := &streamReader{
  128. tr: tr,
  129. u: "http://localhost:7001",
  130. t: streamTypeMessage,
  131. from: types.ID(1),
  132. to: types.ID(2),
  133. cid: types.ID(1),
  134. }
  135. _, err := sr.dial()
  136. if ok := err == nil; ok != tt.wok {
  137. t.Errorf("#%d: ok = %v, want %v", i, ok, tt.wok)
  138. }
  139. }
  140. }
  141. // TestStream tests that streamReader and streamWriter can build stream to
  142. // send messages between each other.
  143. func TestStream(t *testing.T) {
  144. tests := []struct {
  145. t streamType
  146. term uint64
  147. m raftpb.Message
  148. }{
  149. {
  150. streamTypeMessage,
  151. 0,
  152. raftpb.Message{Type: raftpb.MsgProp, To: 2},
  153. },
  154. {
  155. streamTypeMsgApp,
  156. 1,
  157. raftpb.Message{
  158. Type: raftpb.MsgApp,
  159. From: 2,
  160. To: 1,
  161. Term: 1,
  162. LogTerm: 1,
  163. Index: 3,
  164. Entries: []raftpb.Entry{{Term: 1, Index: 4}},
  165. },
  166. },
  167. }
  168. for i, tt := range tests {
  169. h := &fakeStreamHandler{t: tt.t}
  170. srv := httptest.NewServer(h)
  171. defer srv.Close()
  172. sw := startStreamWriter(&stats.FollowerStats{}, &fakeRaft{})
  173. defer sw.stop()
  174. h.sw = sw
  175. recvc := make(chan raftpb.Message)
  176. sr := startStreamReader(&http.Transport{}, srv.URL, tt.t, types.ID(1), types.ID(2), types.ID(1), recvc)
  177. defer sr.stop()
  178. if tt.t == streamTypeMsgApp {
  179. sr.updateMsgAppTerm(tt.term)
  180. }
  181. sw.msgc <- tt.m
  182. m := <-recvc
  183. if !reflect.DeepEqual(m, tt.m) {
  184. t.Errorf("#%d: message = %+v, want %+v", i, m, tt.m)
  185. }
  186. }
  187. }
  188. type fakeWriteFlushCloser struct {
  189. err error
  190. written int
  191. closed bool
  192. }
  193. func (wfc *fakeWriteFlushCloser) Write(p []byte) (n int, err error) {
  194. wfc.written += len(p)
  195. return len(p), wfc.err
  196. }
  197. func (wfc *fakeWriteFlushCloser) Flush() {}
  198. func (wfc *fakeWriteFlushCloser) Close() error {
  199. wfc.closed = true
  200. return wfc.err
  201. }
  202. type fakeStreamHandler struct {
  203. t streamType
  204. sw *streamWriter
  205. }
  206. func (h *fakeStreamHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  207. w.(http.Flusher).Flush()
  208. c := newCloseNotifier()
  209. h.sw.attach(&outgoingConn{
  210. t: h.t,
  211. termStr: r.Header.Get("X-Raft-Term"),
  212. Writer: w,
  213. Flusher: w.(http.Flusher),
  214. Closer: c,
  215. })
  216. <-c.closeNotify()
  217. }