mirror of
https://github.com/go-gitea/gitea.git
synced 2025-08-24 01:24:08 +09:00
feat: calculate duration
This commit is contained in:
@ -6,6 +6,9 @@ package actions
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -31,3 +34,57 @@ func TestLogIndexes_ToDB(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_calculateDuration(t *testing.T) {
|
||||
oldTimeSince := timeSince
|
||||
defer func() {
|
||||
timeSince = oldTimeSince
|
||||
}()
|
||||
|
||||
timeSince = func(t time.Time) time.Duration {
|
||||
return timeutil.TimeStamp(1000).AsTime().Sub(t)
|
||||
}
|
||||
type args struct {
|
||||
started timeutil.TimeStamp
|
||||
stopped timeutil.TimeStamp
|
||||
status Status
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want time.Duration
|
||||
}{
|
||||
{
|
||||
name: "unknown",
|
||||
args: args{
|
||||
started: 0,
|
||||
stopped: 0,
|
||||
status: StatusUnknown,
|
||||
},
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "running",
|
||||
args: args{
|
||||
started: 500,
|
||||
stopped: 0,
|
||||
status: StatusRunning,
|
||||
},
|
||||
want: 500 * time.Second,
|
||||
},
|
||||
{
|
||||
name: "done",
|
||||
args: args{
|
||||
started: 500,
|
||||
stopped: 600,
|
||||
status: StatusSuccess,
|
||||
},
|
||||
want: 100 * time.Second,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equalf(t, tt.want, calculateDuration(tt.args.started, tt.args.stopped, tt.args.status), "calculateDuration(%v, %v, %v)", tt.args.started, tt.args.stopped, tt.args.status)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user