-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop_test.go
More file actions
65 lines (51 loc) · 1.42 KB
/
loop_test.go
File metadata and controls
65 lines (51 loc) · 1.42 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
// Copyright (C) 2021 Creditor Corp. Group.
// See LICENSE for copying information.
package thelooper_test
import (
"context"
"errors"
"testing"
"time"
"github.com/BoostyLabs/thelooper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoop_Run_Error_With_Interval(t *testing.T) {
loop := &thelooper.Loop{}
loop.SetInterval(time.Second)
err := loop.Run(context.Background(), func(_ context.Context) error {
now := time.Now().UTC()
nextDayTime := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.UTC)
loop.SetNextTickDuration(nextDayTime)
return errors.New("")
})
require.Error(t, err)
}
func TestLoop_Run_NoInterval(t *testing.T) {
loop := &thelooper.Loop{}
require.Panics(t,
func() {
err := loop.Run(context.Background(), func(_ context.Context) error {
return nil
})
require.NoError(t, err)
},
"Run without setting an interval should panic",
)
}
func TestLoop_CtxCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
loop := &thelooper.Loop{}
go func() {
cancel()
}()
loop.SetInterval(time.Second)
err := loop.Run(ctx, func(_ context.Context) error {
now := time.Now().UTC()
nextDayTime := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.UTC)
loop.SetNextTickDuration(nextDayTime)
return nil
})
require.Error(t, err)
assert.True(t, errors.Is(err, thelooper.ErrCtxCancelled))
}