|
|
@@ -4,7 +4,12 @@
|
|
|
|
|
|
package gocql
|
|
|
|
|
|
-import "testing"
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "testing"
|
|
|
+
|
|
|
+ "github.com/hailocab/go-hostpool"
|
|
|
+)
|
|
|
|
|
|
// Tests of the round-robin host selection policy implementation
|
|
|
func TestRoundRobinHostPolicy(t *testing.T) {
|
|
|
@@ -20,26 +25,26 @@ func TestRoundRobinHostPolicy(t *testing.T) {
|
|
|
// the first host selected is actually at [1], but this is ok for RR
|
|
|
// interleaved iteration should always increment the host
|
|
|
iterA := policy.Pick(nil)
|
|
|
- if actual := iterA(); actual != &hosts[1] {
|
|
|
- t.Errorf("Expected hosts[1] but was hosts[%s]", actual.HostId)
|
|
|
+ if actual := iterA(); actual.Info() != &hosts[1] {
|
|
|
+ t.Errorf("Expected hosts[1] but was hosts[%s]", actual.Info().HostId)
|
|
|
}
|
|
|
iterB := policy.Pick(nil)
|
|
|
- if actual := iterB(); actual != &hosts[0] {
|
|
|
- t.Errorf("Expected hosts[0] but was hosts[%s]", actual.HostId)
|
|
|
+ if actual := iterB(); actual.Info() != &hosts[0] {
|
|
|
+ t.Errorf("Expected hosts[0] but was hosts[%s]", actual.Info().HostId)
|
|
|
}
|
|
|
- if actual := iterB(); actual != &hosts[1] {
|
|
|
- t.Errorf("Expected hosts[1] but was hosts[%s]", actual.HostId)
|
|
|
+ if actual := iterB(); actual.Info() != &hosts[1] {
|
|
|
+ t.Errorf("Expected hosts[1] but was hosts[%s]", actual.Info().HostId)
|
|
|
}
|
|
|
- if actual := iterA(); actual != &hosts[0] {
|
|
|
- t.Errorf("Expected hosts[0] but was hosts[%s]", actual.HostId)
|
|
|
+ if actual := iterA(); actual.Info() != &hosts[0] {
|
|
|
+ t.Errorf("Expected hosts[0] but was hosts[%s]", actual.Info().HostId)
|
|
|
}
|
|
|
|
|
|
iterC := policy.Pick(nil)
|
|
|
- if actual := iterC(); actual != &hosts[1] {
|
|
|
- t.Errorf("Expected hosts[1] but was hosts[%s]", actual.HostId)
|
|
|
+ if actual := iterC(); actual.Info() != &hosts[1] {
|
|
|
+ t.Errorf("Expected hosts[1] but was hosts[%s]", actual.Info().HostId)
|
|
|
}
|
|
|
- if actual := iterC(); actual != &hosts[0] {
|
|
|
- t.Errorf("Expected hosts[0] but was hosts[%s]", actual.HostId)
|
|
|
+ if actual := iterC(); actual.Info() != &hosts[0] {
|
|
|
+ t.Errorf("Expected hosts[0] but was hosts[%s]", actual.Info().HostId)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -70,13 +75,13 @@ func TestTokenAwareHostPolicy(t *testing.T) {
|
|
|
|
|
|
// the token ring is not setup without the partitioner, but the fallback
|
|
|
// should work
|
|
|
- if actual := policy.Pick(nil)(); actual.Peer != "1" {
|
|
|
- t.Errorf("Expected peer 1 but was %s", actual.Peer)
|
|
|
+ if actual := policy.Pick(nil)(); actual.Info().Peer != "1" {
|
|
|
+ t.Errorf("Expected peer 1 but was %s", actual.Info().Peer)
|
|
|
}
|
|
|
|
|
|
query.RoutingKey([]byte("30"))
|
|
|
- if actual := policy.Pick(query)(); actual.Peer != "2" {
|
|
|
- t.Errorf("Expected peer 2 but was %s", actual.Peer)
|
|
|
+ if actual := policy.Pick(query)(); actual.Info().Peer != "2" {
|
|
|
+ t.Errorf("Expected peer 2 but was %s", actual.Info().Peer)
|
|
|
}
|
|
|
|
|
|
policy.SetPartitioner("OrderedPartitioner")
|
|
|
@@ -84,19 +89,58 @@ func TestTokenAwareHostPolicy(t *testing.T) {
|
|
|
// now the token ring is configured
|
|
|
query.RoutingKey([]byte("20"))
|
|
|
iter = policy.Pick(query)
|
|
|
- if actual := iter(); actual.Peer != "1" {
|
|
|
- t.Errorf("Expected peer 1 but was %s", actual.Peer)
|
|
|
+ if actual := iter(); actual.Info().Peer != "1" {
|
|
|
+ t.Errorf("Expected peer 1 but was %s", actual.Info().Peer)
|
|
|
}
|
|
|
// rest are round robin
|
|
|
- if actual := iter(); actual.Peer != "3" {
|
|
|
- t.Errorf("Expected peer 3 but was %s", actual.Peer)
|
|
|
+ if actual := iter(); actual.Info().Peer != "3" {
|
|
|
+ t.Errorf("Expected peer 3 but was %s", actual.Info().Peer)
|
|
|
+ }
|
|
|
+ if actual := iter(); actual.Info().Peer != "0" {
|
|
|
+ t.Errorf("Expected peer 0 but was %s", actual.Info().Peer)
|
|
|
+ }
|
|
|
+ if actual := iter(); actual.Info().Peer != "2" {
|
|
|
+ t.Errorf("Expected peer 2 but was %s", actual.Info().Peer)
|
|
|
}
|
|
|
- if actual := iter(); actual.Peer != "0" {
|
|
|
- t.Errorf("Expected peer 0 but was %s", actual.Peer)
|
|
|
+}
|
|
|
+
|
|
|
+// Tests of the host pool host selection policy implementation
|
|
|
+func TestHostPoolHostPolicy(t *testing.T) {
|
|
|
+ policy := HostPoolHostPolicy(hostpool.New(nil))
|
|
|
+
|
|
|
+ hosts := []HostInfo{
|
|
|
+ HostInfo{HostId: "0", Peer: "0"},
|
|
|
+ HostInfo{HostId: "1", Peer: "1"},
|
|
|
}
|
|
|
- if actual := iter(); actual.Peer != "2" {
|
|
|
- t.Errorf("Expected peer 2 but was %s", actual.Peer)
|
|
|
+
|
|
|
+ policy.SetHosts(hosts)
|
|
|
+
|
|
|
+ // the first host selected is actually at [1], but this is ok for RR
|
|
|
+ // interleaved iteration should always increment the host
|
|
|
+ iter := policy.Pick(nil)
|
|
|
+ actualA := iter()
|
|
|
+ if actualA.Info().HostId != "0" {
|
|
|
+ t.Errorf("Expected hosts[0] but was hosts[%s]", actualA.Info().HostId)
|
|
|
+ }
|
|
|
+ actualA.Mark(nil)
|
|
|
+
|
|
|
+ actualB := iter()
|
|
|
+ if actualB.Info().HostId != "1" {
|
|
|
+ t.Errorf("Expected hosts[1] but was hosts[%s]", actualB.Info().HostId)
|
|
|
+ }
|
|
|
+ actualB.Mark(fmt.Errorf("error"))
|
|
|
+
|
|
|
+ actualC := iter()
|
|
|
+ if actualC.Info().HostId != "0" {
|
|
|
+ t.Errorf("Expected hosts[0] but was hosts[%s]", actualC.Info().HostId)
|
|
|
+ }
|
|
|
+ actualC.Mark(nil)
|
|
|
+
|
|
|
+ actualD := iter()
|
|
|
+ if actualD.Info().HostId != "0" {
|
|
|
+ t.Errorf("Expected hosts[0] but was hosts[%s]", actualD.Info().HostId)
|
|
|
}
|
|
|
+ actualD.Mark(nil)
|
|
|
}
|
|
|
|
|
|
// Tests of the round-robin connection selection policy implementation
|