-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.go
More file actions
91 lines (79 loc) · 1.78 KB
/
Copy pathcommon.go
File metadata and controls
91 lines (79 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package kivikmock
import (
"context"
"fmt"
"sync"
"time"
kivik "github.com/go-kivik/kivik/v4"
)
type expectation interface {
fulfill()
fulfilled() bool
Lock()
Unlock()
fmt.Stringer
// method should return the name of the method that would trigger this
// condition. If verbose is true, the output should disambiguate between
// different calls to the same method.
method(verbose bool) string
error() error
wait(context.Context) error
// met is called on the actual value, and returns true if the expectation
// is met.
met(expectation) bool
// for DB methods, this returns the associated *DB object
dbo() *DB
opts() kivik.Options
}
// commonExpectation satisfies the expectation interface, except the String()
// and method() methods.
type commonExpectation struct {
sync.Mutex
triggered bool
err error // nolint: structcheck
delay time.Duration
options kivik.Options
db *DB
}
func (e *commonExpectation) opts() kivik.Options {
return e.options
}
func (e *commonExpectation) dbo() *DB {
return e.db
}
func (e *commonExpectation) fulfill() {
e.triggered = true
}
func (e *commonExpectation) fulfilled() bool {
return e.triggered
}
func (e *commonExpectation) error() error {
return e.err
}
// wait blocks until e.delay expires, or ctx is cancelled. If e.delay expires,
// e.err is returned, otherwise ctx.Err() is returned.
func (e *commonExpectation) wait(ctx context.Context) error {
if e.delay == 0 {
return e.err
}
if err := pause(ctx, e.delay); err != nil {
return err
}
return e.err
}
func pause(ctx context.Context, delay time.Duration) error {
if delay == 0 {
return nil
}
t := time.NewTimer(delay)
defer t.Stop()
select {
case <-t.C:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
const (
defaultOptionPlaceholder = "[?]"
)