2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2018-04-08 14:08:34 +00:00
|
|
|
package cachetype
|
|
|
|
|
|
|
|
import (
|
2018-04-15 20:11:04 +00:00
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
2022-07-05 21:57:15 +00:00
|
|
|
testinf "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.
|
2022-07-05 21:57:15 +00:00
|
|
|
func TestRPC(t testinf.T) *MockRPC {
|
2018-04-08 14:08:34 +00:00
|
|
|
// 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(
|
2022-07-05 21:57:15 +00:00
|
|
|
t testinf.T,
|
2018-04-15 20:11:04 +00:00
|
|
|
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).
|
2022-07-05 21:57:15 +00:00
|
|
|
func TestFetchChResult(t testinf.T, ch <-chan interface{}, expected interface{}) {
|
2018-04-15 20:11:04 +00:00
|
|
|
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):
|
|
|
|
}
|
|
|
|
}
|