2018-04-08 14:08:34 +00:00
|
|
|
package cachetype
|
|
|
|
|
|
|
|
import (
|
2018-04-15 20:11:04 +00:00
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
2018-04-08 14:08:34 +00:00
|
|
|
"github.com/mitchellh/go-testing-interface"
|
2020-10-09 00:15:13 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
2018-04-08 14:08:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestRPC returns a mock implementation of the RPC interface.
|
|
|
|
func TestRPC(t testing.T) *MockRPC {
|
|
|
|
// This function is relatively useless but this allows us to perhaps
|
|
|
|
// perform some initialization later.
|
|
|
|
return &MockRPC{}
|
|
|
|
}
|
2018-04-15 20:11:04 +00:00
|
|
|
|
|
|
|
// TestFetchCh returns a channel that returns the result of the Fetch call.
|
|
|
|
// This is useful for testing timing and concurrency with Fetch calls.
|
|
|
|
// Errors will show up as an error type on the resulting channel so a
|
|
|
|
// type switch should be used.
|
|
|
|
func TestFetchCh(
|
|
|
|
t testing.T,
|
|
|
|
typ cache.Type,
|
|
|
|
opts cache.FetchOptions,
|
2020-10-09 00:15:13 +00:00
|
|
|
req cache.Request,
|
|
|
|
) <-chan interface{} {
|
2018-04-15 20:11:04 +00:00
|
|
|
resultCh := make(chan interface{})
|
|
|
|
go func() {
|
|
|
|
result, err := typ.Fetch(opts, req)
|
|
|
|
if err != nil {
|
|
|
|
resultCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resultCh <- result
|
|
|
|
}()
|
|
|
|
|
|
|
|
return resultCh
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFetchChResult tests that the result from TestFetchCh matches
|
|
|
|
// within a reasonable period of time (it expects it to be "immediate" but
|
|
|
|
// waits some milliseconds).
|
|
|
|
func TestFetchChResult(t testing.T, ch <-chan interface{}, expected interface{}) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case result := <-ch:
|
|
|
|
if err, ok := result.(error); ok {
|
|
|
|
t.Fatalf("Result was error: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(result, expected) {
|
|
|
|
t.Fatalf("Result doesn't match!\n\n%#v\n\n%#v", result, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
}
|
|
|
|
}
|