peer_test.go 6.2 KB

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