urlpick_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2015 CoreOS, Inc.
  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 rafthttp
  15. import (
  16. "net/url"
  17. "testing"
  18. "github.com/coreos/etcd/pkg/testutil"
  19. )
  20. // TestURLPickerPickTwice tests that pick returns a possible url,
  21. // and always returns the same one.
  22. func TestURLPickerPick(t *testing.T) {
  23. picker := mustNewURLPicker(t, []string{"http://127.0.0.1:2380", "http://127.0.0.1:7001"})
  24. u := picker.pick()
  25. urlmap := map[url.URL]bool{
  26. url.URL{Scheme: "http", Host: "127.0.0.1:2380"}: true,
  27. url.URL{Scheme: "http", Host: "127.0.0.1:7001"}: true,
  28. }
  29. if !urlmap[u] {
  30. t.Errorf("url picked = %+v, want a possible url in %+v", u, urlmap)
  31. }
  32. // pick out the same url when calling pick again
  33. uu := picker.pick()
  34. if u != uu {
  35. t.Errorf("url picked = %+v, want %+v", uu, u)
  36. }
  37. }
  38. func TestURLPickerUpdate(t *testing.T) {
  39. picker := mustNewURLPicker(t, []string{"http://127.0.0.1:2380", "http://127.0.0.1:7001"})
  40. picker.update(testutil.MustNewURLs(t, []string{"http://localhost:2380", "http://localhost:7001"}))
  41. u := picker.pick()
  42. urlmap := map[url.URL]bool{
  43. url.URL{Scheme: "http", Host: "localhost:2380"}: true,
  44. url.URL{Scheme: "http", Host: "localhost:7001"}: true,
  45. }
  46. if !urlmap[u] {
  47. t.Errorf("url picked = %+v, want a possible url in %+v", u, urlmap)
  48. }
  49. }
  50. func TestURLPickerUnreachable(t *testing.T) {
  51. picker := mustNewURLPicker(t, []string{"http://127.0.0.1:2380", "http://127.0.0.1:7001"})
  52. u := picker.pick()
  53. picker.unreachable(u)
  54. uu := picker.pick()
  55. if u == uu {
  56. t.Errorf("url picked = %+v, want other possible urls", uu)
  57. }
  58. }
  59. func mustNewURLPicker(t *testing.T, us []string) *urlPicker {
  60. urls := testutil.MustNewURLs(t, us)
  61. return newURLPicker(urls)
  62. }