Add JSONTracePred to the zipkin testing package (#1157)

Allows the use of an arbitrary predicate when polling a zipkin trace
ID. Enables the creation of tests in which the number of trace spans
may vary.
This commit is contained in:
ian-mi 2020-03-13 17:04:29 -07:00 committed by GitHub
parent a4c6b47c82
commit 7cbd0bcc1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 2 deletions

View File

@ -130,9 +130,17 @@ func CleanupZipkinTracingSetup(logf logging.FormatLogger) {
// JSONTrace returns a trace for the given traceID. It will continually try to get the trace. If the
// trace it gets has the expected number of spans, then it will be returned. If not, it will try
// again. If it reaches timeout, then it returns everything it has so far with an error.
func JSONTrace(traceID string, expected int, timeout time.Duration) (trace []model.SpanModel, err error) {
func JSONTrace(traceID string, expected int, timeout time.Duration) ([]model.SpanModel, error) {
return JSONTracePred(traceID, timeout, func(trace []model.SpanModel) bool { return len(trace) == expected })
}
// JSONTracePred returns a trace for the given traceID. It will
// continually try to get the trace until the trace spans satisfy the
// predicate. If the timeout is reached then the last fetched trace
// tree if available is returned along with an error.
func JSONTracePred(traceID string, timeout time.Duration, pred func([]model.SpanModel) bool) (trace []model.SpanModel, err error) {
t := time.After(timeout)
for len(trace) != expected {
for !pred(trace) {
select {
case <-t:
return trace, &TimeoutError{