member_test.go 2.7 KB

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