pipeline_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rafthttp
  15. import (
  16. "errors"
  17. "io/ioutil"
  18. "net/http"
  19. "sync"
  20. "testing"
  21. "github.com/coreos/etcd/etcdserver/stats"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. "github.com/coreos/etcd/pkg/types"
  24. "github.com/coreos/etcd/raft/raftpb"
  25. )
  26. // TestPipelineSend tests that pipeline could send data using roundtripper
  27. // and increase success count in stats.
  28. func TestPipelineSend(t *testing.T) {
  29. tr := &roundTripperRecorder{}
  30. picker := mustNewURLPicker(t, []string{"http://localhost:2380"})
  31. fs := &stats.FollowerStats{}
  32. p := newPipeline(tr, picker, types.ID(1), types.ID(1), fs, &fakeRaft{}, nil)
  33. p.msgc <- raftpb.Message{Type: raftpb.MsgApp}
  34. p.stop()
  35. if tr.Request() == nil {
  36. t.Errorf("sender fails to post the data")
  37. }
  38. fs.Lock()
  39. defer fs.Unlock()
  40. if fs.Counts.Success != 1 {
  41. t.Errorf("success = %d, want 1", fs.Counts.Success)
  42. }
  43. }
  44. func TestPipelineExceedMaximalServing(t *testing.T) {
  45. tr := newRoundTripperBlocker()
  46. picker := mustNewURLPicker(t, []string{"http://localhost:2380"})
  47. fs := &stats.FollowerStats{}
  48. p := newPipeline(tr, picker, types.ID(1), types.ID(1), fs, &fakeRaft{}, nil)
  49. // keep the sender busy and make the buffer full
  50. // nothing can go out as we block the sender
  51. testutil.ForceGosched()
  52. for i := 0; i < connPerPipeline+pipelineBufSize; i++ {
  53. select {
  54. case p.msgc <- raftpb.Message{}:
  55. default:
  56. t.Errorf("failed to send out message")
  57. }
  58. // force the sender to grab data
  59. testutil.ForceGosched()
  60. }
  61. // try to send a data when we are sure the buffer is full
  62. select {
  63. case p.msgc <- raftpb.Message{}:
  64. t.Errorf("unexpected message sendout")
  65. default:
  66. }
  67. // unblock the senders and force them to send out the data
  68. tr.unblock()
  69. testutil.ForceGosched()
  70. // It could send new data after previous ones succeed
  71. select {
  72. case p.msgc <- raftpb.Message{}:
  73. default:
  74. t.Errorf("failed to send out message")
  75. }
  76. p.stop()
  77. }
  78. // TestPipelineSendFailed tests that when send func meets the post error,
  79. // it increases fail count in stats.
  80. func TestPipelineSendFailed(t *testing.T) {
  81. picker := mustNewURLPicker(t, []string{"http://localhost:2380"})
  82. fs := &stats.FollowerStats{}
  83. p := newPipeline(newRespRoundTripper(0, errors.New("blah")), picker, types.ID(1), types.ID(1), fs, &fakeRaft{}, nil)
  84. p.msgc <- raftpb.Message{Type: raftpb.MsgApp}
  85. p.stop()
  86. fs.Lock()
  87. defer fs.Unlock()
  88. if fs.Counts.Fail != 1 {
  89. t.Errorf("fail = %d, want 1", fs.Counts.Fail)
  90. }
  91. }
  92. func TestPipelinePost(t *testing.T) {
  93. tr := &roundTripperRecorder{}
  94. picker := mustNewURLPicker(t, []string{"http://localhost:2380"})
  95. p := newPipeline(tr, picker, types.ID(1), types.ID(1), nil, &fakeRaft{}, nil)
  96. if err := p.post([]byte("some data")); err != nil {
  97. t.Fatalf("unexpect post error: %v", err)
  98. }
  99. p.stop()
  100. if g := tr.Request().Method; g != "POST" {
  101. t.Errorf("method = %s, want %s", g, "POST")
  102. }
  103. if g := tr.Request().URL.String(); g != "http://localhost:2380/raft" {
  104. t.Errorf("url = %s, want %s", g, "http://localhost:2380/raft")
  105. }
  106. if g := tr.Request().Header.Get("Content-Type"); g != "application/protobuf" {
  107. t.Errorf("content type = %s, want %s", g, "application/protobuf")
  108. }
  109. if g := tr.Request().Header.Get("X-Etcd-Cluster-ID"); g != "1" {
  110. t.Errorf("cluster id = %s, want %s", g, "1")
  111. }
  112. b, err := ioutil.ReadAll(tr.Request().Body)
  113. if err != nil {
  114. t.Fatalf("unexpected ReadAll error: %v", err)
  115. }
  116. if string(b) != "some data" {
  117. t.Errorf("body = %s, want %s", b, "some data")
  118. }
  119. }
  120. func TestPipelinePostBad(t *testing.T) {
  121. tests := []struct {
  122. u string
  123. code int
  124. err error
  125. }{
  126. // RoundTrip returns error
  127. {"http://localhost:2380", 0, errors.New("blah")},
  128. // unexpected response status code
  129. {"http://localhost:2380", http.StatusOK, nil},
  130. {"http://localhost:2380", http.StatusCreated, nil},
  131. }
  132. for i, tt := range tests {
  133. picker := mustNewURLPicker(t, []string{tt.u})
  134. p := newPipeline(newRespRoundTripper(tt.code, tt.err), picker, types.ID(1), types.ID(1), nil, &fakeRaft{}, make(chan error))
  135. err := p.post([]byte("some data"))
  136. p.stop()
  137. if err == nil {
  138. t.Errorf("#%d: err = nil, want not nil", i)
  139. }
  140. }
  141. }
  142. func TestPipelinePostErrorc(t *testing.T) {
  143. tests := []struct {
  144. u string
  145. code int
  146. err error
  147. }{
  148. {"http://localhost:2380", http.StatusForbidden, nil},
  149. {"http://localhost:2380", http.StatusPreconditionFailed, nil},
  150. }
  151. for i, tt := range tests {
  152. picker := mustNewURLPicker(t, []string{tt.u})
  153. errorc := make(chan error, 1)
  154. p := newPipeline(newRespRoundTripper(tt.code, tt.err), picker, types.ID(1), types.ID(1), nil, &fakeRaft{}, errorc)
  155. p.post([]byte("some data"))
  156. p.stop()
  157. select {
  158. case <-errorc:
  159. default:
  160. t.Fatalf("#%d: cannot receive from errorc", i)
  161. }
  162. }
  163. }
  164. type roundTripperBlocker struct {
  165. c chan struct{}
  166. }
  167. func newRoundTripperBlocker() *roundTripperBlocker {
  168. return &roundTripperBlocker{c: make(chan struct{})}
  169. }
  170. func (t *roundTripperBlocker) RoundTrip(req *http.Request) (*http.Response, error) {
  171. <-t.c
  172. return &http.Response{StatusCode: http.StatusNoContent, Body: &nopReadCloser{}}, nil
  173. }
  174. func (t *roundTripperBlocker) unblock() {
  175. close(t.c)
  176. }
  177. type respRoundTripper struct {
  178. code int
  179. err error
  180. }
  181. func newRespRoundTripper(code int, err error) *respRoundTripper {
  182. return &respRoundTripper{code: code, err: err}
  183. }
  184. func (t *respRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
  185. return &http.Response{StatusCode: t.code, Body: &nopReadCloser{}}, t.err
  186. }
  187. type roundTripperRecorder struct {
  188. req *http.Request
  189. sync.Mutex
  190. }
  191. func (t *roundTripperRecorder) RoundTrip(req *http.Request) (*http.Response, error) {
  192. t.Lock()
  193. defer t.Unlock()
  194. t.req = req
  195. return &http.Response{StatusCode: http.StatusNoContent, Body: &nopReadCloser{}}, nil
  196. }
  197. func (t *roundTripperRecorder) Request() *http.Request {
  198. t.Lock()
  199. defer t.Unlock()
  200. return t.req
  201. }
  202. type nopReadCloser struct{}
  203. func (n *nopReadCloser) Read(p []byte) (int, error) { return 0, nil }
  204. func (n *nopReadCloser) Close() error { return nil }