peers_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package etcdhttp
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "reflect"
  6. "sort"
  7. "testing"
  8. )
  9. func TestPeers(t *testing.T) {
  10. tests := []struct {
  11. in string
  12. wids []int64
  13. wep []string
  14. wstring string
  15. }{
  16. {
  17. "1=1.1.1.1",
  18. []int64{1},
  19. []string{"http://1.1.1.1"},
  20. "1=1.1.1.1",
  21. },
  22. {
  23. "2=2.2.2.2",
  24. []int64{2},
  25. []string{"http://2.2.2.2"},
  26. "2=2.2.2.2",
  27. },
  28. {
  29. "1=1.1.1.1&1=1.1.1.2&2=2.2.2.2",
  30. []int64{1, 2},
  31. []string{"http://1.1.1.1", "http://1.1.1.2", "http://2.2.2.2"},
  32. "1=1.1.1.1&1=1.1.1.2&2=2.2.2.2",
  33. },
  34. {
  35. "3=3.3.3.3&4=4.4.4.4&1=1.1.1.1&1=1.1.1.2&2=2.2.2.2",
  36. []int64{1, 2, 3, 4},
  37. []string{"http://1.1.1.1", "http://1.1.1.2", "http://2.2.2.2",
  38. "http://3.3.3.3", "http://4.4.4.4"},
  39. "1=1.1.1.1&1=1.1.1.2&2=2.2.2.2&3=3.3.3.3&4=4.4.4.4",
  40. },
  41. }
  42. for i, tt := range tests {
  43. p := &Peers{}
  44. err := p.Set(tt.in)
  45. if err != nil {
  46. t.Errorf("#%d: err=%v, want nil", i, err)
  47. }
  48. ids := int64Slice(p.IDs())
  49. sort.Sort(ids)
  50. if !reflect.DeepEqual([]int64(ids), tt.wids) {
  51. t.Errorf("#%d: IDs=%#v, want %#v", i, []int64(ids), tt.wids)
  52. }
  53. ep := p.Endpoints()
  54. if !reflect.DeepEqual(ep, tt.wep) {
  55. t.Errorf("#%d: Endpoints=%#v, want %#v", i, ep, tt.wep)
  56. }
  57. s := p.String()
  58. if s != tt.wstring {
  59. t.Errorf("#%d: string=%q, want %q", i, s, tt.wstring)
  60. }
  61. }
  62. }
  63. type int64Slice []int64
  64. func (p int64Slice) Len() int { return len(p) }
  65. func (p int64Slice) Less(i, j int) bool { return p[i] < p[j] }
  66. func (p int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  67. func TestPeersSetBad(t *testing.T) {
  68. tests := []string{
  69. // garbage URL
  70. "asdf%%",
  71. // non-int64 keys
  72. "a=1.2.3.4",
  73. "-1-23=1.2.3.4",
  74. }
  75. for i, tt := range tests {
  76. p := &Peers{}
  77. if err := p.Set(tt); err == nil {
  78. t.Errorf("#%d: err=nil unexpectedly", i)
  79. }
  80. }
  81. }
  82. func TestPeersPick(t *testing.T) {
  83. ps := &Peers{
  84. 1: []string{"abc", "def", "ghi", "jkl", "mno", "pqr", "stu"},
  85. 2: []string{"xyz"},
  86. 3: []string{},
  87. }
  88. ids := map[string]bool{
  89. "http://abc": true,
  90. "http://def": true,
  91. "http://ghi": true,
  92. "http://jkl": true,
  93. "http://mno": true,
  94. "http://pqr": true,
  95. "http://stu": true,
  96. }
  97. for i := 0; i < 1000; i++ {
  98. a := ps.Pick(1)
  99. if _, ok := ids[a]; !ok {
  100. t.Errorf("returned ID %q not in expected range!", a)
  101. break
  102. }
  103. }
  104. if b := ps.Pick(2); b != "http://xyz" {
  105. t.Errorf("id=%q, want %q", b, "http://xyz")
  106. }
  107. if c := ps.Pick(3); c != "" {
  108. t.Errorf("id=%q, want \"\"", c)
  109. }
  110. }
  111. func TestHttpPost(t *testing.T) {
  112. var tr *http.Request
  113. tests := []struct {
  114. h http.HandlerFunc
  115. w bool
  116. }{
  117. {
  118. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  119. tr = r
  120. w.WriteHeader(200)
  121. }),
  122. true,
  123. },
  124. {
  125. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  126. tr = r
  127. w.WriteHeader(404)
  128. }),
  129. false,
  130. },
  131. }
  132. for i, tt := range tests {
  133. ts := httptest.NewServer(tt.h)
  134. if g := httpPost(ts.URL, []byte("adsf")); g != tt.w {
  135. t.Errorf("#%d: httpPost()=%t, want %t", i, g, tt.w)
  136. }
  137. if tr.Method != "POST" {
  138. t.Errorf("#%d: Method=%q, want %q", i, tr.Method, "POST")
  139. }
  140. if ct := tr.Header.Get("Content-Type"); ct != "application/protobuf" {
  141. t.Errorf("%#d: Content-Type=%q, want %q", ct, "application/protobuf")
  142. }
  143. tr = nil
  144. ts.Close()
  145. }
  146. if httpPost("garbage url", []byte("data")) {
  147. t.Errorf("httpPost with bad URL returned true unexpectedly!")
  148. }
  149. }