Skip to content

Commit 41475c6

Browse files
committed
Add some cache and healtcheck tests
1 parent 4f2addb commit 41475c6

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

pkg/ingester/client/cache_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package client
2+
3+
import (
4+
fmt "fmt"
5+
"testing"
6+
7+
"google.golang.org/grpc/health/grpc_health_v1"
8+
)
9+
10+
func (i mockIngester) Close() error {
11+
return nil
12+
}
13+
14+
func TestIngesterCache(t *testing.T) {
15+
buildCount := 0
16+
factory := func(addr string, _ Config) (IngesterClient, error) {
17+
if addr == "bad" {
18+
return nil, fmt.Errorf("Fail")
19+
}
20+
buildCount++
21+
return mockIngester{happy: true, status: grpc_health_v1.HealthCheckResponse_SERVING}, nil
22+
}
23+
cache := NewIngesterClientCache(factory, Config{})
24+
25+
cache.GetClientFor("1")
26+
if buildCount != 1 {
27+
t.Errorf("Did not create client")
28+
}
29+
30+
cache.GetClientFor("1")
31+
if buildCount != 1 {
32+
t.Errorf("Created client that should have been cached")
33+
}
34+
35+
cache.GetClientFor("2")
36+
if cache.Count() != 2 {
37+
t.Errorf("Expected Count() = 2, got %d", cache.Count())
38+
}
39+
40+
cache.RemoveClientFor("1")
41+
if cache.Count() != 1 {
42+
t.Errorf("Expected Count() = 1, got %d", cache.Count())
43+
}
44+
45+
cache.GetClientFor("1")
46+
if buildCount != 3 || cache.Count() != 2 {
47+
t.Errorf("Did not re-create client correctly")
48+
}
49+
50+
_, err := cache.GetClientFor("bad")
51+
if err == nil {
52+
t.Errorf("Bad create should have thrown an error")
53+
}
54+
if cache.Count() != 2 {
55+
t.Errorf("Bad create should not have been added to cache")
56+
}
57+
58+
addrs := cache.RegisteredAddresses()
59+
if len(addrs) != cache.Count() {
60+
t.Errorf("Lengths of registered addresses and cache.Count() do not match")
61+
}
62+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package client
2+
3+
import (
4+
fmt "fmt"
5+
"testing"
6+
"time"
7+
8+
"golang.org/x/net/context"
9+
grpc "google.golang.org/grpc"
10+
"google.golang.org/grpc/health/grpc_health_v1"
11+
)
12+
13+
type mockIngester struct {
14+
IngesterClient
15+
happy bool
16+
status grpc_health_v1.HealthCheckResponse_ServingStatus
17+
}
18+
19+
func (i mockIngester) Check(ctx context.Context, in *grpc_health_v1.HealthCheckRequest, opts ...grpc.CallOption) (*grpc_health_v1.HealthCheckResponse, error) {
20+
if !i.happy {
21+
return nil, fmt.Errorf("Fail")
22+
}
23+
return &grpc_health_v1.HealthCheckResponse{Status: i.status}, nil
24+
}
25+
26+
func TestHealthCheck(t *testing.T) {
27+
tcs := []struct {
28+
ingester mockIngester
29+
hasError bool
30+
}{
31+
{mockIngester{happy: true, status: grpc_health_v1.HealthCheckResponse_UNKNOWN}, true},
32+
{mockIngester{happy: true, status: grpc_health_v1.HealthCheckResponse_SERVING}, false},
33+
{mockIngester{happy: true, status: grpc_health_v1.HealthCheckResponse_NOT_SERVING}, true},
34+
{mockIngester{happy: false, status: grpc_health_v1.HealthCheckResponse_UNKNOWN}, true},
35+
{mockIngester{happy: false, status: grpc_health_v1.HealthCheckResponse_SERVING}, true},
36+
{mockIngester{happy: false, status: grpc_health_v1.HealthCheckResponse_NOT_SERVING}, true},
37+
}
38+
for _, tc := range tcs {
39+
err := HealthCheck(tc.ingester, 50*time.Millisecond)
40+
hasError := err != nil
41+
if hasError != tc.hasError {
42+
t.Errorf("Expected error: %t, error: %v", tc.hasError, err)
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)