policies_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) 2015 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import "testing"
  6. // Tests of the round-robin host selection policy implementation
  7. func TestRoundRobinHostPolicy(t *testing.T) {
  8. policy := NewRoundRobinHostPolicy()
  9. hosts := []HostInfo{
  10. HostInfo{HostId: "0"},
  11. HostInfo{HostId: "1"},
  12. }
  13. policy.SetHosts(hosts)
  14. // the first host selected is actually at [1], but this is ok for RR
  15. // interleaved iteration should always increment the host
  16. iterA := policy.Pick(nil)
  17. if actual := iterA(); actual != &hosts[1] {
  18. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.HostId)
  19. }
  20. iterB := policy.Pick(nil)
  21. if actual := iterB(); actual != &hosts[0] {
  22. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.HostId)
  23. }
  24. if actual := iterB(); actual != &hosts[1] {
  25. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.HostId)
  26. }
  27. if actual := iterA(); actual != &hosts[0] {
  28. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.HostId)
  29. }
  30. iterC := policy.Pick(nil)
  31. if actual := iterC(); actual != &hosts[1] {
  32. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.HostId)
  33. }
  34. if actual := iterC(); actual != &hosts[0] {
  35. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.HostId)
  36. }
  37. }
  38. // Tests of the token-aware host selection policy implementation with a
  39. // round-robin host selection policy fallback.
  40. func TestTokenAwareHostPolicy(t *testing.T) {
  41. policy := NewTokenAwareHostPolicy(NewRoundRobinHostPolicy())
  42. query := &Query{}
  43. iter := policy.Pick(nil)
  44. if iter == nil {
  45. t.Fatal("host iterator was nil")
  46. }
  47. actual := iter()
  48. if actual != nil {
  49. t.Fatalf("expected nil from iterator, but was %v", actual)
  50. }
  51. // set the hosts
  52. hosts := []HostInfo{
  53. HostInfo{Peer: "0", Tokens: []string{"00"}},
  54. HostInfo{Peer: "1", Tokens: []string{"25"}},
  55. HostInfo{Peer: "2", Tokens: []string{"50"}},
  56. HostInfo{Peer: "3", Tokens: []string{"75"}},
  57. }
  58. policy.SetHosts(hosts)
  59. // the token ring is not setup without the partitioner, but the fallback
  60. // should work
  61. if actual := policy.Pick(nil)(); actual.Peer != "1" {
  62. t.Errorf("Expected peer 1 but was %s", actual.Peer)
  63. }
  64. query.RoutingKey([]byte("30"))
  65. if actual := policy.Pick(query)(); actual.Peer != "2" {
  66. t.Errorf("Expected peer 2 but was %s", actual.Peer)
  67. }
  68. policy.SetPartitioner("OrderedPartitioner")
  69. // now the token ring is configured
  70. query.RoutingKey([]byte("20"))
  71. iter = policy.Pick(query)
  72. if actual := iter(); actual.Peer != "1" {
  73. t.Errorf("Expected peer 1 but was %s", actual.Peer)
  74. }
  75. // rest are round robin
  76. if actual := iter(); actual.Peer != "3" {
  77. t.Errorf("Expected peer 3 but was %s", actual.Peer)
  78. }
  79. if actual := iter(); actual.Peer != "0" {
  80. t.Errorf("Expected peer 0 but was %s", actual.Peer)
  81. }
  82. if actual := iter(); actual.Peer != "2" {
  83. t.Errorf("Expected peer 2 but was %s", actual.Peer)
  84. }
  85. }
  86. // Tests of the round-robin connection selection policy implementation
  87. func TestRoundRobinConnPolicy(t *testing.T) {
  88. policy := NewRoundRobinConnPolicy()
  89. conn0 := &Conn{}
  90. conn1 := &Conn{}
  91. conn := []*Conn{
  92. conn0,
  93. conn1,
  94. }
  95. policy.SetConns(conn)
  96. // the first conn selected is actually at [1], but this is ok for RR
  97. if actual := policy.Pick(nil); actual != conn1 {
  98. t.Error("Expected conn1")
  99. }
  100. if actual := policy.Pick(nil); actual != conn0 {
  101. t.Error("Expected conn0")
  102. }
  103. if actual := policy.Pick(nil); actual != conn1 {
  104. t.Error("Expected conn1")
  105. }
  106. }