mirror of https://github.com/hslam/ipc.git
98 lines
2.1 KiB
Go
98 lines
2.1 KiB
Go
// Copyright (c) 2020 Meng Huang (mhboy@outlook.com)
|
|
// This package is licensed under a MIT license that can be found in the LICENSE file.
|
|
|
|
package ipc
|
|
|
|
import (
|
|
"github.com/hslam/sem"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestIPC(t *testing.T) {
|
|
context := strings.Repeat("1", 64)
|
|
done := make(chan struct{})
|
|
semnum := 0
|
|
go func() {
|
|
key, _ := Ftok("/tmp", 0x22)
|
|
semid, err := Semget(key, 1, 0600)
|
|
if err != nil {
|
|
semid, err = Semget(key, 1, IPC_CREAT|IPC_EXCL|0600)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
_, err := Semsetvalue(semid, semnum, 1)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
defer Semrm(semid)
|
|
shmid, data, _ := Shmgetat(key, 128, IPC_CREAT|0600)
|
|
defer Shmrm(shmid)
|
|
defer Shmdt(data)
|
|
msgid, _ := Msgget(key, IPC_CREAT|0600)
|
|
defer Msgrm(msgid)
|
|
if _, err := Semp(semid, semnum, SEM_UNDO); err != nil {
|
|
return
|
|
}
|
|
copy(data, context)
|
|
if _, err := Semv(semid, semnum, SEM_UNDO); err != nil {
|
|
return
|
|
}
|
|
if err := Msgsnd(msgid, 1, []byte{byte(len(context))}, 0600); err != nil {
|
|
return
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 200)
|
|
close(done)
|
|
}()
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
key, _ := Ftok("/tmp", 0x22)
|
|
semid, err := Semget(key, 1, 0600)
|
|
if err != nil {
|
|
semid, err = Semget(key, 1, IPC_CREAT|IPC_EXCL|0600)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
_, err := Semsetvalue(semid, semnum, 1)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
defer Semrm(semid)
|
|
shmid, data, _ := Shmgetat(key, 128, 0600)
|
|
defer Shmrm(shmid)
|
|
defer Shmdt(data)
|
|
msgid, _ := Msgget(key, 0600)
|
|
defer Msgrm(msgid)
|
|
|
|
m, err := Msgrcv(msgid, 1, 0600)
|
|
if err != nil {
|
|
return
|
|
}
|
|
length := int(m[0])
|
|
if ret, err := Semgetvalue(semid, semnum); err != nil {
|
|
t.Error(err)
|
|
} else if ret < 1 {
|
|
t.Error()
|
|
}
|
|
var sops [1]sem.Sembuf
|
|
sops[0] = sem.Sembuf{SemNum: uint16(semnum), SemFlg: SEM_UNDO}
|
|
sops[0].SemOp = -1
|
|
if _, err := Semop(semid, sops[:]); err != nil {
|
|
return
|
|
}
|
|
text := string(data[:length])
|
|
sops[0].SemOp = 1
|
|
if _, err := Semop(semid, sops[:]); err != nil {
|
|
return
|
|
}
|
|
|
|
if context != string(text) {
|
|
t.Error(context, string(text))
|
|
}
|
|
<-done
|
|
}
|