sendhub_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 etcdserver
  14. import (
  15. "net/http"
  16. "testing"
  17. "time"
  18. "github.com/coreos/etcd/etcdserver/stats"
  19. "github.com/coreos/etcd/pkg/testutil"
  20. "github.com/coreos/etcd/pkg/types"
  21. "github.com/coreos/etcd/raft/raftpb"
  22. )
  23. func TestSendHubAdd(t *testing.T) {
  24. cl := newTestCluster(nil)
  25. ls := stats.NewLeaderStats("")
  26. h := newSendHub(nil, cl, nil, nil, ls)
  27. m := newTestMember(1, []string{"http://a"}, "", nil)
  28. h.Add(m)
  29. if _, ok := ls.Followers["1"]; !ok {
  30. t.Errorf("FollowerStats[1] is nil, want exists")
  31. }
  32. s, ok := h.senders[types.ID(1)]
  33. if !ok {
  34. t.Fatalf("senders[1] is nil, want exists")
  35. }
  36. h.Add(m)
  37. ns := h.senders[types.ID(1)]
  38. if s != ns {
  39. t.Errorf("sender = %p, want %p", ns, s)
  40. }
  41. }
  42. func TestSendHubRemove(t *testing.T) {
  43. cl := newTestCluster(nil)
  44. ls := stats.NewLeaderStats("")
  45. h := newSendHub(nil, cl, nil, nil, ls)
  46. m := newTestMember(1, []string{"http://a"}, "", nil)
  47. h.Add(m)
  48. h.Remove(types.ID(1))
  49. if _, ok := h.senders[types.ID(1)]; ok {
  50. t.Fatalf("senders[1] exists, want removed")
  51. }
  52. }
  53. func TestSendHubShouldStop(t *testing.T) {
  54. tr := newRespRoundTripper(http.StatusForbidden, nil)
  55. cl := newTestCluster(nil)
  56. ls := stats.NewLeaderStats("")
  57. h := newSendHub(tr, cl, nil, nil, ls)
  58. m := newTestMember(1, []string{"http://a"}, "", nil)
  59. h.Add(m)
  60. shouldstop := h.ShouldStopNotify()
  61. select {
  62. case <-shouldstop:
  63. t.Fatalf("received unexpected shouldstop notification")
  64. case <-time.After(10 * time.Millisecond):
  65. }
  66. h.senders[1].Send(raftpb.Message{})
  67. testutil.ForceGosched()
  68. select {
  69. case <-shouldstop:
  70. default:
  71. t.Fatalf("cannot receive stop notification")
  72. }
  73. }
  74. type respRoundTripper struct {
  75. code int
  76. err error
  77. }
  78. func newRespRoundTripper(code int, err error) *respRoundTripper {
  79. return &respRoundTripper{code: code, err: err}
  80. }
  81. func (t *respRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
  82. return &http.Response{StatusCode: t.code, Body: &nopReadCloser{}}, t.err
  83. }
  84. type nopReadCloser struct{}
  85. func (n *nopReadCloser) Read(p []byte) (int, error) { return 0, nil }
  86. func (n *nopReadCloser) Close() error { return nil }