member_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/url"
  16. "testing"
  17. "time"
  18. )
  19. func timeParse(value string) *time.Time {
  20. t, err := time.Parse(time.RFC3339, value)
  21. if err != nil {
  22. panic(err)
  23. }
  24. return &t
  25. }
  26. func TestMemberTime(t *testing.T) {
  27. tests := []struct {
  28. mem *Member
  29. id uint64
  30. }{
  31. {NewMember("mem1", []url.URL{{Scheme: "http", Host: "10.0.0.8:2379"}}, "", nil), 14544069596553697298},
  32. // Same ID, different name (names shouldn't matter)
  33. {NewMember("memfoo", []url.URL{{Scheme: "http", Host: "10.0.0.8:2379"}}, "", nil), 14544069596553697298},
  34. // Same ID, different Time
  35. {NewMember("mem1", []url.URL{{Scheme: "http", Host: "10.0.0.8:2379"}}, "", timeParse("1984-12-23T15:04:05Z")), 2448790162483548276},
  36. // Different cluster name
  37. {NewMember("mcm1", []url.URL{{Scheme: "http", Host: "10.0.0.8:2379"}}, "etcd", timeParse("1984-12-23T15:04:05Z")), 6973882743191604649},
  38. {NewMember("mem1", []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}}, "", timeParse("1984-12-23T15:04:05Z")), 1466075294948436910},
  39. // Order shouldn't matter
  40. {NewMember("mem1", []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}, {Scheme: "http", Host: "10.0.0.2:2379"}}, "", nil), 16552244735972308939},
  41. {NewMember("mem1", []url.URL{{Scheme: "http", Host: "10.0.0.2:2379"}, {Scheme: "http", Host: "10.0.0.1:2379"}}, "", nil), 16552244735972308939},
  42. }
  43. for i, tt := range tests {
  44. if tt.mem.ID != tt.id {
  45. t.Errorf("#%d: mem.ID = %v, want %v", i, tt.mem.ID, tt.id)
  46. }
  47. }
  48. }
  49. func TestMemberPick(t *testing.T) {
  50. tests := []struct {
  51. memb *Member
  52. urls map[string]bool
  53. }{
  54. {
  55. newTestMemberp(1, []string{"abc", "def", "ghi", "jkl", "mno", "pqr", "stu"}, "", nil),
  56. map[string]bool{
  57. "abc": true,
  58. "def": true,
  59. "ghi": true,
  60. "jkl": true,
  61. "mno": true,
  62. "pqr": true,
  63. "stu": true,
  64. },
  65. },
  66. {
  67. newTestMemberp(2, []string{"xyz"}, "", nil),
  68. map[string]bool{"xyz": true},
  69. },
  70. }
  71. for i, tt := range tests {
  72. for j := 0; j < 1000; j++ {
  73. a := tt.memb.PickPeerURL()
  74. if !tt.urls[a] {
  75. t.Errorf("#%d: returned ID %q not in expected range!", i, a)
  76. break
  77. }
  78. }
  79. }
  80. }