sender_test.go 6.0 KB

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