-
Notifications
You must be signed in to change notification settings - Fork 8
/
shm_test.go
50 lines (44 loc) · 931 Bytes
/
shm_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
// +build linux darwin freebsd
package shm
import (
"os"
"syscall"
"testing"
"github.com/pborman/uuid"
)
func TestCanCreateAndUseSharedRegion(t *testing.T) {
var (
expected = ([]byte)("a test")
name = "shm-test-" + uuid.New()
)
file, err := Open(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := file.Close(); err != nil {
t.Error(err)
}
if err := Unlink(file.Name()); err != nil {
t.Error(err)
}
}()
if err := syscall.Ftruncate(
int(file.Fd()), int64(len(expected)),
); err != nil {
t.Fatal(err)
}
if _, err := file.Write(expected); err != nil {
t.Fatal(err)
}
if err := file.Sync(); err != nil {
t.Fatal(err)
}
buf := make([]byte, len(expected))
if _, err := file.ReadAt(buf, 0); err != nil {
t.Fatal(err)
}
if string(buf) != string(expected) {
t.Fatalf("Expected %q. Got: %q", string(expected), string(buf))
}
}