Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[incompatible] Reduce pointer from type Report #25

Merged
merged 5 commits into from
Feb 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions horenso.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ type Report struct {
Signaled bool `json:"signaled"`
Result string `json:"result"`
Hostname string `json:"hostname"`
Pid *int `json:"pid,omitempty"`
Pid int `json:"pid,omitempty"`
StartAt *time.Time `json:"startAt,omitempty"`
EndAt *time.Time `json:"endAt,omitempty"`
SystemTime *float64 `json:"systemTime,omitempty"`
UserTime *float64 `json:"userTime,omitempty"`
SystemTime float64 `json:"systemTime,omitempty"`
UserTime float64 `json:"userTime,omitempty"`
}

func (ho *horenso) openLog() (io.WriteCloser, error) {
Expand Down Expand Up @@ -112,7 +112,7 @@ func (ho *horenso) run(args []string) (Report, error) {
return ho.failReport(r, err.Error()), err
}
if cmd.Process != nil {
r.Pid = &cmd.Process.Pid
r.Pid = cmd.Process.Pid
}
done := make(chan error)
go func(r Report) {
Expand Down Expand Up @@ -148,12 +148,8 @@ func (ho *horenso) run(args []string) (Report, error) {
r.Stderr = bufStderr.String()
r.Output = bufMerged.String()
if p := cmd.ProcessState; p != nil {
durPtr := func(t time.Duration) *float64 {
f := float64(t) / float64(time.Second)
return &f
}
r.UserTime = durPtr(p.UserTime())
r.SystemTime = durPtr(p.SystemTime())
r.UserTime = float64(p.UserTime()) / float64(time.Second)
r.SystemTime = float64(p.SystemTime()) / float64(time.Second)
}
ho.runReporter(r)
<-done
Expand Down
21 changes: 17 additions & 4 deletions horenso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"reflect"
"testing"
"time"
)

func temp() string {
Expand Down Expand Up @@ -88,7 +89,7 @@ func TestRun(t *testing.T) {
}

nr := parseReport(noticeReport)
if *nr.Pid != *r.Pid {
if nr.Pid != r.Pid {
t.Errorf("something went wrong")
}
if nr.Output != "" {
Expand Down Expand Up @@ -169,7 +170,7 @@ func TestRunHugeOutput(t *testing.T) {
}

nr := parseReport(noticeReport)
if *nr.Pid != *r.Pid {
if nr.Pid != r.Pid {
t.Errorf("something went wrong")
}
if nr.Output != "" {
Expand All @@ -189,6 +190,16 @@ func TestRunHugeOutput(t *testing.T) {
}
}

func equalTimePtr(t1, t2 *time.Time) bool {
if t1 == nil && t2 == nil {
return true
}
if t1 == nil || t2 == nil {
return false
}
return (*t1).Equal(*t2)
}

func deepEqual(r1, r2 Report) bool {
return r1.Command == r2.Command &&
reflect.DeepEqual(r1.CommandArgs, r2.CommandArgs) &&
Expand All @@ -198,7 +209,9 @@ func deepEqual(r1, r2 Report) bool {
r1.Stderr == r2.Stderr &&
*r1.ExitCode == *r2.ExitCode &&
r1.Result == r2.Result &&
*r1.Pid == *r2.Pid &&
r1.Pid == r2.Pid &&
r1.Hostname == r2.Hostname &&
r1.Signaled == r2.Signaled
r1.Signaled == r2.Signaled &&
equalTimePtr(r1.StartAt, r2.StartAt) &&
equalTimePtr(r1.EndAt, r2.EndAt)
}