Skip to content

Commit

Permalink
test: unit tests for the helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jvillafanez committed Apr 12, 2024
1 parent f071518 commit 40fca5e
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions ocis-pkg/runner/helper_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
})

0 comments on commit 40fca5e

Please sign in to comment.