Skip to content

Commit

Permalink
extract handleTime method; add unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
sfwn committed Aug 6, 2024
1 parent 2c349d0 commit 62b09e0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
30 changes: 17 additions & 13 deletions internal/tools/pipeline/spec/pipeline_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,27 @@ func (pt *PipelineTask) Convert2DTO() *apistructs.PipelineTaskDTO {
return &task
}

func (pt *PipelineTask) handleTime() {
// handle time
if !pt.TimeEnd.IsZero() {
if pt.TimeBegin.IsZero() {
earlierTime := pt.TimeUpdated // use earlier time as timeBegin as much as possible
if pt.TimeEnd.Before(pt.TimeUpdated) {
earlierTime = pt.TimeEnd
}
pt.TimeBegin = earlierTime // for some scenarios, timeBegin is not set
}
if pt.CostTimeSec < 0 {
pt.CostTimeSec = int64(pt.TimeEnd.Sub(pt.TimeBegin).Seconds())
}
}
}

func (pt *PipelineTask) Convert2PB() *basepb.PipelineTaskDTO {
if pt == nil {
return nil
}
pt.handleTime()

Check warning on line 373 in internal/tools/pipeline/spec/pipeline_task.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/pipeline/spec/pipeline_task.go#L373

Added line #L373 was not covered by tests
task := basepb.PipelineTaskDTO{
ID: pt.ID,
PipelineID: pt.PipelineID,
Expand All @@ -378,19 +395,6 @@ func (pt *PipelineTask) Convert2PB() *basepb.PipelineTaskDTO {

IsSnippet: pt.IsSnippet,
}
// handle time
if task.TimeEnd != nil && !task.TimeEnd.AsTime().IsZero() {
if task.TimeBegin == nil {
earlierTime := pt.TimeUpdated // use earlier time as timeBegin as much as possible
if pt.TimeEnd.Before(pt.TimeUpdated) {
earlierTime = pt.TimeEnd
}
task.TimeBegin = timestamppb.New(earlierTime) // for some scenarios, timeBegin is not set
}
if task.CostTimeSec < 0 {
task.CostTimeSec = int64(task.TimeEnd.AsTime().Sub(task.TimeBegin.AsTime()).Seconds())
}
}
if pt.SnippetPipelineID != nil {
task.SnippetPipelineID = pt.SnippetPipelineID
}
Expand Down
31 changes: 30 additions & 1 deletion internal/tools/pipeline/spec/pipeline_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"testing"
"time"

"github.com/magiconair/properties/assert"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"

"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/internal/tools/pipeline/pkg/taskerror"
Expand Down Expand Up @@ -316,3 +316,32 @@ func TestMergeTaskParamDetailToDisplay(t *testing.T) {
assert.Equal(t, params[1].Values[apistructs.MergedTaskParamSource.String()], EncryptedValueDisplay)
assert.Equal(t, params[2].Values[apistructs.MergedTaskParamSource.String()], EncryptedValueDisplay)
}

func TestPipelineTask_handleTime(t *testing.T) {
now := time.Now()
timeEnd := now
timeUpdatedBeforeTimeEnd := now.Add(-time.Minute)
timeUpdatedAfterTimeEnd := now.Add(time.Minute)

// timeUpdated < timeEnd
task := PipelineTask{
TimeBegin: time.Time{},
TimeEnd: timeEnd,
TimeUpdated: timeUpdatedBeforeTimeEnd,
CostTimeSec: -1,
}
task.handleTime()
assert.Equal(t, task.TimeBegin, timeUpdatedBeforeTimeEnd)
assert.Equal(t, float64(task.CostTimeSec), timeEnd.Sub(timeUpdatedBeforeTimeEnd).Seconds())

// timeUpdated > timeEnd
task = PipelineTask{
TimeBegin: time.Time{},
TimeEnd: timeEnd,
TimeUpdated: timeUpdatedAfterTimeEnd,
CostTimeSec: -1,
}
task.handleTime()
assert.Equal(t, task.TimeBegin, timeEnd)
assert.Equal(t, float64(task.CostTimeSec), timeEnd.Sub(timeEnd).Seconds())
}

0 comments on commit 62b09e0

Please sign in to comment.