-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_posix_test.go
67 lines (59 loc) · 1.5 KB
/
env_posix_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
package ssdb
import (
"os"
"ssdb/util"
"testing"
)
const (
posixReadOnlyFileLimit = 4
posixMMapLimit = 4
)
func setPosixFileLimits(readOnlyFileLimit, mmapLimit int, t *testing.T) {
setPosixReadOnlyFDLimit(readOnlyFileLimit, t)
setPosixReadOnlyMMapLimit(mmapLimit, t)
}
func setPosixReadOnlyFDLimit(limit int, t *testing.T) {
util.AssertTrue(e == nil, "e == nil", t)
openReadOnlyFileLimit = limit
}
func setPosixReadOnlyMMapLimit(limit int, t *testing.T) {
util.AssertTrue(e == nil, "e == nil", t)
defaultMmapLimit = limit
}
func TestPosixOpenOnRead(t *testing.T) {
setPosixFileLimits(posixReadOnlyFileLimit, posixMMapLimit, t)
env := DefaultEnv()
testDir, err := env.GetTestDirectory()
if err != nil {
t.Fatal(err)
}
testFile := testDir + "/open_on_read.txt"
f, err := os.OpenFile(testFile, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
t.Fatal(err)
}
fileData := []byte("abcdefghijklmnopqrstuvwxyz")
if _, err = f.Write(fileData); err != nil {
t.Error(err)
}
_ = f.Close()
numFiles := posixReadOnlyFileLimit + posixMMapLimit + 5
files := make([]RandomAccessFile, numFiles)
for i := range files {
if files[i], err = env.NewRandomAccessFile(testFile); err != nil {
t.Error(err)
}
}
read := make([]byte, 1)
var result []byte
for i, file := range files {
if result, _, err = file.Read(read, int64(i)); err != nil {
t.Error(err)
}
util.AssertEqual(fileData[i], result[0], "read file", t)
}
files = nil
if err = env.DeleteFile(testFile); err != nil {
t.Error(err)
}
}