(t *testing.T)
| 47 | } |
| 48 | |
| 49 | func TestThreadStat(t *testing.T) { |
| 50 | // Pull process and thread stats. |
| 51 | proc, err := getProcFixtures(t).Proc(testPID) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | procStat, err := proc.Stat() |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | |
| 60 | threads, err := getProcFixtures(t).AllThreads(testPID) |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | sort.Sort(threads) |
| 65 | threadStats := make([]*ProcStat, len(threads)) |
| 66 | for i, thread := range threads { |
| 67 | threadStat, err := thread.Stat() |
| 68 | if err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | threadStats[i] = &threadStat |
| 72 | } |
| 73 | |
| 74 | // The following fields should be shared between the process and its thread: |
| 75 | procStatValue := reflect.ValueOf(procStat) |
| 76 | sharedFields := [...]string{ |
| 77 | "PPID", |
| 78 | "PGRP", |
| 79 | "Session", |
| 80 | "TTY", |
| 81 | "TPGID", |
| 82 | "VSize", |
| 83 | "RSS", |
| 84 | } |
| 85 | |
| 86 | for i, thread := range threads { |
| 87 | threadStatValue := reflect.ValueOf(threadStats[i]).Elem() |
| 88 | for _, f := range sharedFields { |
| 89 | if want, have := procStatValue.FieldByName(f), threadStatValue.FieldByName(f); want.Interface() != have.Interface() { |
| 90 | t.Errorf("TID %d, want %s %#v, have %#v", thread.PID, f, want, have) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Thread specific fields: |
| 96 | for i, thread := range threads { |
| 97 | if want, have := thread.PID, threadStats[i].PID; want != have { |
| 98 | t.Errorf("TID %d, want PID %d, have %d", thread.PID, want, have) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Finally exemplify the relationship between process and constituent |
| 103 | // threads CPU times: each the former ~ the sum of the corresponding |
| 104 | // latter. Require -v flag. |
| 105 | totalUTime, totalSTime := uint(0), uint(0) |
| 106 | for _, thread := range threads { |
nothing calls this directly
no test coverage detected
searching dependent graphs…