-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update!: changed the whole of APIs (#61)
- Loading branch information
Showing
32 changed files
with
3,871 additions
and
4,096 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (C) 2023 Takayuki Sato. All Rights Reserved. | ||
// This program is free software under MIT License. | ||
// See the file LICENSE in this distribution for more details. | ||
|
||
package sabi | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/sttk/sabi/errs" | ||
) | ||
|
||
// AsyncGroup is the interface to execute added functions asynchronously. | ||
// The method: Add is to add target functions. | ||
// This interface is used as an argument of DaxSrc#Setup, DaxConn#Commit, and DaxConn#Rollback. | ||
type AsyncGroup interface { | ||
Add(fn func() errs.Err) | ||
} | ||
|
||
type errEntry[N comparable] struct { | ||
name N | ||
err errs.Err | ||
next *errEntry[N] | ||
} | ||
|
||
type asyncGroupAsync[N comparable] struct { | ||
wg sync.WaitGroup | ||
errHead *errEntry[N] | ||
errLast *errEntry[N] | ||
mutex sync.Mutex | ||
name N | ||
} | ||
|
||
func (ag *asyncGroupAsync[N]) Add(fn func() errs.Err) { | ||
ag.wg.Add(1) | ||
go func(name N) { | ||
defer ag.wg.Done() | ||
err := fn() | ||
if err.IsNotOk() { | ||
ag.mutex.Lock() | ||
defer ag.mutex.Unlock() | ||
ag.addErr(name, err) | ||
} | ||
}(ag.name) | ||
} | ||
|
||
func (ag *asyncGroupAsync[N]) wait() { | ||
ag.wg.Wait() | ||
} | ||
|
||
func (ag *asyncGroupAsync[N]) addErr(name N, err errs.Err) { | ||
ent := &errEntry[N]{name: name, err: err} | ||
if ag.errLast == nil { | ||
ag.errHead = ent | ||
ag.errLast = ent | ||
} else { | ||
ag.errLast.next = ent | ||
ag.errLast = ent | ||
} | ||
} | ||
|
||
func (ag *asyncGroupAsync[N]) hasErr() bool { | ||
return (ag.errHead != nil) | ||
} | ||
|
||
func (ag *asyncGroupAsync[N]) makeErrs() map[N]errs.Err { | ||
m := make(map[N]errs.Err) | ||
for ent := ag.errHead; ent != nil; ent = ent.next { | ||
m[ent.name] = ent.err | ||
} | ||
return m | ||
} | ||
|
||
type asyncGroupSync struct { | ||
err errs.Err | ||
} | ||
|
||
func (ag *asyncGroupSync) Add(fn func() errs.Err) { | ||
ag.err = fn() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package sabi | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/sttk/sabi/errs" | ||
) | ||
|
||
func TestAsyncGroup_asyncGroupSync_ok(t *testing.T) { | ||
var ag asyncGroupSync | ||
assert.True(t, ag.err.IsOk()) | ||
|
||
exec := false | ||
fn := func() errs.Err { | ||
exec = true | ||
return errs.Ok() | ||
} | ||
|
||
ag.Add(fn) | ||
assert.True(t, ag.err.IsOk()) | ||
assert.True(t, exec) | ||
} | ||
|
||
func TestAsyncGroup_asyncGroupSync_error(t *testing.T) { | ||
var ag asyncGroupSync | ||
assert.True(t, ag.err.IsOk()) | ||
|
||
type FailToDoSomething struct{} | ||
|
||
exec := false | ||
fn := func() errs.Err { | ||
exec = true | ||
return errs.New(FailToDoSomething{}) | ||
} | ||
|
||
ag.Add(fn) | ||
switch ag.err.Reason().(type) { | ||
case FailToDoSomething: | ||
default: | ||
assert.Fail(t, ag.err.Error()) | ||
} | ||
assert.True(t, exec) | ||
} | ||
|
||
func TestAsyncGroup_asyncGroupAsync_ok(t *testing.T) { | ||
var ag asyncGroupAsync[string] | ||
assert.False(t, ag.hasErr()) | ||
|
||
exec := false | ||
fn := func() errs.Err { | ||
time.Sleep(50) | ||
exec = true | ||
return errs.Ok() | ||
} | ||
|
||
ag.name = "foo" | ||
ag.Add(fn) | ||
assert.False(t, ag.hasErr()) | ||
assert.False(t, exec) | ||
|
||
ag.wait() | ||
assert.False(t, ag.hasErr()) | ||
assert.True(t, exec) | ||
|
||
assert.Equal(t, len(ag.makeErrs()), 0) | ||
assert.True(t, exec) | ||
} | ||
|
||
func TestAsyncGroup_asyncGroupAsync_error(t *testing.T) { | ||
var ag asyncGroupAsync[string] | ||
assert.False(t, ag.hasErr()) | ||
|
||
type FailToDoSomething struct{} | ||
|
||
exec := false | ||
fn := func() errs.Err { | ||
time.Sleep(50) | ||
exec = true | ||
return errs.New(FailToDoSomething{}) | ||
} | ||
|
||
ag.name = "foo" | ||
ag.Add(fn) | ||
assert.False(t, ag.hasErr()) | ||
assert.False(t, exec) | ||
|
||
ag.wait() | ||
assert.True(t, ag.hasErr()) | ||
assert.True(t, exec) | ||
|
||
m := ag.makeErrs() | ||
assert.Equal(t, len(m), 1) | ||
switch m["foo"].Reason().(type) { | ||
case FailToDoSomething: | ||
default: | ||
assert.Fail(t, m["foo"].Error()) | ||
} | ||
assert.True(t, exec) | ||
} | ||
|
||
func TestAsyncGroup_asyncGroupAsync_multipleErrors(t *testing.T) { | ||
var ag asyncGroupAsync[string] | ||
assert.False(t, ag.hasErr()) | ||
|
||
type Err0 struct{} | ||
type Err1 struct{} | ||
type Err2 struct{} | ||
|
||
exec0 := false | ||
exec1 := false | ||
exec2 := false | ||
|
||
fn0 := func() errs.Err { | ||
time.Sleep(200) | ||
exec0 = true | ||
return errs.New(Err0{}) | ||
} | ||
fn1 := func() errs.Err { | ||
time.Sleep(400) | ||
exec1 = true | ||
return errs.New(Err1{}) | ||
} | ||
fn2 := func() errs.Err { | ||
time.Sleep(800) | ||
exec2 = true | ||
return errs.New(Err2{}) | ||
} | ||
|
||
ag.name = "foo0" | ||
ag.Add(fn0) | ||
ag.name = "foo1" | ||
ag.Add(fn1) | ||
ag.name = "foo2" | ||
ag.Add(fn2) | ||
assert.False(t, ag.hasErr()) | ||
assert.False(t, exec0) | ||
assert.False(t, exec1) | ||
assert.False(t, exec2) | ||
|
||
ag.wait() | ||
assert.True(t, ag.hasErr()) | ||
assert.True(t, exec0) | ||
assert.True(t, exec1) | ||
assert.True(t, exec2) | ||
|
||
m := ag.makeErrs() | ||
assert.Equal(t, len(m), 3) | ||
assert.Equal(t, m["foo0"].ReasonName(), "Err0") | ||
assert.Equal(t, m["foo1"].ReasonName(), "Err1") | ||
assert.Equal(t, m["foo2"].ReasonName(), "Err2") | ||
assert.True(t, exec0) | ||
assert.True(t, exec1) | ||
assert.True(t, exec2) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.