-
Notifications
You must be signed in to change notification settings - Fork 812
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f2addb
commit 41475c6
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |