Skip to content

Commit

Permalink
Add testcase to verify that errors are ignored when the task state al…
Browse files Browse the repository at this point in the history
…ready changed to completed
  • Loading branch information
timandy committed Oct 16, 2023
1 parent 41a5df0 commit 86e4e37
Showing 1 changed file with 148 additions and 0 deletions.
148 changes: 148 additions & 0 deletions api_routine_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package routine

import (
"io"
"os"
"path"
"strconv"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -132,6 +136,40 @@ func TestWrapTask_HasContext(t *testing.T) {
assert.True(t, run)
}

func TestWrapTask_Complete_ThenFail(t *testing.T) {
newStdout, oldStdout := captureStdout()
defer restoreStdout(newStdout, oldStdout)
//
run := false
wg := &sync.WaitGroup{}
wg.Add(1)
wg2 := &sync.WaitGroup{}
wg2.Add(1)
wg3 := &sync.WaitGroup{}
wg3.Add(1)
task := WrapTask(func() {
wg.Done() //1
wg2.Wait() //4
run = true
wg3.Done() //5
panic(1)
})
go task.Run()
wg.Wait() //2
task.Complete(nil)
assert.Nil(t, task.Get())
wg2.Done() //3
wg3.Wait() //6
assert.True(t, task.IsDone())
assert.False(t, task.IsFailed())
assert.False(t, task.IsCanceled())
assert.True(t, run)
//
time.Sleep(10 * time.Millisecond)
output := readAll(newStdout)
assert.Equal(t, "", output)
}

func TestWrapWaitTask_NoContext(t *testing.T) {
run := false
wrappedRun := false
Expand Down Expand Up @@ -304,6 +342,40 @@ func TestWrapWaitTask_HasContext_Cancel(t *testing.T) {
assert.True(t, run)
}

func TestWrapWaitTask_Complete_ThenFail(t *testing.T) {
newStdout, oldStdout := captureStdout()
defer restoreStdout(newStdout, oldStdout)
//
run := false
wg := &sync.WaitGroup{}
wg.Add(1)
wg2 := &sync.WaitGroup{}
wg2.Add(1)
wg3 := &sync.WaitGroup{}
wg3.Add(1)
task := WrapWaitTask(func(token CancelToken) {
wg.Done() //1
wg2.Wait() //4
run = true
wg3.Done() //5
panic(1)
})
go task.Run()
wg.Wait() //2
task.Complete(nil)
assert.Nil(t, task.Get())
wg2.Done() //3
wg3.Wait() //6
assert.True(t, task.IsDone())
assert.False(t, task.IsFailed())
assert.False(t, task.IsCanceled())
assert.True(t, run)
//
time.Sleep(10 * time.Millisecond)
output := readAll(newStdout)
assert.Equal(t, "", output)
}

func TestWrapWaitResultTask_NoContext(t *testing.T) {
run := false
wrappedRun := false
Expand Down Expand Up @@ -480,6 +552,40 @@ func TestWrapWaitResultTask_HasContext_Cancel(t *testing.T) {
assert.True(t, run)
}

func TestWrapWaitResultTask_Complete_ThenFail(t *testing.T) {
newStdout, oldStdout := captureStdout()
defer restoreStdout(newStdout, oldStdout)
//
run := false
wg := &sync.WaitGroup{}
wg.Add(1)
wg2 := &sync.WaitGroup{}
wg2.Add(1)
wg3 := &sync.WaitGroup{}
wg3.Add(1)
task := WrapWaitResultTask(func(token CancelToken) any {
wg.Done() //1
wg2.Wait() //4
run = true
wg3.Done() //5
panic(1)
})
go task.Run()
wg.Wait() //2
task.Complete(nil)
assert.Nil(t, task.Get())
wg2.Done() //3
wg3.Wait() //6
assert.True(t, task.IsDone())
assert.False(t, task.IsFailed())
assert.False(t, task.IsCanceled())
assert.True(t, run)
//
time.Sleep(10 * time.Millisecond)
output := readAll(newStdout)
assert.Equal(t, "", output)
}

func TestGo_Error(t *testing.T) {
run := false
assert.NotPanics(t, func() {
Expand Down Expand Up @@ -700,3 +806,45 @@ func TestGoWaitResult_Cross(t *testing.T) {
}).Get()
assert.Nil(t, result)
}

func captureStdout() (newStdout, oldStdout *os.File) {
oldStdout = os.Stdout
fileName := path.Join(os.TempDir(), "go_test_"+strconv.FormatInt(time.Now().UnixNano(), 10)+".txt")
file, err := os.Create(fileName)
if err != nil {
panic(err)
}
os.Stdout = file
newStdout = file
return
}

func restoreStdout(newStdout, oldStdout *os.File) {
os.Stdout = oldStdout
if err := newStdout.Close(); err != nil {
panic(err)
}
if err := os.Remove(newStdout.Name()); err != nil {
panic(err)
}
}

func readAll(rs io.ReadSeeker) string {
if _, err := rs.Seek(0, io.SeekStart); err != nil {
panic(err)
}
b := make([]byte, 0, 512)
for {
if len(b) == cap(b) {
b = append(b, 0)[:len(b)]
}
n, err := rs.Read(b[len(b):cap(b)])
b = b[:len(b)+n]
if err != nil {
if err == io.EOF {
return string(b)
}
panic(err)
}
}
}

0 comments on commit 86e4e37

Please sign in to comment.