Skip to content

Commit

Permalink
feat: export Statistics.Add
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfogre committed Mar 9, 2020
1 parent 1cccdf5 commit bb35d6f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (c *Cron) Hostname() string {
func (c *Cron) Statistics() Statistics {
ret := Statistics{}
for _, j := range c.jobs {
ret.add(j.statistics)
ret = ret.Add(j.statistics)
}
return ret
}
Expand Down
4 changes: 3 additions & 1 deletion statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type Statistics struct {
RetriedRun int64
}

func (s *Statistics) add(delta Statistics) {
// Add return a new Statistics with two added.
func (s Statistics) Add(delta Statistics) Statistics {
s.TotalTask += delta.TotalTask
s.PassedTask += delta.PassedTask
s.FailedTask += delta.FailedTask
Expand All @@ -24,4 +25,5 @@ func (s *Statistics) add(delta Statistics) {
s.PassedRun += delta.PassedRun
s.FailedRun += delta.FailedRun
s.RetriedRun += delta.RetriedRun
return s
}
86 changes: 86 additions & 0 deletions statistics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package dcron

import (
"reflect"
"testing"
)

func TestStatistics_Add(t *testing.T) {
type fields struct {
TotalTask int64
PassedTask int64
FailedTask int64
SkippedTask int64
MissedTask int64
TotalRun int64
PassedRun int64
FailedRun int64
RetriedRun int64
}
type args struct {
delta Statistics
}
tests := []struct {
name string
fields fields
args args
want Statistics
}{
{
name: "regular",
fields: fields{
TotalTask: 1,
PassedTask: 2,
FailedTask: 3,
SkippedTask: 4,
MissedTask: 5,
TotalRun: 6,
PassedRun: 7,
FailedRun: 8,
RetriedRun: 9,
},
args: args{
delta: Statistics{
TotalTask: 1,
PassedTask: 2,
FailedTask: 3,
SkippedTask: 4,
MissedTask: 5,
TotalRun: 6,
PassedRun: 7,
FailedRun: 8,
RetriedRun: 9,
},
},
want: Statistics{
TotalTask: 2,
PassedTask: 4,
FailedTask: 6,
SkippedTask: 8,
MissedTask: 10,
TotalRun: 12,
PassedRun: 14,
FailedRun: 16,
RetriedRun: 18,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := Statistics{
TotalTask: tt.fields.TotalTask,
PassedTask: tt.fields.PassedTask,
FailedTask: tt.fields.FailedTask,
SkippedTask: tt.fields.SkippedTask,
MissedTask: tt.fields.MissedTask,
TotalRun: tt.fields.TotalRun,
PassedRun: tt.fields.PassedRun,
FailedRun: tt.fields.FailedRun,
RetriedRun: tt.fields.RetriedRun,
}
if got := s.Add(tt.args.delta); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Add() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit bb35d6f

Please sign in to comment.