Sybren A. Stüvel
9bda21648e
Add a timeout when fetching a job from the persistence layers. It's my intention to add more timeouts, so this also introduces some code to make it easier to test that a context has a deadline set.
37 lines
768 B
Go
37 lines
768 B
Go
package moremock
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
)
|
|
|
|
// ContextWithDeadline returns a gomock matcher to match a context.Context()
|
|
// with a deadline in the future.
|
|
func ContextWithDeadline() gomock.Matcher { return ctxWithDeadlineMatcher{} }
|
|
|
|
type ctxWithDeadlineMatcher struct{}
|
|
|
|
// Matches returns whether x is a match.
|
|
func (m ctxWithDeadlineMatcher) Matches(x interface{}) bool {
|
|
ctx, ok := x.(context.Context)
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
deadline, ok := ctx.Deadline()
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
return time.Now().Before(deadline)
|
|
}
|
|
|
|
// String describes what the matcher matches.
|
|
func (m ctxWithDeadlineMatcher) String() string {
|
|
return "is context with deadline in the future"
|
|
}
|