client_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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 client
  15. import (
  16. "errors"
  17. "io"
  18. "io/ioutil"
  19. "math/rand"
  20. "net/http"
  21. "net/url"
  22. "reflect"
  23. "sort"
  24. "strings"
  25. "testing"
  26. "time"
  27. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  28. "github.com/coreos/etcd/pkg/testutil"
  29. )
  30. type actionAssertingHTTPClient struct {
  31. t *testing.T
  32. num int
  33. act httpAction
  34. resp http.Response
  35. body []byte
  36. err error
  37. }
  38. func (a *actionAssertingHTTPClient) Do(_ context.Context, act httpAction) (*http.Response, []byte, error) {
  39. if !reflect.DeepEqual(a.act, act) {
  40. a.t.Errorf("#%d: unexpected httpAction: want=%#v got=%#v", a.num, a.act, act)
  41. }
  42. return &a.resp, a.body, a.err
  43. }
  44. type staticHTTPClient struct {
  45. resp http.Response
  46. body []byte
  47. err error
  48. }
  49. func (s *staticHTTPClient) Do(context.Context, httpAction) (*http.Response, []byte, error) {
  50. return &s.resp, s.body, s.err
  51. }
  52. type staticHTTPAction struct {
  53. request http.Request
  54. }
  55. func (s *staticHTTPAction) HTTPRequest(url.URL) *http.Request {
  56. return &s.request
  57. }
  58. type staticHTTPResponse struct {
  59. resp http.Response
  60. body []byte
  61. err error
  62. }
  63. type multiStaticHTTPClient struct {
  64. responses []staticHTTPResponse
  65. cur int
  66. }
  67. func (s *multiStaticHTTPClient) Do(context.Context, httpAction) (*http.Response, []byte, error) {
  68. r := s.responses[s.cur]
  69. s.cur++
  70. return &r.resp, r.body, r.err
  71. }
  72. func newStaticHTTPClientFactory(responses []staticHTTPResponse) httpClientFactory {
  73. var cur int
  74. return func(url.URL) httpClient {
  75. r := responses[cur]
  76. cur++
  77. return &staticHTTPClient{resp: r.resp, body: r.body, err: r.err}
  78. }
  79. }
  80. type fakeTransport struct {
  81. respchan chan *http.Response
  82. errchan chan error
  83. startCancel chan struct{}
  84. finishCancel chan struct{}
  85. }
  86. func newFakeTransport() *fakeTransport {
  87. return &fakeTransport{
  88. respchan: make(chan *http.Response, 1),
  89. errchan: make(chan error, 1),
  90. startCancel: make(chan struct{}, 1),
  91. finishCancel: make(chan struct{}, 1),
  92. }
  93. }
  94. func (t *fakeTransport) RoundTrip(*http.Request) (*http.Response, error) {
  95. select {
  96. case resp := <-t.respchan:
  97. return resp, nil
  98. case err := <-t.errchan:
  99. return nil, err
  100. case <-t.startCancel:
  101. select {
  102. // this simulates that the request is finished before cancel effects
  103. case resp := <-t.respchan:
  104. return resp, nil
  105. // wait on finishCancel to simulate taking some amount of
  106. // time while calling CancelRequest
  107. case <-t.finishCancel:
  108. return nil, errors.New("cancelled")
  109. }
  110. }
  111. }
  112. func (t *fakeTransport) CancelRequest(*http.Request) {
  113. t.startCancel <- struct{}{}
  114. }
  115. type fakeAction struct{}
  116. func (a *fakeAction) HTTPRequest(url.URL) *http.Request {
  117. return &http.Request{}
  118. }
  119. func TestSimpleHTTPClientDoSuccess(t *testing.T) {
  120. tr := newFakeTransport()
  121. c := &simpleHTTPClient{transport: tr}
  122. tr.respchan <- &http.Response{
  123. StatusCode: http.StatusTeapot,
  124. Body: ioutil.NopCloser(strings.NewReader("foo")),
  125. }
  126. resp, body, err := c.Do(context.Background(), &fakeAction{})
  127. if err != nil {
  128. t.Fatalf("incorrect error value: want=nil got=%v", err)
  129. }
  130. wantCode := http.StatusTeapot
  131. if wantCode != resp.StatusCode {
  132. t.Fatalf("invalid response code: want=%d got=%d", wantCode, resp.StatusCode)
  133. }
  134. wantBody := []byte("foo")
  135. if !reflect.DeepEqual(wantBody, body) {
  136. t.Fatalf("invalid response body: want=%q got=%q", wantBody, body)
  137. }
  138. }
  139. func TestSimpleHTTPClientDoError(t *testing.T) {
  140. tr := newFakeTransport()
  141. c := &simpleHTTPClient{transport: tr}
  142. tr.errchan <- errors.New("fixture")
  143. _, _, err := c.Do(context.Background(), &fakeAction{})
  144. if err == nil {
  145. t.Fatalf("expected non-nil error, got nil")
  146. }
  147. }
  148. func TestSimpleHTTPClientDoCancelContext(t *testing.T) {
  149. tr := newFakeTransport()
  150. c := &simpleHTTPClient{transport: tr}
  151. tr.startCancel <- struct{}{}
  152. tr.finishCancel <- struct{}{}
  153. _, _, err := c.Do(context.Background(), &fakeAction{})
  154. if err == nil {
  155. t.Fatalf("expected non-nil error, got nil")
  156. }
  157. }
  158. type checkableReadCloser struct {
  159. io.ReadCloser
  160. closed bool
  161. }
  162. func (c *checkableReadCloser) Close() error {
  163. if !c.closed {
  164. c.closed = true
  165. return c.ReadCloser.Close()
  166. }
  167. return nil
  168. }
  169. func TestSimpleHTTPClientDoCancelContextResponseBodyClosed(t *testing.T) {
  170. tr := newFakeTransport()
  171. c := &simpleHTTPClient{transport: tr}
  172. // create an already-cancelled context
  173. ctx, cancel := context.WithCancel(context.Background())
  174. cancel()
  175. body := &checkableReadCloser{ReadCloser: ioutil.NopCloser(strings.NewReader("foo"))}
  176. go func() {
  177. // wait that simpleHTTPClient knows the context is already timed out,
  178. // and calls CancelRequest
  179. testutil.WaitSchedule()
  180. // response is returned before cancel effects
  181. tr.respchan <- &http.Response{Body: body}
  182. }()
  183. _, _, err := c.Do(ctx, &fakeAction{})
  184. if err == nil {
  185. t.Fatalf("expected non-nil error, got nil")
  186. }
  187. if !body.closed {
  188. t.Fatalf("expected closed body")
  189. }
  190. }
  191. type blockingBody struct {
  192. c chan struct{}
  193. }
  194. func (bb *blockingBody) Read(p []byte) (n int, err error) {
  195. <-bb.c
  196. return 0, errors.New("closed")
  197. }
  198. func (bb *blockingBody) Close() error {
  199. close(bb.c)
  200. return nil
  201. }
  202. func TestSimpleHTTPClientDoCancelContextResponseBodyClosedWithBlockingBody(t *testing.T) {
  203. tr := newFakeTransport()
  204. c := &simpleHTTPClient{transport: tr}
  205. ctx, cancel := context.WithCancel(context.Background())
  206. body := &checkableReadCloser{ReadCloser: &blockingBody{c: make(chan struct{})}}
  207. go func() {
  208. tr.respchan <- &http.Response{Body: body}
  209. time.Sleep(2 * time.Millisecond)
  210. // cancel after the body is received
  211. cancel()
  212. }()
  213. _, _, err := c.Do(ctx, &fakeAction{})
  214. if err != context.Canceled {
  215. t.Fatalf("expected %+v, got %+v", context.Canceled, err)
  216. }
  217. if !body.closed {
  218. t.Fatalf("expected closed body")
  219. }
  220. }
  221. func TestSimpleHTTPClientDoCancelContextWaitForRoundTrip(t *testing.T) {
  222. tr := newFakeTransport()
  223. c := &simpleHTTPClient{transport: tr}
  224. donechan := make(chan struct{})
  225. ctx, cancel := context.WithCancel(context.Background())
  226. go func() {
  227. c.Do(ctx, &fakeAction{})
  228. close(donechan)
  229. }()
  230. // This should call CancelRequest and begin the cancellation process
  231. cancel()
  232. select {
  233. case <-donechan:
  234. t.Fatalf("simpleHTTPClient.Do should not have exited yet")
  235. default:
  236. }
  237. tr.finishCancel <- struct{}{}
  238. select {
  239. case <-donechan:
  240. //expected behavior
  241. return
  242. case <-time.After(time.Second):
  243. t.Fatalf("simpleHTTPClient.Do did not exit within 1s")
  244. }
  245. }
  246. func TestHTTPClusterClientDo(t *testing.T) {
  247. fakeErr := errors.New("fake!")
  248. fakeURL := url.URL{}
  249. tests := []struct {
  250. client *httpClusterClient
  251. wantCode int
  252. wantErr error
  253. wantPinned int
  254. }{
  255. // first good response short-circuits Do
  256. {
  257. client: &httpClusterClient{
  258. endpoints: []url.URL{fakeURL, fakeURL},
  259. clientFactory: newStaticHTTPClientFactory(
  260. []staticHTTPResponse{
  261. staticHTTPResponse{resp: http.Response{StatusCode: http.StatusTeapot}},
  262. staticHTTPResponse{err: fakeErr},
  263. },
  264. ),
  265. rand: rand.New(rand.NewSource(0)),
  266. },
  267. wantCode: http.StatusTeapot,
  268. },
  269. // fall through to good endpoint if err is arbitrary
  270. {
  271. client: &httpClusterClient{
  272. endpoints: []url.URL{fakeURL, fakeURL},
  273. clientFactory: newStaticHTTPClientFactory(
  274. []staticHTTPResponse{
  275. staticHTTPResponse{err: fakeErr},
  276. staticHTTPResponse{resp: http.Response{StatusCode: http.StatusTeapot}},
  277. },
  278. ),
  279. rand: rand.New(rand.NewSource(0)),
  280. },
  281. wantCode: http.StatusTeapot,
  282. wantPinned: 1,
  283. },
  284. // context.DeadlineExceeded short-circuits Do
  285. {
  286. client: &httpClusterClient{
  287. endpoints: []url.URL{fakeURL, fakeURL},
  288. clientFactory: newStaticHTTPClientFactory(
  289. []staticHTTPResponse{
  290. staticHTTPResponse{err: context.DeadlineExceeded},
  291. staticHTTPResponse{resp: http.Response{StatusCode: http.StatusTeapot}},
  292. },
  293. ),
  294. rand: rand.New(rand.NewSource(0)),
  295. },
  296. wantErr: &ClusterError{Errors: []error{context.DeadlineExceeded}},
  297. },
  298. // context.Canceled short-circuits Do
  299. {
  300. client: &httpClusterClient{
  301. endpoints: []url.URL{fakeURL, fakeURL},
  302. clientFactory: newStaticHTTPClientFactory(
  303. []staticHTTPResponse{
  304. staticHTTPResponse{err: context.Canceled},
  305. staticHTTPResponse{resp: http.Response{StatusCode: http.StatusTeapot}},
  306. },
  307. ),
  308. rand: rand.New(rand.NewSource(0)),
  309. },
  310. wantErr: context.Canceled,
  311. },
  312. // return err if there are no endpoints
  313. {
  314. client: &httpClusterClient{
  315. endpoints: []url.URL{},
  316. clientFactory: newHTTPClientFactory(nil, nil),
  317. rand: rand.New(rand.NewSource(0)),
  318. },
  319. wantErr: ErrNoEndpoints,
  320. },
  321. // return err if all endpoints return arbitrary errors
  322. {
  323. client: &httpClusterClient{
  324. endpoints: []url.URL{fakeURL, fakeURL},
  325. clientFactory: newStaticHTTPClientFactory(
  326. []staticHTTPResponse{
  327. staticHTTPResponse{err: fakeErr},
  328. staticHTTPResponse{err: fakeErr},
  329. },
  330. ),
  331. rand: rand.New(rand.NewSource(0)),
  332. },
  333. wantErr: &ClusterError{Errors: []error{fakeErr, fakeErr}},
  334. },
  335. // 500-level errors cause Do to fallthrough to next endpoint
  336. {
  337. client: &httpClusterClient{
  338. endpoints: []url.URL{fakeURL, fakeURL},
  339. clientFactory: newStaticHTTPClientFactory(
  340. []staticHTTPResponse{
  341. staticHTTPResponse{resp: http.Response{StatusCode: http.StatusBadGateway}},
  342. staticHTTPResponse{resp: http.Response{StatusCode: http.StatusTeapot}},
  343. },
  344. ),
  345. rand: rand.New(rand.NewSource(0)),
  346. },
  347. wantCode: http.StatusTeapot,
  348. wantPinned: 1,
  349. },
  350. }
  351. for i, tt := range tests {
  352. resp, _, err := tt.client.Do(context.Background(), nil)
  353. if !reflect.DeepEqual(tt.wantErr, err) {
  354. t.Errorf("#%d: got err=%v, want=%v", i, err, tt.wantErr)
  355. continue
  356. }
  357. if resp == nil {
  358. if tt.wantCode != 0 {
  359. t.Errorf("#%d: resp is nil, want=%d", i, tt.wantCode)
  360. }
  361. continue
  362. }
  363. if resp.StatusCode != tt.wantCode {
  364. t.Errorf("#%d: resp code=%d, want=%d", i, resp.StatusCode, tt.wantCode)
  365. continue
  366. }
  367. if tt.client.pinned != tt.wantPinned {
  368. t.Errorf("#%d: pinned=%d, want=%d", i, tt.client.pinned, tt.wantPinned)
  369. }
  370. }
  371. }
  372. func TestRedirectedHTTPAction(t *testing.T) {
  373. act := &redirectedHTTPAction{
  374. action: &staticHTTPAction{
  375. request: http.Request{
  376. Method: "DELETE",
  377. URL: &url.URL{
  378. Scheme: "https",
  379. Host: "foo.example.com",
  380. Path: "/ping",
  381. },
  382. },
  383. },
  384. location: url.URL{
  385. Scheme: "https",
  386. Host: "bar.example.com",
  387. Path: "/pong",
  388. },
  389. }
  390. want := &http.Request{
  391. Method: "DELETE",
  392. URL: &url.URL{
  393. Scheme: "https",
  394. Host: "bar.example.com",
  395. Path: "/pong",
  396. },
  397. }
  398. got := act.HTTPRequest(url.URL{Scheme: "http", Host: "baz.example.com", Path: "/pang"})
  399. if !reflect.DeepEqual(want, got) {
  400. t.Fatalf("HTTPRequest is %#v, want %#v", want, got)
  401. }
  402. }
  403. func TestRedirectFollowingHTTPClient(t *testing.T) {
  404. tests := []struct {
  405. checkRedirect CheckRedirectFunc
  406. client httpClient
  407. wantCode int
  408. wantErr error
  409. }{
  410. // errors bubbled up
  411. {
  412. checkRedirect: func(int) error { return ErrTooManyRedirects },
  413. client: &multiStaticHTTPClient{
  414. responses: []staticHTTPResponse{
  415. staticHTTPResponse{
  416. err: errors.New("fail!"),
  417. },
  418. },
  419. },
  420. wantErr: errors.New("fail!"),
  421. },
  422. // no need to follow redirect if none given
  423. {
  424. checkRedirect: func(int) error { return ErrTooManyRedirects },
  425. client: &multiStaticHTTPClient{
  426. responses: []staticHTTPResponse{
  427. staticHTTPResponse{
  428. resp: http.Response{
  429. StatusCode: http.StatusTeapot,
  430. },
  431. },
  432. },
  433. },
  434. wantCode: http.StatusTeapot,
  435. },
  436. // redirects if less than max
  437. {
  438. checkRedirect: func(via int) error {
  439. if via >= 2 {
  440. return ErrTooManyRedirects
  441. }
  442. return nil
  443. },
  444. client: &multiStaticHTTPClient{
  445. responses: []staticHTTPResponse{
  446. staticHTTPResponse{
  447. resp: http.Response{
  448. StatusCode: http.StatusTemporaryRedirect,
  449. Header: http.Header{"Location": []string{"http://example.com"}},
  450. },
  451. },
  452. staticHTTPResponse{
  453. resp: http.Response{
  454. StatusCode: http.StatusTeapot,
  455. },
  456. },
  457. },
  458. },
  459. wantCode: http.StatusTeapot,
  460. },
  461. // succeed after reaching max redirects
  462. {
  463. checkRedirect: func(via int) error {
  464. if via >= 3 {
  465. return ErrTooManyRedirects
  466. }
  467. return nil
  468. },
  469. client: &multiStaticHTTPClient{
  470. responses: []staticHTTPResponse{
  471. staticHTTPResponse{
  472. resp: http.Response{
  473. StatusCode: http.StatusTemporaryRedirect,
  474. Header: http.Header{"Location": []string{"http://example.com"}},
  475. },
  476. },
  477. staticHTTPResponse{
  478. resp: http.Response{
  479. StatusCode: http.StatusTemporaryRedirect,
  480. Header: http.Header{"Location": []string{"http://example.com"}},
  481. },
  482. },
  483. staticHTTPResponse{
  484. resp: http.Response{
  485. StatusCode: http.StatusTeapot,
  486. },
  487. },
  488. },
  489. },
  490. wantCode: http.StatusTeapot,
  491. },
  492. // fail if too many redirects
  493. {
  494. checkRedirect: func(via int) error {
  495. if via >= 2 {
  496. return ErrTooManyRedirects
  497. }
  498. return nil
  499. },
  500. client: &multiStaticHTTPClient{
  501. responses: []staticHTTPResponse{
  502. staticHTTPResponse{
  503. resp: http.Response{
  504. StatusCode: http.StatusTemporaryRedirect,
  505. Header: http.Header{"Location": []string{"http://example.com"}},
  506. },
  507. },
  508. staticHTTPResponse{
  509. resp: http.Response{
  510. StatusCode: http.StatusTemporaryRedirect,
  511. Header: http.Header{"Location": []string{"http://example.com"}},
  512. },
  513. },
  514. staticHTTPResponse{
  515. resp: http.Response{
  516. StatusCode: http.StatusTeapot,
  517. },
  518. },
  519. },
  520. },
  521. wantErr: ErrTooManyRedirects,
  522. },
  523. // fail if Location header not set
  524. {
  525. checkRedirect: func(int) error { return ErrTooManyRedirects },
  526. client: &multiStaticHTTPClient{
  527. responses: []staticHTTPResponse{
  528. staticHTTPResponse{
  529. resp: http.Response{
  530. StatusCode: http.StatusTemporaryRedirect,
  531. },
  532. },
  533. },
  534. },
  535. wantErr: errors.New("Location header not set"),
  536. },
  537. // fail if Location header is invalid
  538. {
  539. checkRedirect: func(int) error { return ErrTooManyRedirects },
  540. client: &multiStaticHTTPClient{
  541. responses: []staticHTTPResponse{
  542. staticHTTPResponse{
  543. resp: http.Response{
  544. StatusCode: http.StatusTemporaryRedirect,
  545. Header: http.Header{"Location": []string{":"}},
  546. },
  547. },
  548. },
  549. },
  550. wantErr: errors.New("Location header not valid URL: :"),
  551. },
  552. // fail if redirects checked way too many times
  553. {
  554. checkRedirect: func(int) error { return nil },
  555. client: &staticHTTPClient{
  556. resp: http.Response{
  557. StatusCode: http.StatusTemporaryRedirect,
  558. Header: http.Header{"Location": []string{"http://example.com"}},
  559. },
  560. },
  561. wantErr: errTooManyRedirectChecks,
  562. },
  563. }
  564. for i, tt := range tests {
  565. client := &redirectFollowingHTTPClient{client: tt.client, checkRedirect: tt.checkRedirect}
  566. resp, _, err := client.Do(context.Background(), nil)
  567. if !reflect.DeepEqual(tt.wantErr, err) {
  568. t.Errorf("#%d: got err=%v, want=%v", i, err, tt.wantErr)
  569. continue
  570. }
  571. if resp == nil {
  572. if tt.wantCode != 0 {
  573. t.Errorf("#%d: resp is nil, want=%d", i, tt.wantCode)
  574. }
  575. continue
  576. }
  577. if resp.StatusCode != tt.wantCode {
  578. t.Errorf("#%d: resp code=%d, want=%d", i, resp.StatusCode, tt.wantCode)
  579. continue
  580. }
  581. }
  582. }
  583. func TestDefaultCheckRedirect(t *testing.T) {
  584. tests := []struct {
  585. num int
  586. err error
  587. }{
  588. {0, nil},
  589. {5, nil},
  590. {10, nil},
  591. {11, ErrTooManyRedirects},
  592. {29, ErrTooManyRedirects},
  593. }
  594. for i, tt := range tests {
  595. err := DefaultCheckRedirect(tt.num)
  596. if !reflect.DeepEqual(tt.err, err) {
  597. t.Errorf("#%d: want=%#v got=%#v", i, tt.err, err)
  598. }
  599. }
  600. }
  601. func TestHTTPClusterClientSync(t *testing.T) {
  602. cf := newStaticHTTPClientFactory([]staticHTTPResponse{
  603. staticHTTPResponse{
  604. resp: http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": []string{"application/json"}}},
  605. body: []byte(`{"members":[{"id":"2745e2525fce8fe","peerURLs":["http://127.0.0.1:7003"],"name":"node3","clientURLs":["http://127.0.0.1:4003"]},{"id":"42134f434382925","peerURLs":["http://127.0.0.1:2380","http://127.0.0.1:7001"],"name":"node1","clientURLs":["http://127.0.0.1:2379","http://127.0.0.1:4001"]},{"id":"94088180e21eb87b","peerURLs":["http://127.0.0.1:7002"],"name":"node2","clientURLs":["http://127.0.0.1:4002"]}]}`),
  606. },
  607. })
  608. hc := &httpClusterClient{
  609. clientFactory: cf,
  610. rand: rand.New(rand.NewSource(0)),
  611. }
  612. err := hc.reset([]string{"http://127.0.0.1:2379"})
  613. if err != nil {
  614. t.Fatalf("unexpected error during setup: %#v", err)
  615. }
  616. want := []string{"http://127.0.0.1:2379"}
  617. got := hc.Endpoints()
  618. if !reflect.DeepEqual(want, got) {
  619. t.Fatalf("incorrect endpoints: want=%#v got=%#v", want, got)
  620. }
  621. err = hc.Sync(context.Background())
  622. if err != nil {
  623. t.Fatalf("unexpected error during Sync: %#v", err)
  624. }
  625. want = []string{"http://127.0.0.1:2379", "http://127.0.0.1:4001", "http://127.0.0.1:4002", "http://127.0.0.1:4003"}
  626. got = hc.Endpoints()
  627. sort.Sort(sort.StringSlice(got))
  628. if !reflect.DeepEqual(want, got) {
  629. t.Fatalf("incorrect endpoints post-Sync: want=%#v got=%#v", want, got)
  630. }
  631. err = hc.reset([]string{"http://127.0.0.1:4009"})
  632. if err != nil {
  633. t.Fatalf("unexpected error during reset: %#v", err)
  634. }
  635. want = []string{"http://127.0.0.1:4009"}
  636. got = hc.Endpoints()
  637. if !reflect.DeepEqual(want, got) {
  638. t.Fatalf("incorrect endpoints post-reset: want=%#v got=%#v", want, got)
  639. }
  640. }
  641. func TestHTTPClusterClientSyncFail(t *testing.T) {
  642. cf := newStaticHTTPClientFactory([]staticHTTPResponse{
  643. staticHTTPResponse{err: errors.New("fail!")},
  644. })
  645. hc := &httpClusterClient{
  646. clientFactory: cf,
  647. rand: rand.New(rand.NewSource(0)),
  648. }
  649. err := hc.reset([]string{"http://127.0.0.1:2379"})
  650. if err != nil {
  651. t.Fatalf("unexpected error during setup: %#v", err)
  652. }
  653. want := []string{"http://127.0.0.1:2379"}
  654. got := hc.Endpoints()
  655. if !reflect.DeepEqual(want, got) {
  656. t.Fatalf("incorrect endpoints: want=%#v got=%#v", want, got)
  657. }
  658. err = hc.Sync(context.Background())
  659. if err == nil {
  660. t.Fatalf("got nil error during Sync")
  661. }
  662. got = hc.Endpoints()
  663. if !reflect.DeepEqual(want, got) {
  664. t.Fatalf("incorrect endpoints after failed Sync: want=%#v got=%#v", want, got)
  665. }
  666. }
  667. func TestHTTPClusterClientAutoSyncCancelContext(t *testing.T) {
  668. cf := newStaticHTTPClientFactory([]staticHTTPResponse{
  669. staticHTTPResponse{
  670. resp: http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": []string{"application/json"}}},
  671. body: []byte(`{"members":[{"id":"2745e2525fce8fe","peerURLs":["http://127.0.0.1:7003"],"name":"node3","clientURLs":["http://127.0.0.1:4003"]},{"id":"42134f434382925","peerURLs":["http://127.0.0.1:2380","http://127.0.0.1:7001"],"name":"node1","clientURLs":["http://127.0.0.1:2379","http://127.0.0.1:4001"]},{"id":"94088180e21eb87b","peerURLs":["http://127.0.0.1:7002"],"name":"node2","clientURLs":["http://127.0.0.1:4002"]}]}`),
  672. },
  673. })
  674. hc := &httpClusterClient{
  675. clientFactory: cf,
  676. rand: rand.New(rand.NewSource(0)),
  677. }
  678. err := hc.reset([]string{"http://127.0.0.1:2379"})
  679. if err != nil {
  680. t.Fatalf("unexpected error during setup: %#v", err)
  681. }
  682. ctx, cancel := context.WithCancel(context.Background())
  683. cancel()
  684. err = hc.AutoSync(ctx, time.Hour)
  685. if err != context.Canceled {
  686. t.Fatalf("incorrect error value: want=%v got=%v", context.Canceled, err)
  687. }
  688. }
  689. func TestHTTPClusterClientAutoSyncFail(t *testing.T) {
  690. cf := newStaticHTTPClientFactory([]staticHTTPResponse{
  691. staticHTTPResponse{err: errors.New("fail!")},
  692. })
  693. hc := &httpClusterClient{
  694. clientFactory: cf,
  695. rand: rand.New(rand.NewSource(0)),
  696. }
  697. err := hc.reset([]string{"http://127.0.0.1:2379"})
  698. if err != nil {
  699. t.Fatalf("unexpected error during setup: %#v", err)
  700. }
  701. err = hc.AutoSync(context.Background(), time.Hour)
  702. if err.Error() != ErrClusterUnavailable.Error() {
  703. t.Fatalf("incorrect error value: want=%v got=%v", ErrClusterUnavailable, err)
  704. }
  705. }
  706. func TestHTTPClusterClientResetFail(t *testing.T) {
  707. tests := [][]string{
  708. // need at least one endpoint
  709. []string{},
  710. // urls must be valid
  711. []string{":"},
  712. }
  713. for i, tt := range tests {
  714. hc := &httpClusterClient{rand: rand.New(rand.NewSource(0))}
  715. err := hc.reset(tt)
  716. if err == nil {
  717. t.Errorf("#%d: expected non-nil error", i)
  718. }
  719. }
  720. }
  721. func TestHTTPClusterClientResetPinRandom(t *testing.T) {
  722. round := 2000
  723. pinNum := 0
  724. for i := 0; i < round; i++ {
  725. hc := &httpClusterClient{rand: rand.New(rand.NewSource(int64(i)))}
  726. err := hc.reset([]string{"http://127.0.0.1:4001", "http://127.0.0.1:4002", "http://127.0.0.1:4003"})
  727. if err != nil {
  728. t.Fatalf("#%d: reset error (%v)", i, err)
  729. }
  730. if hc.endpoints[hc.pinned].String() == "http://127.0.0.1:4001" {
  731. pinNum++
  732. }
  733. }
  734. min := 1.0/3.0 - 0.05
  735. max := 1.0/3.0 + 0.05
  736. if ratio := float64(pinNum) / float64(round); ratio > max || ratio < min {
  737. t.Errorf("pinned ratio = %v, want [%v, %v]", ratio, min, max)
  738. }
  739. }