policies_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 (
  6. "fmt"
  7. "github.com/gocql/gocql/internal/streams"
  8. "testing"
  9. "github.com/hailocab/go-hostpool"
  10. )
  11. // Tests of the round-robin host selection policy implementation
  12. func TestRoundRobinHostPolicy(t *testing.T) {
  13. policy := RoundRobinHostPolicy()
  14. hosts := []HostInfo{
  15. HostInfo{HostId: "0"},
  16. HostInfo{HostId: "1"},
  17. }
  18. policy.SetHosts(hosts)
  19. // the first host selected is actually at [1], but this is ok for RR
  20. // interleaved iteration should always increment the host
  21. iterA := policy.Pick(nil)
  22. if actual := iterA(); actual.Info() != &hosts[1] {
  23. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.Info().HostId)
  24. }
  25. iterB := policy.Pick(nil)
  26. if actual := iterB(); actual.Info() != &hosts[0] {
  27. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.Info().HostId)
  28. }
  29. if actual := iterB(); actual.Info() != &hosts[1] {
  30. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.Info().HostId)
  31. }
  32. if actual := iterA(); actual.Info() != &hosts[0] {
  33. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.Info().HostId)
  34. }
  35. iterC := policy.Pick(nil)
  36. if actual := iterC(); actual.Info() != &hosts[1] {
  37. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.Info().HostId)
  38. }
  39. if actual := iterC(); actual.Info() != &hosts[0] {
  40. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.Info().HostId)
  41. }
  42. }
  43. // Tests of the token-aware host selection policy implementation with a
  44. // round-robin host selection policy fallback.
  45. func TestTokenAwareHostPolicy(t *testing.T) {
  46. policy := TokenAwareHostPolicy(RoundRobinHostPolicy())
  47. query := &Query{}
  48. iter := policy.Pick(nil)
  49. if iter == nil {
  50. t.Fatal("host iterator was nil")
  51. }
  52. actual := iter()
  53. if actual != nil {
  54. t.Fatalf("expected nil from iterator, but was %v", actual)
  55. }
  56. // set the hosts
  57. hosts := []HostInfo{
  58. HostInfo{Peer: "0", Tokens: []string{"00"}},
  59. HostInfo{Peer: "1", Tokens: []string{"25"}},
  60. HostInfo{Peer: "2", Tokens: []string{"50"}},
  61. HostInfo{Peer: "3", Tokens: []string{"75"}},
  62. }
  63. policy.SetHosts(hosts)
  64. // the token ring is not setup without the partitioner, but the fallback
  65. // should work
  66. if actual := policy.Pick(nil)(); actual.Info().Peer != "1" {
  67. t.Errorf("Expected peer 1 but was %s", actual.Info().Peer)
  68. }
  69. query.RoutingKey([]byte("30"))
  70. if actual := policy.Pick(query)(); actual.Info().Peer != "2" {
  71. t.Errorf("Expected peer 2 but was %s", actual.Info().Peer)
  72. }
  73. policy.SetPartitioner("OrderedPartitioner")
  74. // now the token ring is configured
  75. query.RoutingKey([]byte("20"))
  76. iter = policy.Pick(query)
  77. if actual := iter(); actual.Info().Peer != "1" {
  78. t.Errorf("Expected peer 1 but was %s", actual.Info().Peer)
  79. }
  80. // rest are round robin
  81. if actual := iter(); actual.Info().Peer != "3" {
  82. t.Errorf("Expected peer 3 but was %s", actual.Info().Peer)
  83. }
  84. if actual := iter(); actual.Info().Peer != "0" {
  85. t.Errorf("Expected peer 0 but was %s", actual.Info().Peer)
  86. }
  87. if actual := iter(); actual.Info().Peer != "2" {
  88. t.Errorf("Expected peer 2 but was %s", actual.Info().Peer)
  89. }
  90. }
  91. // Tests of the host pool host selection policy implementation
  92. func TestHostPoolHostPolicy(t *testing.T) {
  93. policy := HostPoolHostPolicy(hostpool.New(nil))
  94. hosts := []HostInfo{
  95. HostInfo{HostId: "0", Peer: "0"},
  96. HostInfo{HostId: "1", Peer: "1"},
  97. }
  98. policy.SetHosts(hosts)
  99. // the first host selected is actually at [1], but this is ok for RR
  100. // interleaved iteration should always increment the host
  101. iter := policy.Pick(nil)
  102. actualA := iter()
  103. if actualA.Info().HostId != "0" {
  104. t.Errorf("Expected hosts[0] but was hosts[%s]", actualA.Info().HostId)
  105. }
  106. actualA.Mark(nil)
  107. actualB := iter()
  108. if actualB.Info().HostId != "1" {
  109. t.Errorf("Expected hosts[1] but was hosts[%s]", actualB.Info().HostId)
  110. }
  111. actualB.Mark(fmt.Errorf("error"))
  112. actualC := iter()
  113. if actualC.Info().HostId != "0" {
  114. t.Errorf("Expected hosts[0] but was hosts[%s]", actualC.Info().HostId)
  115. }
  116. actualC.Mark(nil)
  117. actualD := iter()
  118. if actualD.Info().HostId != "0" {
  119. t.Errorf("Expected hosts[0] but was hosts[%s]", actualD.Info().HostId)
  120. }
  121. actualD.Mark(nil)
  122. }
  123. // Tests of the round-robin connection selection policy implementation
  124. func TestRoundRobinConnPolicy(t *testing.T) {
  125. policy := RoundRobinConnPolicy()()
  126. conn0 := &Conn{streams: streams.New(1)}
  127. conn1 := &Conn{streams: streams.New(1)}
  128. conn := []*Conn{
  129. conn0,
  130. conn1,
  131. }
  132. policy.SetConns(conn)
  133. if actual := policy.Pick(nil); actual != conn0 {
  134. t.Error("Expected conn1")
  135. }
  136. if actual := policy.Pick(nil); actual != conn1 {
  137. t.Error("Expected conn0")
  138. }
  139. if actual := policy.Pick(nil); actual != conn0 {
  140. t.Error("Expected conn1")
  141. }
  142. }
  143. func TestRoundRobinNilHostInfo(t *testing.T) {
  144. policy := RoundRobinHostPolicy()
  145. host := HostInfo{HostId: "host-1"}
  146. policy.SetHosts([]HostInfo{host})
  147. iter := policy.Pick(nil)
  148. next := iter()
  149. if next == nil {
  150. t.Fatal("got nil host")
  151. } else if v := next.Info(); v == nil {
  152. t.Fatal("got nil HostInfo")
  153. } else if v.HostId != host.HostId {
  154. t.Fatalf("expected host %v got %v", host, *v)
  155. }
  156. next = iter()
  157. if next != nil {
  158. t.Errorf("expected to get nil host got %+v", next)
  159. if next.Info() == nil {
  160. t.Fatalf("HostInfo is nil")
  161. }
  162. }
  163. }