-
Notifications
You must be signed in to change notification settings - Fork 18
/
msgrcv_test.go
114 lines (97 loc) · 2.27 KB
/
msgrcv_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
package ipc_test
import (
"bytes"
"fmt"
"log"
"syscall"
"testing"
"time"
"github.com/siadat/ipc"
"github.com/stretchr/testify/assert"
)
func TestMsgrcv(t *testing.T) {
mykey, err := ipc.Ftok("/dev/null", 42)
if err != nil {
panic(fmt.Sprintf("Failed to generate key: %s\n", err))
} else {
fmt.Printf("Generate key %d\n", mykey)
}
qid, err := ipc.Msgget(mykey, ipc.IPC_CREAT|0600)
if err != nil {
panic(fmt.Sprintf("Failed to create ipc key %d: %s\n", mykey, err))
} else {
fmt.Printf("Create ipc queue id %d\n", qid)
}
defer func() {
err = ipc.Msgctl(qid, ipc.IPC_RMID)
if err != nil {
t.Fatal(err)
}
}()
input := []byte{0x18, 0x2d, 0x44, 0x00, 0xfb, 0x21, 0x09, 0x40}
msg := ipc.Msgbuf{Mtype: 12, Mtext: input}
err = ipc.Msgsnd(qid, &msg, 0)
if err != nil {
panic(fmt.Sprintf("Failed to send message to ipc id %d: %s\n", qid, err))
} else {
fmt.Printf("Message %x send to ipc id %d\n", input, qid)
}
var qbuf ipc.Msgbuf
err = ipc.Msgrcv(qid, &qbuf, 0)
if err != nil {
panic(fmt.Sprintf("Failed to receive message to ipc id %d: %s\n", qid, err))
} else {
fmt.Printf("Message %x receive to ipc id %d\n", qbuf.Mtext, qid)
}
if !bytes.Equal(input, qbuf.Mtext) {
t.Errorf("Input = %v, want %v", qbuf.Mtext, input)
}
assert.Equal(t, 12, int(qbuf.Mtype))
}
func TestMsgrcvBlocks(t *testing.T) {
keyFunc := func(path string, id uint) uint {
key, err := ipc.Ftok(path, id)
if err != nil {
t.Fatal(err)
}
return key
}
cases := []struct {
key uint
perm int
}{
{keyFunc("/dev/null", uint('m')), 0600},
}
for _, tt := range cases {
qid, err := ipc.Msgget(tt.key, ipc.IPC_CREAT|ipc.IPC_EXCL|tt.perm)
if err == syscall.EEXIST {
t.Errorf("queue with key 0x%x exists", tt.key)
}
if err != nil {
t.Fatal(err)
}
defer func() {
err = ipc.Msgctl(qid, ipc.IPC_RMID)
if err != nil {
t.Fatal(err)
}
}()
qbuf := &ipc.Msgbuf{Mtype: 12}
ch := make(chan struct{})
go func() {
err = ipc.Msgrcv(qid, qbuf, 0)
if err == syscall.EIDRM {
// OK, queue was removed
} else if err != nil {
log.Fatalf("syscall error: %v", err)
}
ch <- struct{}{}
}()
select {
case <-time.After(100 * time.Millisecond):
// OK, Msgrvc should block
case <-ch:
t.Fatal("msgrcv did not block")
}
}
}