procfs_linux_test.go 707 B

123456789101112131415161718192021222324252627282930313233343536
  1. package procfs
  2. import (
  3. "os"
  4. "path/filepath"
  5. "regexp"
  6. "runtime"
  7. "testing"
  8. "gotest.tools/assert"
  9. )
  10. func TestPidOf(t *testing.T) {
  11. pids, err := PidOf(filepath.Base(os.Args[0]))
  12. assert.NilError(t, err)
  13. assert.Check(t, len(pids) == 1)
  14. assert.DeepEqual(t, pids[0], os.Getpid())
  15. }
  16. func BenchmarkGetPids(b *testing.B) {
  17. if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
  18. b.Skipf("not supported on GOOS=%s", runtime.GOOS)
  19. }
  20. re, err := regexp.Compile("(^|/)" + filepath.Base(os.Args[0]) + "$")
  21. assert.Check(b, err == nil)
  22. for i := 0; i < b.N; i++ {
  23. pids := getPids(re)
  24. b.StopTimer()
  25. assert.Check(b, len(pids) > 0)
  26. assert.Check(b, pids[0] == os.Getpid())
  27. b.StartTimer()
  28. }
  29. }