From 6c1d9aa39a8c46641904d25b7405856575dec9d5 Mon Sep 17 00:00:00 2001 From: Bogdan Serdinov Date: Thu, 6 Oct 2022 19:19:38 +0300 Subject: [PATCH 1/4] handing ctx cancelation --- .gitignore | 1 + .idea/.gitignore | 8 ++++++++ .idea/modules.xml | 8 ++++++++ .idea/thelooper.iml | 9 +++++++++ .idea/vcs.xml | 6 ++++++ go.mod | 3 +-- go.sum | 3 +-- loop.go | 6 ++++++ loop_test.go | 27 +++++++++++++++++++++++++++ 9 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/thelooper.iml create mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..71e4b5b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/thelooper.iml b/.idea/thelooper.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/thelooper.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/go.mod b/go.mod index 5fb53f5..ef4b345 100644 --- a/go.mod +++ b/go.mod @@ -2,11 +2,10 @@ module github.com/BoostyLabs/thelooper go 1.17 -require golang.org/x/sync v0.0.0-20210220032951-036812b2e83c +require github.com/stretchr/testify v1.7.0 require ( github.com/davecgh/go-spew v1.1.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/testify v1.7.0 // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) diff --git a/go.sum b/go.sum index 29ab36c..acb88a4 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/loop.go b/loop.go index 960a222..d00bb9a 100644 --- a/loop.go +++ b/loop.go @@ -5,9 +5,13 @@ package thelooper import ( "context" + "errors" "time" ) +// ErrCtxCancelled is error returned by loop when context cancelled. +var ErrCtxCancelled = errors.New("context cancelled") + // Loop implements a controllable recurring event. type Loop struct { interval time.Duration @@ -47,6 +51,8 @@ func (loop *Loop) Run(ctx context.Context, fn func(ctx context.Context) error) e } for { select { + case <-ctx.Done(): + return ErrCtxCancelled case <-loop.stop: return nil diff --git a/loop_test.go b/loop_test.go index 1de2681..ac51065 100644 --- a/loop_test.go +++ b/loop_test.go @@ -6,6 +6,8 @@ package thelooper_test import ( "context" "errors" + "github.com/stretchr/testify/assert" + "log" "testing" "time" @@ -41,3 +43,28 @@ func TestLoop_Run_NoInterval(t *testing.T) { "Run without setting an interval should panic", ) } + +func TestLoop_CtxCancel(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + loop := &thelooper.Loop{} + + t.Parallel() + + 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 + }) + + log.Println(err) + + require.Error(t, err) + assert.True(t, errors.Is(err, thelooper.ErrCtxCancelled)) +} From f70e5a571a0e7fa5c68e42f2ee80e9eb0b908caf Mon Sep 17 00:00:00 2001 From: Bogdan Serdinov Date: Fri, 7 Oct 2022 10:43:20 +0300 Subject: [PATCH 2/4] removed idea folder --- .idea/.gitignore | 8 -------- .idea/modules.xml | 8 -------- .idea/thelooper.iml | 9 --------- .idea/vcs.xml | 6 ------ 4 files changed, 31 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/modules.xml delete mode 100644 .idea/thelooper.iml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 71e4b5b..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/thelooper.iml b/.idea/thelooper.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/thelooper.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 575f02a1f7f057d46c56b297d627378b62da61f3 Mon Sep 17 00:00:00 2001 From: Bogdan Serdinov Date: Fri, 7 Oct 2022 10:45:19 +0300 Subject: [PATCH 3/4] fixed remakrs --- loop_test.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/loop_test.go b/loop_test.go index ac51065..6b7ccb9 100644 --- a/loop_test.go +++ b/loop_test.go @@ -6,12 +6,11 @@ package thelooper_test import ( "context" "errors" - "github.com/stretchr/testify/assert" - "log" "testing" "time" "github.com/BoostyLabs/thelooper" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -48,8 +47,6 @@ func TestLoop_CtxCancel(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) loop := &thelooper.Loop{} - t.Parallel() - go func() { cancel() }() @@ -63,8 +60,6 @@ func TestLoop_CtxCancel(t *testing.T) { return nil }) - log.Println(err) - require.Error(t, err) assert.True(t, errors.Is(err, thelooper.ErrCtxCancelled)) } From 128c1ff493f7cabff026cec5f835334d0a1aaee0 Mon Sep 17 00:00:00 2001 From: Bogdan Serdinov Date: Fri, 28 Oct 2022 11:38:31 +0300 Subject: [PATCH 4/4] change setInterval func --- loop.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/loop.go b/loop.go index d00bb9a..3f236fe 100644 --- a/loop.go +++ b/loop.go @@ -27,9 +27,8 @@ func NewLoop(interval time.Duration) *Loop { } // SetInterval allows to change the interval before starting. -// The interval is specified in milliseconds. 1 sec = 1000 millisec. func (loop *Loop) SetInterval(interval time.Duration) { - loop.interval = interval * time.Millisecond + loop.interval = interval } // SetNextTickDuration allows to change the next tick duration after starting.