Skip to content

Commit

Permalink
Add more executor tests to cover all cases
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwasila committed Jan 7, 2020
1 parent 0b88a4d commit c277e48
Showing 1 changed file with 136 additions and 6 deletions.
142 changes: 136 additions & 6 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import (
. "github.com/smartystreets/goconvey/convey"
)

func TestEmptyExecutor(t *testing.T) {
Convey("Given empty executor", t, func() {
e := Executor()
Convey("When empty executor runs it should not panic", func() {
So(e.Run, ShouldNotPanic)
})
})
}

func TestSimpleConcurrentExecution(t *testing.T) {
Convey("Given executor with concurrent operations", t, func() {
var response chan rune = make(chan rune, 6)
Expand All @@ -32,19 +41,140 @@ func TestSimpleConcurrentExecution(t *testing.T) {
}),
Execute(func() {
time.Sleep(100 * time.Millisecond)
close(response)
}),
)
Convey("When executor runs", func() {
e.Run()
sb := strings.Builder{}
for r := range response {
sb.WriteRune(r)
}
result := readChannel(response)

Convey("Then all operations should be executed concurrently in correct order determined by internal delays", func() {
So(sb.String(), ShouldEqual, "ABCcba")
So(result, ShouldEqual, "ABCcba")
})
})
})
}

func TestExecutionFinalizer(t *testing.T) {
Convey("Given executor single operation and finalizer", t, func() {
var response chan rune = make(chan rune, 6)
e := Executor(
Execute(func() {
response <- 'A'
}),
WhenAllFinished(func() {
response <- 'F'
}),
)
Convey("When executor runs", func() {
e.Run()
result := readChannel(response)

Convey("Then operation should run then finalizer", func() {
So(result, ShouldEqual, "AF")
})
})
})
}

func TestExecutionWithTwoFinalizers(t *testing.T) {
Convey("Given executor single operation and two finalizers", t, func() {
var response chan rune = make(chan rune, 6)
e := Executor(
Execute(func() {
response <- 'A'
}),
Execute(func() {
time.Sleep(100 * time.Millisecond)
response <- 'B'
}),
WhenAllFinished(func() {
response <- 'X'
}),
WhenAllFinished(func() {
response <- 'F'
}),
)
Convey("When executor runs", func() {
e.Run()
result := readChannel(response)

Convey("Then operations should run and only last defined finalizer", func() {
So(result, ShouldEqual, "ABF")
})
})
})
}

func TestExecutionFinalizerFirst(t *testing.T) {
Convey("Given executor with finalizer defined before operation", t, func() {
var response chan rune = make(chan rune, 6)
e := Executor(
WhenAllFinished(func() {
response <- 'F'
}),
Execute(func() {
response <- 'A'
}),
)
Convey("When executor runs", func() {
e.Run()
result := readChannel(response)

Convey("Then all operation should still be executed before finalizer", func() {
So(result, ShouldEqual, "AF")
})
})
})
}

func TestExecutionOnlyFinalizer(t *testing.T) {
Convey("Given executor with no operation", t, func() {
var response chan rune = make(chan rune, 6)
e := Executor(
WhenAllFinished(func() {
response <- 'F'
}),
)
Convey("When executor runs", func() {
e.Run()
result := readChannel(response)

Convey("Then all operations should be executed concurrently in correct order determined by internal delays", func() {
So(result, ShouldEqual, "F")
})
})
})
}

func TestExecutionCatchingPanic(t *testing.T) {
Convey("Given executor with panic handler", t, func() {
var response chan rune = make(chan rune, 6)
e := Executor(
Execute(func() {
response <- 'A'
panic('p')
}),
WhenPanic(func(p interface{}) {
response <- 'P'
response <- p.(rune)
}),
)
Convey("When executor runs it should not panic", func() {
e.Run()
result := readChannel(response)

Convey("Then panic is succesfuly recovered", func() {
So(result, ShouldEqual, "APp")
})
})
})
}

func readChannel(ch chan rune) string {
close(ch)
sb := strings.Builder{}
for r := range ch {
sb.WriteRune(r)
}
return sb.String()
}

0 comments on commit c277e48

Please sign in to comment.