routines_test.go 478 B

123456789101112131415161718192021222324252627282930313233343536
  1. package threading
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/tal-tech/go-zero/core/lang"
  8. )
  9. func TestRoutineId(t *testing.T) {
  10. assert.True(t, RoutineId() > 0)
  11. }
  12. func TestRunSafe(t *testing.T) {
  13. log.SetOutput(ioutil.Discard)
  14. i := 0
  15. defer func() {
  16. assert.Equal(t, 1, i)
  17. }()
  18. ch := make(chan lang.PlaceholderType)
  19. go RunSafe(func() {
  20. defer func() {
  21. ch <- lang.Placeholder
  22. }()
  23. panic("panic")
  24. })
  25. <-ch
  26. i++
  27. }