-
Notifications
You must be signed in to change notification settings - Fork 44
/
diskwriter_unix_test.go
117 lines (89 loc) · 2.71 KB
/
diskwriter_unix_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
//go:build !windows
// +build !windows
package fsutil
import (
"context"
"os"
"path/filepath"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sys/unix"
)
func TestWalkerWriterAsync(t *testing.T) {
d, err := tmpDir(changeStream([]string{
"ADD foo dir",
"ADD foo/foo1 file data1",
"ADD foo/foo2 file data2",
"ADD foo/foo3 file data3",
"ADD foo/foo4 file >foo/foo3",
"ADD foo5 file data5",
}))
assert.NoError(t, err)
defer os.RemoveAll(d)
dest := t.TempDir()
dw, err := NewDiskWriter(context.TODO(), dest, DiskWriterOpt{
AsyncDataCb: newWriteToFunc(d, 300*time.Millisecond),
})
assert.NoError(t, err)
st := time.Now()
err = Walk(context.Background(), d, nil, readAsAdd(dw.HandleChange))
assert.NoError(t, err)
err = dw.Wait(context.TODO())
assert.NoError(t, err)
dt, err := os.ReadFile(filepath.Join(dest, "foo/foo3"))
assert.NoError(t, err)
assert.Equal(t, "data3", string(dt))
dt, err = os.ReadFile(filepath.Join(dest, "foo/foo4"))
assert.NoError(t, err)
assert.Equal(t, "data3", string(dt))
fi1, err := os.Lstat(filepath.Join(dest, "foo/foo3"))
assert.NoError(t, err)
fi2, err := os.Lstat(filepath.Join(dest, "foo/foo4"))
assert.NoError(t, err)
stat1, ok1 := fi1.Sys().(*syscall.Stat_t)
stat2, ok2 := fi2.Sys().(*syscall.Stat_t)
if ok1 && ok2 {
assert.Equal(t, stat1.Ino, stat2.Ino)
}
dt, err = os.ReadFile(filepath.Join(dest, "foo5"))
assert.NoError(t, err)
assert.Equal(t, "data5", string(dt))
duration := time.Since(st)
assert.True(t, duration < 500*time.Millisecond)
}
func TestWalkerWriterDevices(t *testing.T) {
requiresRoot(t)
d, err := tmpDir(changeStream([]string{
"ADD foo dir",
"ADD foo/foo1 file data1",
}))
require.NoError(t, err)
err = unix.Mknod(filepath.Join(d, "foo/block"), syscall.S_IFBLK|0600, mkdev(2, 3))
require.NoError(t, err)
err = unix.Mknod(filepath.Join(d, "foo/char"), syscall.S_IFCHR|0400, mkdev(1, 9))
require.NoError(t, err)
dest := t.TempDir()
dw, err := NewDiskWriter(context.TODO(), dest, DiskWriterOpt{
SyncDataCb: newWriteToFunc(d, 0),
})
assert.NoError(t, err)
err = Walk(context.Background(), d, nil, readAsAdd(dw.HandleChange))
assert.NoError(t, err)
err = dw.Wait(context.TODO())
assert.NoError(t, err)
fi, err := os.Lstat(filepath.Join(dest, "foo/char"))
require.NoError(t, err)
stat, ok := fi.Sys().(*syscall.Stat_t)
require.True(t, ok)
assert.Equal(t, uint64(1), stat.Rdev>>8)
assert.Equal(t, uint64(9), stat.Rdev&0xff)
fi, err = os.Lstat(filepath.Join(dest, "foo/block"))
require.NoError(t, err)
stat, ok = fi.Sys().(*syscall.Stat_t)
require.True(t, ok)
assert.Equal(t, uint64(2), stat.Rdev>>8)
assert.Equal(t, uint64(3), stat.Rdev&0xff)
}