1
0
mirror of https://github.com/hslam/shm.git synced 2025-04-25 13:48:57 +08:00
hslam_shm/shm_unix_test.go

127 lines
2.5 KiB
Go
Raw Normal View History

2020-11-27 02:01:38 +08:00
// 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 shm
import (
2020-11-28 17:25:41 +08:00
"github.com/hslam/ftok"
2020-11-27 02:01:38 +08:00
"github.com/hslam/mmap"
"os"
"testing"
"time"
)
func TestGetAt(t *testing.T) {
context := "Hello World"
done := make(chan struct{})
go func() {
2020-11-28 17:25:41 +08:00
key, err := ftok.Ftok("/tmp", 0x22)
if err != nil {
panic(err)
}
2020-12-01 16:05:01 +08:00
shmid, data, err := GetAttach(key, 128, IPC_CREAT|0600)
2020-11-27 02:01:38 +08:00
if err != nil {
t.Error(err)
}
defer Remove(shmid)
2020-12-01 16:05:01 +08:00
defer Detach(data)
2020-11-27 02:01:38 +08:00
copy(data, context)
2020-11-30 18:04:30 +08:00
time.Sleep(time.Millisecond * 200)
2020-11-27 02:01:38 +08:00
close(done)
}()
2020-11-30 18:04:30 +08:00
time.Sleep(time.Millisecond * 100)
2020-11-28 17:25:41 +08:00
key, err := ftok.Ftok("/tmp", 0x22)
if err != nil {
panic(err)
}
2020-12-01 16:05:01 +08:00
_, data, err := GetAttach(key, 128, 0600)
2020-11-27 02:01:38 +08:00
if err != nil {
t.Error(err)
}
2020-12-01 16:05:01 +08:00
defer Detach(data)
2020-11-27 02:01:38 +08:00
if context != string(data[:11]) {
t.Error(context, string(data[:11]))
}
<-done
}
func TestGetAtZeroFlag(t *testing.T) {
context := "Hello World"
done := make(chan struct{})
go func() {
2020-11-28 17:25:41 +08:00
key, err := ftok.Ftok("/tmp", 0x22)
if err != nil {
panic(err)
}
2020-12-01 16:05:01 +08:00
shmid, data, err := GetAttach(key, 128, 0)
2020-11-27 02:01:38 +08:00
if err != nil {
t.Error(err)
}
defer Remove(shmid)
2020-12-01 16:05:01 +08:00
defer Detach(data)
2020-11-27 02:01:38 +08:00
copy(data, context)
2020-11-30 18:04:30 +08:00
time.Sleep(time.Millisecond * 200)
2020-11-27 02:01:38 +08:00
close(done)
}()
2020-11-30 18:04:30 +08:00
time.Sleep(time.Millisecond * 100)
2020-11-28 17:25:41 +08:00
key, err := ftok.Ftok("/tmp", 0x22)
if err != nil {
panic(err)
}
2020-12-01 16:05:01 +08:00
_, data, err := GetAttach(key, 128, 0600)
2020-11-27 02:01:38 +08:00
if err != nil {
t.Error(err)
}
2020-12-01 16:05:01 +08:00
defer Detach(data)
2020-11-27 02:01:38 +08:00
if context != string(data[:11]) {
t.Error(context, string(data[:11]))
}
<-done
}
func TestOpen(t *testing.T) {
context := "Hello World"
done := make(chan struct{})
go func() {
name := "shared"
fd, err := Open(name, O_RDWR|O_CREATE, 0600)
if err != nil {
panic(err)
}
2020-11-28 17:25:41 +08:00
defer Unlink(name)
2020-11-27 02:01:38 +08:00
defer Close(fd)
length := 128
Ftruncate(fd, int64(length))
data, err := mmap.Open(fd, 0, length, mmap.READ|mmap.WRITE)
if err != nil {
panic(err)
}
defer mmap.Munmap(data)
copy(data, []byte("Hello World"))
2020-11-30 18:04:30 +08:00
time.Sleep(time.Millisecond * 200)
2020-11-27 02:01:38 +08:00
close(done)
}()
2020-11-30 18:04:30 +08:00
time.Sleep(time.Millisecond * 100)
2020-11-27 02:01:38 +08:00
fd, err := Open("shared", O_RDONLY, 0600)
if err != nil {
panic(err)
}
defer Close(fd)
data, err := mmap.Open(fd, 0, 128, mmap.READ)
if err != nil {
panic(err)
}
defer mmap.Munmap(data)
if context != string(data[:11]) {
t.Error(context, string(data[:11]))
}
<-done
}
func TestValidSize(t *testing.T) {
size := int64(os.Getpagesize()) * 2
if validSize(size) != size {
t.Error(validSize(size), size)
}
}