-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer_test.go
117 lines (90 loc) · 2.73 KB
/
writer_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package ctxio
import (
"bytes"
"context"
"io"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestExample(t *testing.T) {
content := "The ctxio package gives golang io.copy operations the ability to terminate with context and retrieve progress data."
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tempDir := t.TempDir()
srcFile, err := ioutil.TempFile(tempDir, "source-file-*")
t.Logf("srcFile: %s", srcFile.Name())
require.NoError(t, err)
srcFile.WriteString("The ctxio package gives golang io.copy operations the ability to terminate with context and retrieve progress data.")
srcFile.Sync()
defer srcFile.Close()
dstFile, err := ioutil.TempFile(tempDir, "destination-file-*")
t.Logf("dstFile: %s", dstFile.Name())
require.NoError(t, err)
defer dstFile.Close()
file, _ := os.Open(srcFile.Name())
progressFn := func(n int64) {
t.Logf("Progress bytes: %d", n)
}
w := NewWriter(ctx, dstFile, progressFn)
written, err := io.Copy(w, file)
t.Logf("written bytes: %d", written)
require.NoError(t, err)
expectedContent, err := os.ReadFile(dstFile.Name())
require.NoError(t, err)
require.Equal(t, content, string(expectedContent))
}
func TestNewWriter(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
buf := []byte("binalyze")
var progress int64
progressFn := func(n int64) {
t.Logf("written bytes: %d", n)
progress = n
}
b := bytes.NewBuffer(nil)
w := NewWriter(ctx, b, progressFn)
// Write first half
n, err := w.Write(buf[:4]) // b = "bina"
require.NoError(t, err)
require.NoError(t, w.Err())
require.Equal(t, 4, n)
require.Equal(t, int64(4), w.N())
require.Equal(t, b.String(), string(buf[:4]))
require.Equal(t, int64(4), progress)
// Write second half
n, err = w.Write(buf[4:8]) // b = "lyze"
require.NoError(t, err)
require.NoError(t, w.Err())
require.Equal(t, 4, n)
require.Equal(t, int64(8), w.N())
require.Equal(t, b.String(), string(buf))
require.Equal(t, int64(8), progress)
}
func TestContextCancel_Writer(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
buf := []byte("binalyze")
b := bytes.NewBuffer(nil)
var progress int64
progressFn := func(n int64) {
t.Logf("written bytes: %d", n)
progress = n
}
w := NewWriter(ctx, b, progressFn)
// Write first half
n, err := w.Write(buf[:4]) // b = "bina"
require.NoError(t, err)
require.NoError(t, w.Err())
require.Equal(t, 4, n)
require.Equal(t, b.String(), string(buf[:4]))
require.Equal(t, int64(4), progress)
cancel()
n, err = w.Write(buf[4:8]) // b = "lyze"
require.Error(t, context.Canceled, err)
require.Error(t, context.Canceled, w.Err())
require.Equal(t, 0, n)
require.Equal(t, int64(4), progress)
}