Skip to content

Commit

Permalink
feat: add wrapper test
Browse files Browse the repository at this point in the history
  • Loading branch information
daheige committed Jan 15, 2022
1 parent 89851fd commit 3a9c686
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
4 changes: 4 additions & 0 deletions chanwrap/chan_wrap_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type WrapImpl struct {
}

// New create wrapImpl entity
// If the wrapper using the chan method needs to specify the number of
// goroutines to be executed,the wrapper.WithBufCap method needs to be called.
// Otherwise, after the Wait method is executed, some goroutines
// will exit without execution.
func New(opts ...wrapper.Option) wrapper.Wrapper {
w := &WrapImpl{}

Expand Down
33 changes: 27 additions & 6 deletions chanwrap/chan_wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,37 @@ func mockRecovery() {
}

func TestWrapper(t *testing.T) {
var wg = New(wrapper.WithBufCap(2), wrapper.WithRecover(mockRecovery))
wg.Wrap(func() {
num := 10 * 10000
c := num + 2
chWrap := New(wrapper.WithBufCap(c), wrapper.WithRecover(mockRecovery))
chWrap.Wrap(func() {
log.Println("1111")
})

wg.WrapWithRecover(func() {
for i := 0; i < num; i++ {
// The method of copying is used here to avoid the i
// in the wrap func being the same variable
index := i
chWrap.Wrap(func() {
log.Printf("current index: %d\n", index)
})
}

chWrap.WrapWithRecover(func() {
log.Println(2222)
panic("mock panic test")
})
wg.Wait()

// time.Sleep(10 * time.Second)
chWrap.Wait()
}

/**
$ go test -v
2022/01/15 18:06:42 current index: 99992
2022/01/15 18:06:42 current index: 99996
2022/01/15 18:06:42 current index: 99998
2022/01/15 18:06:42 current index: 99997
2022/01/15 18:06:42 current index: 99999
2022/01/15 18:06:42 2222
2022/01/15 18:06:45 exec recover:mock panic test
--- PASS: TestWrapper (3.70s)
*/
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (
)

func main() {
// If the wrapper using the chan method needs to specify the number of
// goroutines to be executed,the wrapper.WithBufCap method needs to be called.
// Otherwise, after the Wait method is executed, some goroutines
// will exit without execution.
chWrap := factory.New(factory.ChWrapper, wrapper.WithBufCap(2))
chWrap.Wrap(func() {
log.Println("chan wrapper: 1111")
Expand Down
19 changes: 13 additions & 6 deletions waitgroup/waitgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ func mockRecovery() {
}

func TestWrapper(t *testing.T) {
var wg = New(wrapper.WithRecover(mockRecovery))
wg := New(wrapper.WithRecover(mockRecovery))
wg.Wrap(func() {
log.Println("1111")
})

for i := 0; i < 10*10000; i++ {
num := 10 * 10000
for i := 0; i < num; i++ {
// The method of copying is used here to avoid the i
// in the wrap func being the same variable
index := i
Expand All @@ -36,9 +37,15 @@ func TestWrapper(t *testing.T) {
}

/*
2022/01/15 17:58:32 current index: 99998
2022/01/15 17:58:32 current index: 99999
2022/01/15 17:58:32 exec recover:mock panic test
--- PASS: TestWrapper (4.66s)
$ go test -v
2022/01/15 18:15:50 current index: 99993
2022/01/15 18:15:50 current index: 99994
2022/01/15 18:15:50 exec recover:mock panic test
2022/01/15 18:15:50 current index: 99991
2022/01/15 18:15:50 current index: 99995
2022/01/15 18:15:50 current index: 99999
2022/01/15 18:15:50 current index: 99997
2022/01/15 18:15:50 current index: 99998
--- PASS: TestWrapper (4.06s)
PASS
*/

0 comments on commit 3a9c686

Please sign in to comment.