1
0
Fork 0
mirror of https://github.com/ii64/gouring.git synced 2025-04-26 05:42:48 +02:00

api: impl get sqe, wait cqe, and test

Signed-off-by: Nugraha <richiisei@gmail.com>
This commit is contained in:
Xeffy Chen 2022-07-07 08:26:44 +07:00
parent aa6bf08729
commit 7fa3c507b1
Signed by: Xeffy
GPG key ID: E41C08AD390E7C49
2 changed files with 55 additions and 7 deletions

View file

@ -2,14 +2,16 @@ package gouring
func New(entries uint32, flags uint32) (*IoUring, error) {
ring := &IoUring{}
err := io_uring_queue_init(entries, ring, flags)
p := new(IoUringParams)
p.Flags = flags
err := io_uring_queue_init_params(entries, ring, p)
if err != nil {
return nil, err
}
return ring, nil
}
func NewWithParamms(entries uint32, params *IoUringParams) (*IoUring, error) {
func NewWithParams(entries uint32, params *IoUringParams) (*IoUring, error) {
ring := &IoUring{}
err := io_uring_queue_init_params(entries, ring, params)
if err != nil {
@ -21,3 +23,19 @@ func NewWithParamms(entries uint32, params *IoUringParams) (*IoUring, error) {
func (h *IoUring) Close() {
h.io_uring_queue_exit()
}
func (h *IoUring) GetSQE() *IoUringSqe {
return h.io_uring_get_sqe()
}
func (h *IoUring) WaitCQE(cqePtr **IoUringCqe) error {
return h.io_uring_wait_cqe(cqePtr)
}
func (h *IoUring) Submit() (int, error) {
return h.io_uringn_submit()
}
func (h *IoUring) SubmitAndWait(waitNr uint32) (int, error) {
return h.io_uring_submit_and_wait(waitNr)
}

View file

@ -1,15 +1,45 @@
package gouring
import (
"bytes"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRingSetup(t *testing.T) {
h, err := New(256, 0)
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 {
sqe := h.GetSQE()
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)
assert.NoError(t, err)
assert.NotNil(t, h)
assert.NotEqual(t, 0, h.RingFd)
h.Close()
readed = readed[:nb]
joined := bytes.Join(whatToWrite, []byte{})
assert.Equal(t, joined, readed)
}