2020-11-27 18:37:59 +08:00
|
|
|
package msg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hslam/ftok"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMsg(t *testing.T) {
|
2020-11-28 16:04:08 +08:00
|
|
|
context := strings.Repeat("1", 128)
|
2020-11-27 18:37:59 +08:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
key, err := ftok.Ftok("/tmp", 0x22)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
msgid, err := Get(key, IPC_CREAT|0600)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer Remove(msgid)
|
2020-12-01 16:57:00 +08:00
|
|
|
err = Send(msgid, 1, []byte(context), 0600)
|
2020-11-27 18:37:59 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-30 18:06:51 +08:00
|
|
|
time.Sleep(time.Millisecond * 200)
|
2020-11-27 18:37:59 +08:00
|
|
|
close(done)
|
|
|
|
}()
|
2020-11-30 18:06:51 +08:00
|
|
|
time.Sleep(time.Millisecond * 100)
|
2020-11-27 18:37:59 +08:00
|
|
|
key, err := ftok.Ftok("/tmp", 0x22)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
msgid, err := Get(key, 0600)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-12-01 16:57:00 +08:00
|
|
|
text, err := Receive(msgid, 1, 0600)
|
2020-11-27 18:37:59 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-29 01:54:53 +08:00
|
|
|
if context != string(text) {
|
|
|
|
t.Error(context, string(text))
|
2020-11-27 18:37:59 +08:00
|
|
|
}
|
|
|
|
<-done
|
|
|
|
}
|