Skip to content

Commit

Permalink
add(util):add simply exec function
Browse files Browse the repository at this point in the history
  • Loading branch information
ITcathyh committed Dec 30, 2019
1 parent 9ffdee0 commit 7046c6c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 56 deletions.
96 changes: 40 additions & 56 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,7 @@ import (

func testTimeout(t *testing.T, c TimedActuator) {
st := time.Now().UnixNano()
err := c.Exec(
func() error {
fmt.Println(1)
time.Sleep(time.Millisecond * 100)
return nil
},
func() error {
fmt.Println(2)
return nil
},
func() error {
time.Sleep(time.Millisecond * 200)
fmt.Println(3)
return nil
},
)
err := c.Exec(getTasks()...)

Equal(t, err, ErrorTimeOut)
et := time.Now().UnixNano()
Expand All @@ -36,8 +21,29 @@ func testTimeout(t *testing.T, c TimedActuator) {

func testError(t *testing.T, c TimedActuator) {
st := time.Now().UnixNano()
te := errors.New("TestErr")
err := c.Exec(
tasks, te := getErrorTask()
err := c.Exec(tasks...)

Equal(t, err, te)
et := time.Now().UnixNano()
t.Logf("used time:%ds", (et-st)/1000000)
time.Sleep(time.Millisecond * 500)
}

func testNormal(t *testing.T, c TimedActuator) {
Equal(t, c.Exec(getTasks()...), nil)
}

func testPanic(t *testing.T, c TimedActuator) {
NotEqual(t, c.Exec(getPanicTask()), nil)
}

func testEmpty(t *testing.T, c TimedActuator) {
Equal(t, c.Exec(), nil)
}

func getTasks() []Task {
return []Task{
func() error {
fmt.Println(1)
time.Sleep(time.Millisecond * 100)
Expand All @@ -52,6 +58,14 @@ func testError(t *testing.T, c TimedActuator) {
fmt.Println(3)
return nil
},
}
}

func getErrorTask() ([]Task, error) {
te := errors.New("TestErr")

tasks := getTasks()
tasks = append(tasks,
func() error {
fmt.Println("4")
return te
Expand All @@ -65,46 +79,16 @@ func testError(t *testing.T, c TimedActuator) {
time.Sleep(time.Second)
fmt.Println("6")
return te
},
)
}, )

Equal(t, err, te)
et := time.Now().UnixNano()
t.Logf("used time:%ds", (et-st)/1000000)
time.Sleep(time.Millisecond * 500)
return tasks, te
}

func testNormal(t *testing.T, c TimedActuator) {
fs := []Task{
func() error {
fmt.Println(1)
time.Sleep(time.Millisecond * 100)
return nil
},
func() error {
fmt.Println(2)
return nil
},
func() error {
time.Sleep(time.Millisecond * 200)
fmt.Println(3)
return nil
},
func getPanicTask() Task {
return func() error {
var i *int64
num := *i + 1
fmt.Println(num)
return nil
}

Equal(t, c.Exec(fs...), nil)
}

func testPanic(t *testing.T, c TimedActuator) {
NotEqual(t, c.Exec(
func() error {
var i *int64
num := *i + 1
fmt.Println(num)
return nil
}), nil)
}

func testEmpty(t *testing.T, c TimedActuator) {
Equal(t, c.Exec(), nil)
}
32 changes: 32 additions & 0 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package conexec

import (
"context"
"fmt"
"runtime/debug"
"sync"
"sync/atomic"
"time"
)

Expand Down Expand Up @@ -70,3 +73,32 @@ func execTasks(c TimedActuator, ctx context.Context,
func simplyRun(f func()) {
go f()
}

// Exec simply runs the tasks concurrently
// True will be returned is all tasks complete successfully
// otherwise false will be returned
func Exec(tasks ...Task) bool {
var c int32
wg := &sync.WaitGroup{}
wg.Add(len(tasks))

for _, t := range tasks {
go func(task Task) {
defer func() {
if r := recover(); r != nil {
atomic.CompareAndSwapInt32(&c, 0, 1)
fmt.Printf("conexec panic:%v\n%s\n", r, string(debug.Stack()))
}

wg.Done()
}()

if err := task(); err != nil {
atomic.CompareAndSwapInt32(&c, 0, 1)
}
}(t)
}

wg.Wait()
return c == 0
}
15 changes: 15 additions & 0 deletions exec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package conexec

import (
"testing"

. "github.com/go-playground/assert/v2"
)

func TestExec(t *testing.T) {
Equal(t, Exec(getTasks()...), true)
tasks, _ := getErrorTask()
Equal(t, Exec(tasks...), false)
Equal(t, Exec(getPanicTask()), false)
Equal(t, Exec(), true)
}

0 comments on commit 7046c6c

Please sign in to comment.