util_test.go 3.4 KB

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