|
|
@@ -5,6 +5,7 @@ package gocql
|
|
|
import (
|
|
|
"context"
|
|
|
"fmt"
|
|
|
+ "net"
|
|
|
"testing"
|
|
|
)
|
|
|
|
|
|
@@ -99,18 +100,15 @@ func (f funcQueryObserver) ObserveQuery(ctx context.Context, o ObservedQuery) {
|
|
|
func TestQueryBasicAPI(t *testing.T) {
|
|
|
qry := &Query{}
|
|
|
|
|
|
- // Initialise metrics map
|
|
|
- qry.metrics = &queryMetrics{m: make(map[string]*hostMetrics)}
|
|
|
-
|
|
|
// Initiate host
|
|
|
ip := "127.0.0.1"
|
|
|
|
|
|
- qry.metrics.m[ip] = &hostMetrics{Attempts: 0, TotalLatency: 0}
|
|
|
+ qry.metrics = preFilledQueryMetrics(map[string]*hostMetrics{ip: {Attempts: 0, TotalLatency: 0}})
|
|
|
if qry.Latency() != 0 {
|
|
|
t.Fatalf("expected Query.Latency() to return 0, got %v", qry.Latency())
|
|
|
}
|
|
|
|
|
|
- qry.metrics.m[ip] = &hostMetrics{Attempts: 2, TotalLatency: 4}
|
|
|
+ qry.metrics = preFilledQueryMetrics(map[string]*hostMetrics{ip: {Attempts: 2, TotalLatency: 4}})
|
|
|
if qry.Attempts() != 2 {
|
|
|
t.Fatalf("expected Query.Attempts() to return 2, got %v", qry.Attempts())
|
|
|
}
|
|
|
@@ -118,6 +116,11 @@ func TestQueryBasicAPI(t *testing.T) {
|
|
|
t.Fatalf("expected Query.Latency() to return 2, got %v", qry.Latency())
|
|
|
}
|
|
|
|
|
|
+ qry.AddAttempts(2, &HostInfo{hostname: ip, connectAddress: net.ParseIP(ip), port: 9042})
|
|
|
+ if qry.Attempts() != 4 {
|
|
|
+ t.Fatalf("expected Query.Attempts() to return 4, got %v", qry.Attempts())
|
|
|
+ }
|
|
|
+
|
|
|
qry.Consistency(All)
|
|
|
if qry.GetConsistency() != All {
|
|
|
t.Fatalf("expected Query.GetConsistency to return 'All', got '%s'", qry.GetConsistency())
|
|
|
@@ -202,21 +205,25 @@ func TestBatchBasicAPI(t *testing.T) {
|
|
|
t.Fatalf("expected batch.Type to be '%v', got '%v'", LoggedBatch, b.Type)
|
|
|
}
|
|
|
|
|
|
- b.metrics = &queryMetrics{m: make(map[string]*hostMetrics)}
|
|
|
ip := "127.0.0.1"
|
|
|
|
|
|
// Test attempts
|
|
|
- b.metrics.m[ip] = &hostMetrics{Attempts: 1}
|
|
|
+ b.metrics = preFilledQueryMetrics(map[string]*hostMetrics{ip: {Attempts: 1}})
|
|
|
if b.Attempts() != 1 {
|
|
|
t.Fatalf("expected batch.Attempts() to return %v, got %v", 1, b.Attempts())
|
|
|
}
|
|
|
|
|
|
+ b.AddAttempts(2, &HostInfo{hostname: ip, connectAddress: net.ParseIP(ip), port: 9042})
|
|
|
+ if b.Attempts() != 3 {
|
|
|
+ t.Fatalf("expected batch.Attempts() to return %v, got %v", 3, b.Attempts())
|
|
|
+ }
|
|
|
+
|
|
|
// Test latency
|
|
|
if b.Latency() != 0 {
|
|
|
t.Fatalf("expected batch.Latency() to be 0, got %v", b.Latency())
|
|
|
}
|
|
|
|
|
|
- b.metrics.m[ip] = &hostMetrics{Attempts: 1, TotalLatency: 4}
|
|
|
+ b.metrics = preFilledQueryMetrics(map[string]*hostMetrics{ip: {Attempts: 1, TotalLatency: 4}})
|
|
|
if b.Latency() != 4 {
|
|
|
t.Fatalf("expected batch.Latency() to return %v, got %v", 4, b.Latency())
|
|
|
}
|