hslam_msg/msg_test.go

50 lines
836 B
Go
Raw Normal View History

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)
m := &Msg{Type: 1, Text: []byte(context)}
err = Snd(msgid, m, 0600)
if err != nil {
panic(err)
}
time.Sleep(time.Second * 2)
close(done)
}()
time.Sleep(time.Second)
key, err := ftok.Ftok("/tmp", 0x22)
if err != nil {
panic(err)
}
msgid, err := Get(key, 0600)
if err != nil {
panic(err)
}
m := &Msg{Type: 1}
err = Rcv(msgid, m, 0600)
if err != nil {
panic(err)
}
if context != string(m.Text) {
t.Error(context, string(m.Text))
}
<-done
}