File: progress_channel_test.go

package info (click to toggle)
golang-github-containers-image 5.28.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,104 kB
  • sloc: sh: 194; makefile: 73
file content (80 lines) | stat: -rw-r--r-- 1,694 bytes parent folder | download | duplicates (5)
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)

}