goroutine_test.go 683 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package util
  2. import (
  3. "sync"
  4. "testing"
  5. )
  6. /**
  7. 对于非闭包函数的支持
  8. */
  9. func TestGoWithRecover(t *testing.T) {
  10. wg := sync.WaitGroup{}
  11. x := 0
  12. wg.Add(1)
  13. GoWithRecover(func() {
  14. defer func() {
  15. wg.Done()
  16. }()
  17. t.Logf("run : %d\n", x)
  18. t.Log(10 / x)
  19. }, func(r interface{}) {
  20. t.Logf("find err: %s\n", r)
  21. })
  22. wg.Wait()
  23. }
  24. /**
  25. 不支持闭包函数循环结构
  26. */
  27. func TestGoWithRecoverBiBao(t *testing.T) {
  28. wg := sync.WaitGroup{}
  29. x := 0
  30. wg.Add(10)
  31. for n := 0; n < 10; n++ {
  32. GoWithRecover(func() {
  33. defer func() {
  34. wg.Done()
  35. }()
  36. t.Logf("run : %d\n", n)
  37. t.Log(10 / x)
  38. }, func(r interface{}) {
  39. t.Logf("find err: %s\n", r)
  40. })
  41. }
  42. wg.Wait()
  43. }