forked from synapsecns/sanguine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleep_test.go
62 lines (49 loc) · 1.33 KB
/
sleep_test.go
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
package core_test
import (
"errors"
"fmt"
"github.com/synapsecns/sanguine/core"
"testing"
"time"
)
func TestSleepWithContext(t *testing.T) {
ctx := &FakeContext{DoneCh: make(chan struct{})}
err := core.SleepWithContext(ctx, 1*time.Millisecond)
if err != nil {
t.Errorf("expect context to not be canceled, got %v", err)
}
}
func TestSleepWithContext_Canceled(t *testing.T) {
ctx := &FakeContext{DoneCh: make(chan struct{})}
expectErr := fmt.Errorf("context canceled")
ctx.Error = expectErr
close(ctx.DoneCh)
err := core.SleepWithContext(ctx, 10*time.Second)
if err == nil {
t.Fatalf("expect error, did not get one")
}
if e, a := expectErr, err; !errors.Is(e, a) {
t.Errorf("expect %v error, got %v", e, a)
}
}
// A FakeContext provides a simple stub implementation of a Context.
type FakeContext struct {
Error error
DoneCh chan struct{}
}
// Deadline always will return not set.
func (c *FakeContext) Deadline() (deadline time.Time, ok bool) {
return time.Time{}, false
}
// Done returns a read channel for listening to the Done event.
func (c *FakeContext) Done() <-chan struct{} {
return c.DoneCh
}
// Err returns the error, is nil if not set.
func (c *FakeContext) Err() error {
return c.Error
}
// Value ignores the Value and always returns nil.
func (c *FakeContext) Value(key interface{}) interface{} {
return nil
}