github.com/iceber/iouring-go@v0.0.0-20230403020409-002cfd2e2a90/iouring_test.go (about)

     1  package iouring
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  func testSubmitRequests(t *testing.T, nreqs uint) {
    10  	f, err := os.Open("/dev/zero") // For read access.
    11  	if err != nil {
    12  		t.Fatal(err)
    13  	}
    14  	defer f.Close()
    15  	iour, err := New(nreqs)
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	defer iour.Close()
    20  
    21  	fd := int(f.Fd())
    22  	for iter := 0; iter < 2; iter++ {
    23  		var offset uint64
    24  		preqs := make([]PrepRequest, nreqs)
    25  		bufs := make([][]byte, nreqs)
    26  		for i := range preqs {
    27  			bufs[i] = make([]byte, 2)
    28  			preqs[i] = Pread(fd, bufs[i], offset)
    29  			offset += 2
    30  		}
    31  
    32  		requests, err := iour.SubmitRequests(preqs, nil)
    33  		if err != nil {
    34  			t.Fatal(err)
    35  		}
    36  		<-requests.Done()
    37  		errResults := requests.ErrResults()
    38  		if errResults != nil {
    39  			t.Fatal(errResults[0].Err())
    40  		}
    41  	}
    42  }
    43  
    44  func TestSubmitRequests(t *testing.T) {
    45  	for i := uint(0); i < 8; i++ {
    46  		nreqs := uint(1 << i)
    47  		t.Run(fmt.Sprintf("%d", nreqs), func(t *testing.T) { testSubmitRequests(t, nreqs) })
    48  	}
    49  }