github.com/pdmccormick/importable-docker-buildx@v0.0.0-20240426161518-e47091289030/util/progress/metricwriter_test.go (about)

     1  package progress
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestCalculateIdleTime(t *testing.T) {
    11  	for _, tt := range []struct {
    12  		started   []int64
    13  		completed []int64
    14  		ms        int64
    15  	}{
    16  		{
    17  			started:   []int64{0, 1, 3},
    18  			completed: []int64{2, 10, 5},
    19  			ms:        0,
    20  		},
    21  		{
    22  			started:   []int64{0, 3},
    23  			completed: []int64{2, 5},
    24  			ms:        1,
    25  		},
    26  		{
    27  			started:   []int64{3, 0, 7},
    28  			completed: []int64{5, 2, 10},
    29  			ms:        3,
    30  		},
    31  	} {
    32  		started := unixMillis(tt.started...)
    33  		completed := unixMillis(tt.completed...)
    34  
    35  		actual := int64(calculateIdleTime(started, completed) / time.Millisecond)
    36  		assert.Equal(t, tt.ms, actual)
    37  	}
    38  }
    39  
    40  func unixMillis(ts ...int64) []time.Time {
    41  	times := make([]time.Time, len(ts))
    42  	for i, ms := range ts {
    43  		times[i] = time.UnixMilli(ms)
    44  	}
    45  	return times
    46  }