Skip to content

Commit 382d9da

Browse files
lvan100lianghuan
authored andcommitted
111
1 parent 2e4cdea commit 382d9da

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

util/goutil/goutil_test.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"testing"
23+
"time"
2324

2425
"github.com/go-spring/spring-base/testing/assert"
2526
"github.com/go-spring/spring-core/util/goutil"
@@ -39,45 +40,54 @@ func TestGo(t *testing.T) {
3940
assert.That(t, s).Equal("hello world!")
4041
}
4142

42-
func TestGoFunc(t *testing.T) {
43-
44-
var s string
45-
goutil.GoFunc(func() {
46-
panic("something is wrong")
47-
}).Wait()
48-
assert.That(t, s).Equal("")
49-
50-
goutil.GoFunc(func() {
51-
s = "hello world!"
52-
}).Wait()
53-
assert.That(t, s).Equal("hello world!")
54-
}
55-
5643
func TestGoValue(t *testing.T) {
5744

5845
s, err := goutil.GoValue(t.Context(), func(ctx context.Context) (string, error) {
5946
panic("something is wrong")
6047
}).Wait()
6148
assert.That(t, s).Equal("")
62-
assert.That(t, err).Equal(errors.New("panic occurred"))
49+
assert.Error(t, err).Matches("panic recovered: .*")
50+
51+
i, err := goutil.GoValue(t.Context(), func(ctx context.Context) (int, error) {
52+
return 42, nil
53+
}).Wait()
54+
assert.That(t, err).Nil()
55+
assert.That(t, i).Equal(42)
6356

6457
s, err = goutil.GoValue(t.Context(), func(ctx context.Context) (string, error) {
6558
return "hello world!", nil
6659
}).Wait()
67-
assert.That(t, s).Equal("hello world!")
6860
assert.That(t, err).Nil()
61+
assert.That(t, s).Equal("hello world!")
6962

7063
var arr []*goutil.ValueStatus[int]
7164
for i := range 3 {
7265
arr = append(arr, goutil.GoValue(t.Context(), func(ctx context.Context) (int, error) {
7366
return i, nil
7467
}))
7568
}
76-
77-
var v int
7869
for i, g := range arr {
79-
v, err = g.Wait()
70+
v, err := g.Wait()
8071
assert.That(t, v).Equal(i)
8172
assert.That(t, err).Nil()
8273
}
74+
75+
expectedErr := errors.New("expected error")
76+
_, err = goutil.GoValue(t.Context(), func(ctx context.Context) (string, error) {
77+
return "", expectedErr
78+
}).Wait()
79+
assert.That(t, err).Equal(expectedErr)
80+
81+
t.Run("context cancel", func(t *testing.T) {
82+
ctx, cancel := context.WithCancel(t.Context())
83+
go func() {
84+
time.Sleep(10 * time.Millisecond)
85+
cancel()
86+
}()
87+
_, err := goutil.GoValue(ctx, func(ctx context.Context) (string, error) {
88+
time.Sleep(100 * time.Millisecond)
89+
return "done", nil
90+
}).Wait()
91+
assert.That(t, err).Nil()
92+
})
8393
}

0 commit comments

Comments
 (0)