1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-26 13:48:59 +08:00
shirou_gopsutil/test/process_test.go

121 lines
2.0 KiB
Go
Raw Normal View History

2014-05-20 16:38:20 +09:00
package test
import (
2014-04-25 16:38:23 +09:00
"os"
2014-04-24 01:07:15 +09:00
"runtime"
"testing"
2014-05-20 16:38:20 +09:00
"github.com/shirou/gopsutil"
)
2014-05-20 16:38:20 +09:00
func test_getProcess() gopsutil.Process {
checkPid := os.Getpid()
if runtime.GOOS == "windows" {
checkPid = 7960
}
2014-05-20 16:38:20 +09:00
ret, _ := gopsutil.NewProcess(int32(checkPid))
return *ret
}
func Test_Pids(t *testing.T) {
2014-05-20 16:38:20 +09:00
ret, err := gopsutil.Pids()
if err != nil {
t.Errorf("error %v", err)
}
if len(ret) == 0 {
t.Errorf("could not get pids %v", ret)
}
}
func Test_Pid_exists(t *testing.T) {
2014-04-30 16:16:07 +09:00
checkPid := 1
2014-04-24 01:07:15 +09:00
if runtime.GOOS == "windows" {
2014-04-30 16:16:07 +09:00
checkPid = 0
2014-04-24 01:07:15 +09:00
}
2014-05-20 16:38:20 +09:00
ret, err := gopsutil.PidExists(int32(checkPid))
if err != nil {
t.Errorf("error %v", err)
}
if ret == false {
t.Errorf("could not get init process %v", ret)
}
2014-04-23 17:57:47 +09:00
}
func Test_NewProcess(t *testing.T) {
2014-04-30 16:16:07 +09:00
checkPid := 1
2014-04-24 01:07:15 +09:00
if runtime.GOOS == "windows" {
2014-04-30 16:16:07 +09:00
checkPid = 0
2014-04-24 01:07:15 +09:00
}
2014-05-20 16:38:20 +09:00
ret, err := gopsutil.NewProcess(int32(checkPid))
2014-04-23 17:57:47 +09:00
if err != nil {
t.Errorf("error %v", err)
}
2014-05-20 16:38:20 +09:00
empty := &gopsutil.Process{}
2014-05-12 11:51:08 +09:00
if runtime.GOOS != "windows" { // Windows pid is 0
if empty == ret {
t.Errorf("error %v", ret)
}
}
}
func Test_Process_memory_maps(t *testing.T) {
2014-04-30 16:16:07 +09:00
checkPid := os.Getpid()
if runtime.GOOS == "windows" {
2014-04-30 16:16:07 +09:00
checkPid = 0
}
2014-05-20 16:38:20 +09:00
ret, err := gopsutil.NewProcess(int32(checkPid))
2014-04-30 15:32:05 +09:00
mmaps, err := ret.MemoryMaps(false)
if err != nil {
t.Errorf("memory map get error %v", err)
}
2014-05-20 16:38:20 +09:00
empty := gopsutil.MemoryMapsStat{}
for _, m := range *mmaps {
2014-05-12 11:51:08 +09:00
if m == empty {
t.Errorf("memory map get error %v", m)
}
}
}
func Test_Process_Ppid(t *testing.T) {
p := test_getProcess()
v, err := p.Ppid()
if err != nil {
t.Errorf("geting ppid error %v", err)
}
if v == 0 {
t.Errorf("return value is 0 %v", v)
}
}
func Test_Process_IOCounters(t *testing.T) {
p := test_getProcess()
v, err := p.IOCounters()
if err != nil {
t.Errorf("geting ppid error %v", err)
2014-05-01 18:46:40 +09:00
return
}
2014-05-20 16:38:20 +09:00
empty := &gopsutil.IOCountersStat{}
2014-05-12 11:51:08 +09:00
if v == empty {
t.Errorf("error %v", v)
}
}
func Test_Process_NumCtx(t *testing.T) {
p := test_getProcess()
_, err := p.NumCtxSwitches()
if err != nil {
t.Errorf("geting numctx error %v", err)
return
}
}