Skip to content

Commit

Permalink
Add some cache and healtcheck tests
Browse files Browse the repository at this point in the history
  • Loading branch information
csmarchbanks committed Mar 14, 2018
1 parent 4f2addb commit 41475c6
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
62 changes: 62 additions & 0 deletions pkg/ingester/client/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package client

import (
fmt "fmt"
"testing"

"google.golang.org/grpc/health/grpc_health_v1"
)

func (i mockIngester) Close() error {
return nil
}

func TestIngesterCache(t *testing.T) {
buildCount := 0
factory := func(addr string, _ Config) (IngesterClient, error) {
if addr == "bad" {
return nil, fmt.Errorf("Fail")
}
buildCount++
return mockIngester{happy: true, status: grpc_health_v1.HealthCheckResponse_SERVING}, nil
}
cache := NewIngesterClientCache(factory, Config{})

cache.GetClientFor("1")
if buildCount != 1 {
t.Errorf("Did not create client")
}

cache.GetClientFor("1")
if buildCount != 1 {
t.Errorf("Created client that should have been cached")
}

cache.GetClientFor("2")
if cache.Count() != 2 {
t.Errorf("Expected Count() = 2, got %d", cache.Count())
}

cache.RemoveClientFor("1")
if cache.Count() != 1 {
t.Errorf("Expected Count() = 1, got %d", cache.Count())
}

cache.GetClientFor("1")
if buildCount != 3 || cache.Count() != 2 {
t.Errorf("Did not re-create client correctly")
}

_, err := cache.GetClientFor("bad")
if err == nil {
t.Errorf("Bad create should have thrown an error")
}
if cache.Count() != 2 {
t.Errorf("Bad create should not have been added to cache")
}

addrs := cache.RegisteredAddresses()
if len(addrs) != cache.Count() {
t.Errorf("Lengths of registered addresses and cache.Count() do not match")
}
}
45 changes: 45 additions & 0 deletions pkg/ingester/client/healthcheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package client

import (
fmt "fmt"
"testing"
"time"

"golang.org/x/net/context"
grpc "google.golang.org/grpc"
"google.golang.org/grpc/health/grpc_health_v1"
)

type mockIngester struct {
IngesterClient
happy bool
status grpc_health_v1.HealthCheckResponse_ServingStatus
}

func (i mockIngester) Check(ctx context.Context, in *grpc_health_v1.HealthCheckRequest, opts ...grpc.CallOption) (*grpc_health_v1.HealthCheckResponse, error) {
if !i.happy {
return nil, fmt.Errorf("Fail")
}
return &grpc_health_v1.HealthCheckResponse{Status: i.status}, nil
}

func TestHealthCheck(t *testing.T) {
tcs := []struct {
ingester mockIngester
hasError bool
}{
{mockIngester{happy: true, status: grpc_health_v1.HealthCheckResponse_UNKNOWN}, true},
{mockIngester{happy: true, status: grpc_health_v1.HealthCheckResponse_SERVING}, false},
{mockIngester{happy: true, status: grpc_health_v1.HealthCheckResponse_NOT_SERVING}, true},
{mockIngester{happy: false, status: grpc_health_v1.HealthCheckResponse_UNKNOWN}, true},
{mockIngester{happy: false, status: grpc_health_v1.HealthCheckResponse_SERVING}, true},
{mockIngester{happy: false, status: grpc_health_v1.HealthCheckResponse_NOT_SERVING}, true},
}
for _, tc := range tcs {
err := HealthCheck(tc.ingester, 50*time.Millisecond)
hasError := err != nil
if hasError != tc.hasError {
t.Errorf("Expected error: %t, error: %v", tc.hasError, err)
}
}
}

0 comments on commit 41475c6

Please sign in to comment.