From cb2e8e0ef24800c63dfd2bf1a958d75b1489e067 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Villaf=C3=A1=C3=B1ez?= Date: Fri, 12 Apr 2024 12:49:56 +0200 Subject: [PATCH] test: unit tests for the helper --- ocis-pkg/runner/helper_test.go | 87 ++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 ocis-pkg/runner/helper_test.go diff --git a/ocis-pkg/runner/helper_test.go b/ocis-pkg/runner/helper_test.go new file mode 100644 index 000000000..a64ea9c25 --- /dev/null +++ b/ocis-pkg/runner/helper_test.go @@ -0,0 +1,87 @@ +package runner_test + +import ( + "context" + "time" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/owncloud/ocis/v2/ocis-pkg/runner" +) + +var _ = Describe("Helper", func() { + Describe("InterruptedTimeoutRunner", func() { + It("Context done, no timeout", func(ctx SpecContext) { + r1 := runner.New("task", func() error { + time.Sleep(10 * time.Millisecond) + return nil + }, func() { + }) + + r2 := runner.InterruptedTimeoutRunner(r1, 2*time.Second) + + // context will be done in 1 second (task will finishes before) + myCtx, cancel := context.WithTimeout(ctx, 1*time.Second) + defer cancel() + + // spawn a new goroutine and return the result in the channel + ch2 := make(chan *runner.Result) + go func(ch2 chan *runner.Result) { + ch2 <- r2.Run(myCtx) + close(ch2) + }(ch2) + + expectedResult := &runner.Result{ + RunnerID: "task", + RunnerError: nil, + } + + Eventually(ctx, ch2).Should(Receive(Equal(expectedResult))) + }, SpecTimeout(5*time.Second)) + + It("Context done, timeout reached", func(ctx SpecContext) { + r1 := runner.New("task", func() error { + time.Sleep(10 * time.Second) + return nil + }, func() { + }) + + r2 := runner.InterruptedTimeoutRunner(r1, 2*time.Second) + + // context will be done in 1 second (task will finishes before) + myCtx, cancel := context.WithTimeout(ctx, 1*time.Second) + defer cancel() + + // spawn a new goroutine and return the result in the channel + ch2 := make(chan *runner.Result) + go func(ch2 chan *runner.Result) { + ch2 <- r2.Run(myCtx) + close(ch2) + }(ch2) + + var expectedResult *runner.Result + Eventually(ctx, ch2).Should(Receive(&expectedResult)) + Expect(expectedResult.RunnerID).To(Equal("task")) + Expect(expectedResult.RunnerError.Error()).To(ContainSubstring("Timeout reached")) + }, SpecTimeout(5*time.Second)) + + It("Interrupted, timeout reached", func(ctx SpecContext) { + r1 := runner.New("task", func() error { + time.Sleep(10 * time.Second) + return nil + }, func() { + }) + + r2 := runner.InterruptedTimeoutRunner(r1, 2*time.Second) + + ch2 := make(chan *runner.Result) + r2.RunAsync(ch2) + r2.Interrupt() + + var expectedResult *runner.Result + Eventually(ctx, ch2).Should(Receive(&expectedResult)) + Expect(expectedResult.RunnerID).To(Equal("task")) + Expect(expectedResult.RunnerError.Error()).To(ContainSubstring("Timeout reached")) + }, SpecTimeout(5*time.Second)) + }) +})