-
Notifications
You must be signed in to change notification settings - Fork 367
/
progress_channel_test.go
80 lines (67 loc) · 1.65 KB
/
progress_channel_test.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package copy
import (
"bytes"
"io"
"testing"
"time"
"github.com/containers/image/v5/types"
"github.com/stretchr/testify/assert"
)
func newSUT(
t *testing.T,
reader io.Reader,
duration time.Duration,
channel chan types.ProgressProperties,
) *progressReader {
artifact := types.BlobInfo{Size: 10}
go func() {
res := <-channel
assert.Equal(t, res.Event, types.ProgressEventNewArtifact)
assert.Equal(t, res.Artifact, artifact)
}()
res := newProgressReader(reader, channel, duration, artifact)
return res
}
func TestNewProgressReader(t *testing.T) {
// Given
channel := make(chan types.ProgressProperties)
sut := newSUT(t, nil, time.Second, channel)
assert.NotNil(t, sut)
// When/Then
go func() {
res := <-channel
assert.Equal(t, res.Event, types.ProgressEventDone)
}()
sut.reportDone()
}
func TestReadWithoutEvent(t *testing.T) {
// Given
channel := make(chan types.ProgressProperties)
reader := bytes.NewReader([]byte{0, 1, 2})
sut := newSUT(t, reader, time.Second, channel)
assert.NotNil(t, sut)
// When
b := []byte{0, 1, 2, 3, 4}
read, err := reader.Read(b)
// Then
assert.Nil(t, err)
assert.Equal(t, read, 3)
}
func TestReadWithEvent(t *testing.T) {
// Given
channel := make(chan types.ProgressProperties)
reader := bytes.NewReader([]byte{0, 1, 2, 3, 4, 5, 6})
sut := newSUT(t, reader, time.Nanosecond, channel)
assert.NotNil(t, sut)
b := []byte{0, 1, 2, 3, 4}
// When/Then
go func() {
res := <-channel
assert.Equal(t, res.Event, types.ProgressEventRead)
assert.Equal(t, res.Offset, uint64(5))
assert.Equal(t, res.OffsetUpdate, uint64(5))
}()
read, err := reader.Read(b)
assert.Equal(t, read, 5)
assert.Nil(t, err)
}