2022-07-04 23:52:54 +02:00
|
|
|
package gouring
|
|
|
|
|
|
|
|
import (
|
2022-07-07 03:26:44 +02:00
|
|
|
"bytes"
|
|
|
|
"os"
|
2022-07-04 23:52:54 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-07-07 03:26:44 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-07-04 23:52:54 +02:00
|
|
|
)
|
|
|
|
|
2022-07-07 18:23:00 +02:00
|
|
|
type genericTestingT interface {
|
|
|
|
assert.TestingT
|
|
|
|
require.TestingT
|
|
|
|
}
|
|
|
|
|
|
|
|
func testNewIoUring(t genericTestingT, entries uint32, flags uint32) *IoUring {
|
|
|
|
h, err := New(entries, flags)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, h)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2022-07-28 14:45:51 +02:00
|
|
|
func testNewIoUringWithParams(t genericTestingT, entries uint32, p *IoUringParams) *IoUring {
|
2022-07-07 18:23:00 +02:00
|
|
|
h, err := NewWithParams(entries, p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, h)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2022-07-07 03:26:44 +02:00
|
|
|
func TestRingWrapper(t *testing.T) {
|
|
|
|
h := testNewIoUring(t, 256, 0)
|
|
|
|
defer h.Close()
|
|
|
|
|
|
|
|
// O_RDWR|O_CREATE|O_EXCL
|
|
|
|
ftmp, err := os.CreateTemp(os.TempDir(), "test_iouring_ring_wrapper_*")
|
|
|
|
require.NoError(t, err)
|
|
|
|
fd := ftmp.Fd()
|
|
|
|
|
|
|
|
var whatToWrite = [][]byte{
|
|
|
|
[]byte("hello\n"),
|
|
|
|
[]byte("\tworld\n\n"),
|
|
|
|
[]byte("io_uring\t\t\n"),
|
|
|
|
[]byte("nice!\n!!!\n\x00"),
|
|
|
|
}
|
|
|
|
var off uint64 = 0
|
|
|
|
for _, bs := range whatToWrite {
|
2022-07-28 14:45:51 +02:00
|
|
|
sqe := h.GetSqe()
|
2022-07-07 03:26:44 +02:00
|
|
|
PrepWrite(sqe, int(fd), &bs[0], len(bs), off)
|
|
|
|
sqe.Flags = IOSQE_IO_LINK
|
|
|
|
off = off + uint64(len(bs))
|
|
|
|
}
|
|
|
|
submitted, err := h.SubmitAndWait(uint32(len(whatToWrite)))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(whatToWrite), int(submitted))
|
|
|
|
|
|
|
|
var readed = make([]byte, 1024)
|
|
|
|
nb, err := ftmp.Read(readed)
|
2022-07-04 23:52:54 +02:00
|
|
|
assert.NoError(t, err)
|
2022-07-07 03:26:44 +02:00
|
|
|
readed = readed[:nb]
|
|
|
|
|
|
|
|
joined := bytes.Join(whatToWrite, []byte{})
|
|
|
|
assert.Equal(t, joined, readed)
|
2022-07-04 23:52:54 +02:00
|
|
|
}
|