feat: addes examples folder

This commit is contained in:
espoal 2022-10-11 22:51:18 +02:00
parent 0e1a2a1776
commit b6cd047a28
5 changed files with 88 additions and 3 deletions

43
examples/read.go Normal file
View file

@ -0,0 +1,43 @@
package main
import (
"github.com/ii64/gouring"
"golang.org/x/sys/unix"
"log"
)
func main() {
h, err := gouring.New(256, 0)
if err != nil {
log.Fatal(err)
}
defer h.Close()
fd, err := unix.Open("/tmp/test", unix.O_RDWR, 0677)
sqe := h.GetSqe()
b := make([]byte, 20)
gouring.PrepRead(sqe, fd, &b[0], len(b), 0)
log.Println("Buffer: ", b)
submitted, err := h.SubmitAndWait(1)
if err != nil {
log.Fatal(err)
}
println(submitted) // 1
var cqe *gouring.IoUringCqe
err = h.WaitCqe(&cqe)
if err != nil {
log.Fatal(err)
} // check also EINTR
log.Println("CQE: ", cqe)
log.Println("Buffer: ", b)
log.Println("Buffer: ", string(b))
_ = cqe.UserData
_ = cqe.Res
_ = cqe.Flags
}

37
examples/write.go Normal file
View file

@ -0,0 +1,37 @@
package main
import (
"github.com/ii64/gouring"
"golang.org/x/sys/unix"
"log"
)
func main() {
h, err := gouring.New(256, 0)
if err != nil {
log.Fatal(err)
}
defer h.Close()
fd, err := unix.Open("/tmp/test", unix.O_RDWR, 0677)
sqe := h.GetSqe()
b := []byte("hello from io_uring!\n")
gouring.PrepWrite(sqe, fd, &b[0], len(b), 0)
submitted, err := h.SubmitAndWait(1)
if err != nil { /*...*/
}
println(submitted) // 1
var cqe *gouring.IoUringCqe
err = h.WaitCqe(&cqe)
if err != nil {
log.Fatal(err)
} // check also EINTR
_ = cqe.UserData
_ = cqe.Res
_ = cqe.Flags
}

7
go.mod
View file

@ -2,10 +2,13 @@ module github.com/ii64/gouring
go 1.18
require github.com/stretchr/testify v1.7.0
require (
github.com/davecgh/go-spew v1.1.0
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20221010170243-090e33056c14
)
require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)

2
go.sum
View file

@ -5,6 +5,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 h1:k5II8e6QD8mITdi+okbbmR/cIyEbeXLBhy5Ha4nevyc=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=

View file

@ -151,7 +151,7 @@ func TestRingQueueSubmitSingleConsumer(t *testing.T) {
}
submit := func(t *testing.T, opt *IoUringParams, h *IoUring, expectedSubmitCount int) {
submitted, err := h.io_uringn_submit()
submitted, err := h.io_uring_submit()
assert.NoError(t, err)
if opt.Flags&IORING_SETUP_SQPOLL == 0 {
assert.Equal(t, expectedSubmitCount, submitted)