pipeline_test.go 6.8 KB

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