util_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2016 The etcd Authors
  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 etcdserver
  15. import (
  16. "net/http"
  17. "testing"
  18. "time"
  19. "github.com/coreos/etcd/etcdserver/membership"
  20. "github.com/coreos/etcd/pkg/types"
  21. "github.com/coreos/etcd/raft/raftpb"
  22. "github.com/coreos/etcd/rafthttp"
  23. "github.com/coreos/etcd/snap"
  24. )
  25. func TestLongestConnected(t *testing.T) {
  26. umap, err := types.NewURLsMap("mem1=http://10.1:2379,mem2=http://10.2:2379,mem3=http://10.3:2379")
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. clus, err := membership.NewClusterFromURLsMap("test", umap)
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. memberIDs := clus.MemberIDs()
  35. tr := newNopTransporterWithActiveTime(memberIDs)
  36. transferee, ok := longestConnected(tr, memberIDs)
  37. if !ok {
  38. t.Fatalf("unexpected ok %v", ok)
  39. }
  40. if memberIDs[0] != transferee {
  41. t.Fatalf("expected first member %s to be transferee, got %s", memberIDs[0], transferee)
  42. }
  43. // make all members non-active
  44. amap := make(map[types.ID]time.Time)
  45. for _, id := range memberIDs {
  46. amap[id] = time.Time{}
  47. }
  48. tr.(*nopTransporterWithActiveTime).reset(amap)
  49. _, ok2 := longestConnected(tr, memberIDs)
  50. if ok2 {
  51. t.Fatalf("unexpected ok %v", ok)
  52. }
  53. }
  54. type nopTransporterWithActiveTime struct {
  55. activeMap map[types.ID]time.Time
  56. }
  57. // newNopTransporterWithActiveTime creates nopTransporterWithActiveTime with the first member
  58. // being the most stable (longest active-since time).
  59. func newNopTransporterWithActiveTime(memberIDs []types.ID) rafthttp.Transporter {
  60. am := make(map[types.ID]time.Time)
  61. for i, id := range memberIDs {
  62. am[id] = time.Now().Add(time.Duration(i) * time.Second)
  63. }
  64. return &nopTransporterWithActiveTime{activeMap: am}
  65. }
  66. func (s *nopTransporterWithActiveTime) Start() error { return nil }
  67. func (s *nopTransporterWithActiveTime) Handler() http.Handler { return nil }
  68. func (s *nopTransporterWithActiveTime) Send(m []raftpb.Message) {}
  69. func (s *nopTransporterWithActiveTime) SendSnapshot(m snap.Message) {}
  70. func (s *nopTransporterWithActiveTime) AddRemote(id types.ID, us []string) {}
  71. func (s *nopTransporterWithActiveTime) AddPeer(id types.ID, us []string) {}
  72. func (s *nopTransporterWithActiveTime) RemovePeer(id types.ID) {}
  73. func (s *nopTransporterWithActiveTime) RemoveAllPeers() {}
  74. func (s *nopTransporterWithActiveTime) UpdatePeer(id types.ID, us []string) {}
  75. func (s *nopTransporterWithActiveTime) ActiveSince(id types.ID) time.Time { return s.activeMap[id] }
  76. func (s *nopTransporterWithActiveTime) ActivePeers() int { return 0 }
  77. func (s *nopTransporterWithActiveTime) Stop() {}
  78. func (s *nopTransporterWithActiveTime) Pause() {}
  79. func (s *nopTransporterWithActiveTime) Resume() {}
  80. func (s *nopTransporterWithActiveTime) reset(am map[types.ID]time.Time) { s.activeMap = am }