policies_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. func TestRoundRobinHostPolicy(t *testing.T) {
  7. policy := NewRoundRobinHostPolicy()
  8. hosts := []HostInfo{
  9. HostInfo{HostId: "0"},
  10. HostInfo{HostId: "1"},
  11. }
  12. policy.SetHosts(hosts)
  13. // the first host selected is actually at [1], but this is ok for RR
  14. iter := policy.Pick(nil)
  15. if actual := iter(); actual != &hosts[1] {
  16. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.HostId)
  17. }
  18. if actual := iter(); actual != &hosts[0] {
  19. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.HostId)
  20. }
  21. iter = policy.Pick(nil)
  22. if actual := iter(); actual != &hosts[0] {
  23. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.HostId)
  24. }
  25. if actual := iter(); actual != &hosts[1] {
  26. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.HostId)
  27. }
  28. iter = policy.Pick(nil)
  29. if actual := iter(); actual != &hosts[1] {
  30. t.Errorf("Expected hosts[0] but was hosts[%s]", actual.HostId)
  31. }
  32. if actual := iter(); actual != &hosts[0] {
  33. t.Errorf("Expected hosts[1] but was hosts[%s]", actual.HostId)
  34. }
  35. }
  36. func TestTokenAwareHostPolicy(t *testing.T) {
  37. policy := NewTokenAwareHostPolicy(NewRoundRobinHostPolicy())
  38. hosts := []HostInfo{
  39. HostInfo{HostId: "0", Peer: "0", Tokens: []string{"00"}},
  40. HostInfo{HostId: "1", Peer: "1", Tokens: []string{"25"}},
  41. HostInfo{HostId: "2", Peer: "2", Tokens: []string{"50"}},
  42. HostInfo{HostId: "3", Peer: "3", Tokens: []string{"75"}},
  43. }
  44. policy.SetHosts(hosts)
  45. policy.SetPartitioner("OrderedPartitioner")
  46. query := &Query{}
  47. query.RoutingKey([]byte("30"))
  48. if actual := policy.Pick(query)(); actual != &hosts[2] {
  49. t.Errorf("Expected hosts[2] but was hosts[%s]", actual.HostId)
  50. }
  51. }
  52. func TestRoundRobinConnPolicy(t *testing.T) {
  53. policy := NewRoundRobinConnPolicy()
  54. conn0 := &Conn{}
  55. conn1 := &Conn{}
  56. conn := []*Conn{
  57. conn0,
  58. conn1,
  59. }
  60. policy.SetConns(conn)
  61. // the first conn selected is actually at [1], but this is ok for RR
  62. if actual := policy.Pick(nil); actual != conn1 {
  63. t.Error("Expected conn1")
  64. }
  65. if actual := policy.Pick(nil); actual != conn0 {
  66. t.Error("Expected conn0")
  67. }
  68. if actual := policy.Pick(nil); actual != conn1 {
  69. t.Error("Expected conn1")
  70. }
  71. }