File: metricwriter_test.go

package info (click to toggle)
docker-buildx 0.19.3%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,852 kB
  • sloc: sh: 318; makefile: 73
file content (46 lines) | stat: -rw-r--r-- 849 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package progress

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestCalculateIdleTime(t *testing.T) {
	for _, tt := range []struct {
		started   []int64
		completed []int64
		ms        int64
	}{
		{
			started:   []int64{0, 1, 3},
			completed: []int64{2, 10, 5},
			ms:        0,
		},
		{
			started:   []int64{0, 3},
			completed: []int64{2, 5},
			ms:        1,
		},
		{
			started:   []int64{3, 0, 7},
			completed: []int64{5, 2, 10},
			ms:        3,
		},
	} {
		started := unixMillis(tt.started...)
		completed := unixMillis(tt.completed...)

		actual := int64(calculateIdleTime(started, completed) / time.Millisecond)
		assert.Equal(t, tt.ms, actual)
	}
}

func unixMillis(ts ...int64) []time.Time {
	times := make([]time.Time, len(ts))
	for i, ms := range ts {
		times[i] = time.UnixMilli(ms)
	}
	return times
}