-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
direct_io_test.go
72 lines (63 loc) · 1.4 KB
/
direct_io_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
package directio_test
import (
"bytes"
"io"
"io/ioutil"
"os"
"testing"
"github.com/ncw/directio"
)
func TestDirectIo(t *testing.T) {
// Make a temporary file name
fd, err := ioutil.TempFile("", "direct_io_test")
if err != nil {
t.Fatal("Failed to make temp file", err)
}
path := fd.Name()
fd.Close()
// starting block
block1 := directio.AlignedBlock(directio.BlockSize)
for i := 0; i < len(block1); i++ {
block1[i] = 'A'
}
// Write the file
out, err := directio.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
t.Fatal("Failed to directio.OpenFile for read", err)
}
_, err = out.Write(block1)
if err != nil {
t.Fatal("Failed to write", err)
}
err = out.Close()
if err != nil {
t.Fatal("Failed to close writer", err)
}
// Read the file
block2 := directio.AlignedBlock(directio.BlockSize)
in, err := directio.OpenFile(path, os.O_RDONLY, 0666)
if err != nil {
t.Fatal("Failed to directio.OpenFile for write", err)
}
_, err = io.ReadFull(in, block2)
if err != nil {
t.Fatal("Failed to read", err)
}
err = in.Close()
if err != nil {
t.Fatal("Failed to close reader", err)
}
// Tidy
err = os.Remove(path)
if err != nil {
t.Fatal("Failed to remove temp file", path, err)
}
// Compare
if !bytes.Equal(block1, block2) {
t.Fatal("Read not the same as written")
}
}
func TestZeroSizedBlock(t *testing.T) {
// This should not panic!
directio.AlignedBlock(0)
}