Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Sep 8, 2023
1 parent 12521ec commit 646e531
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
3 changes: 2 additions & 1 deletion frame/g/g_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ func Test_Go(t *testing.T) {
)
wg.Add(1)
g.Go(context.Background(), func(ctx context.Context) {
defer wg.Done()
array.Append(1)
}, nil)
wg.Done()
wg.Wait()
t.Assert(array.Len(), 1)
})
}
20 changes: 11 additions & 9 deletions util/gutil/gutil_goroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ func Go(ctx context.Context, goroutineFunc Func, recoverFunc RecoverFunc) {
if goroutineFunc == nil {
return
}
defer func() {
if exception := recover(); exception != nil {
if recoverFunc != nil {
if v, ok := exception.(error); ok && gerror.HasStack(v) {
recoverFunc(ctx, v)
} else {
recoverFunc(ctx, gerror.NewCodef(gcode.CodeInternalPanic, "%+v", exception))
go func() {
defer func() {
if exception := recover(); exception != nil {
if recoverFunc != nil {
if v, ok := exception.(error); ok && gerror.HasStack(v) {
recoverFunc(ctx, v)
} else {
recoverFunc(ctx, gerror.NewCodef(gcode.CodeInternalPanic, "%+v", exception))
}
}
}
}
}()
goroutineFunc(ctx)
}()
goroutineFunc(ctx)
}
15 changes: 9 additions & 6 deletions util/gutil/gutil_z_unit_goroutine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ func Test_Go(t *testing.T) {
array = garray.NewArray(true)
)
wg.Add(1)
gutil.Go(context.Background(), func(ctx context.Context) {
gutil.Go(ctx, func(ctx context.Context) {
defer wg.Done()
array.Append(1)
}, nil)
wg.Done()
wg.Wait()
t.Assert(array.Len(), 1)
})
// recover
Expand All @@ -36,11 +37,12 @@ func Test_Go(t *testing.T) {
array = garray.NewArray(true)
)
wg.Add(1)
gutil.Go(context.Background(), func(ctx context.Context) {
gutil.Go(ctx, func(ctx context.Context) {
defer wg.Done()
panic("error")
array.Append(1)
}, nil)
wg.Done()
wg.Wait()
t.Assert(array.Len(), 0)
})
gtest.C(t, func(t *gtest.T) {
Expand All @@ -49,12 +51,13 @@ func Test_Go(t *testing.T) {
array = garray.NewArray(true)
)
wg.Add(1)
gutil.Go(context.Background(), func(ctx context.Context) {
gutil.Go(ctx, func(ctx context.Context) {
panic("error")
}, func(ctx context.Context, exception error) {
defer wg.Done()
array.Append(exception)
})
wg.Done()
wg.Wait()
t.Assert(array.Len(), 1)
t.Assert(array.At(0), "error")
})
Expand Down

0 comments on commit 646e531

Please sign in to comment.