[Libguestfs] [PATCH libnbd v2 6/9] golang: tests: Use AioBuffer.Slice()

Nir Soffer nsoffer at redhat.com
Fri Feb 11 01:21:26 UTC 2022


Slice() is easier to use and faster than Get() or Bytes(). Let's use the
new way.

Signed-off-by: Nir Soffer <nsoffer at redhat.com>
---
 golang/libnbd_020_aio_buffer_test.go | 8 +++++---
 golang/libnbd_500_aio_pread_test.go  | 2 +-
 golang/libnbd_510_aio_pwrite_test.go | 8 +++++---
 3 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/golang/libnbd_020_aio_buffer_test.go b/golang/libnbd_020_aio_buffer_test.go
index 4b1c5f93..e07f8973 100644
--- a/golang/libnbd_020_aio_buffer_test.go
+++ b/golang/libnbd_020_aio_buffer_test.go
@@ -22,22 +22,23 @@ import (
 	"bytes"
 	"testing"
 )
 
 func TestAioBuffer(t *testing.T) {
 	/* Create a buffer with uninitialized backing array. */
 	buf := MakeAioBuffer(uint(32))
 	defer buf.Free()
 
 	/* Initialize backing array contents. */
+	s := buf.Slice()
 	for i := uint(0); i < buf.Size; i++ {
-		*buf.Get(i) = 0
+		s[i] = 0
 	}
 
 	/* Create a slice by copying the backing array contents into Go memory. */
 	b := buf.Bytes()
 
 	zeroes := make([]byte, 32)
 	if !bytes.Equal(b, zeroes) {
 		t.Fatalf("Expected %v, got %v", zeroes, buf.Bytes())
 	}
 
@@ -45,21 +46,21 @@ func TestAioBuffer(t *testing.T) {
 	for i := 0; i < len(b); i++ {
 		b[i] = 42
 	}
 
 	/* Bytes() still returns zeroes. */
 	if !bytes.Equal(buf.Bytes(), zeroes) {
 		t.Fatalf("Expected %v, got %v", zeroes, buf.Bytes())
 	}
 
 	/* Creating a slice without copying the underlying buffer. */
-	s := buf.Slice()
+	s = buf.Slice()
 	if !bytes.Equal(s, zeroes) {
 		t.Fatalf("Expected %v, got %v", zeroes, s)
 	}
 
 	/* Modifying the slice modifies the underlying buffer. */
 	for i := 0; i < len(s); i++ {
 		s[i] = 42
 	}
 
 	if !bytes.Equal(buf.Slice(), s) {
@@ -154,22 +155,23 @@ func BenchmarkMakeAioBufferZero(b *testing.B) {
 	for i := 0; i < b.N; i++ {
 		buf := MakeAioBufferZero(bufferSize)
 		buf.Free()
 	}
 }
 
 // Benchmark zeroing a buffer.
 func BenchmarkAioBufferZero(b *testing.B) {
 	for i := 0; i < b.N; i++ {
 		buf := MakeAioBuffer(bufferSize)
+		s := buf.Slice()
 		for i := uint(0); i < bufferSize; i++ {
-			*buf.Get(i) = 0
+			s[i] = 0
 		}
 		buf.Free()
 	}
 }
 
 // Benchmark creating a buffer by copying a Go slice.
 func BenchmarkFromBytes(b *testing.B) {
 	for i := 0; i < b.N; i++ {
 		zeroes := make([]byte, bufferSize)
 		buf := FromBytes(zeroes)
diff --git a/golang/libnbd_500_aio_pread_test.go b/golang/libnbd_500_aio_pread_test.go
index 0811378c..bd0208ef 100644
--- a/golang/libnbd_500_aio_pread_test.go
+++ b/golang/libnbd_500_aio_pread_test.go
@@ -55,14 +55,14 @@ func Test500AioPRead(t *testing.T) {
 		}
 		h.Poll(-1)
 	}
 
 	// Expected data.
 	expected := make([]byte, 512)
 	for i := 0; i < 512; i += 8 {
 		binary.BigEndian.PutUint64(expected[i:i+8], uint64(i))
 	}
 
-	if !bytes.Equal(buf.Bytes(), expected) {
+	if !bytes.Equal(buf.Slice(), expected) {
 		t.Fatalf("did not read expected data")
 	}
 }
diff --git a/golang/libnbd_510_aio_pwrite_test.go b/golang/libnbd_510_aio_pwrite_test.go
index 56cdcb05..493159f2 100644
--- a/golang/libnbd_510_aio_pwrite_test.go
+++ b/golang/libnbd_510_aio_pwrite_test.go
@@ -32,23 +32,25 @@ func Test510AioPWrite(t *testing.T) {
 		"nbdkit", "-s", "--exit-with-parent", "-v",
 		"memory", "size=512",
 	})
 	if err != nil {
 		t.Fatalf("could not connect: %s", err)
 	}
 
 	/* Write a pattern and read it back. */
 	buf := MakeAioBuffer(512)
 	defer buf.Free()
+
+	s := buf.Slice()
 	for i := 0; i < 512; i += 2 {
-		*buf.Get(uint(i)) = 0x55
-		*buf.Get(uint(i + 1)) = 0xAA
+		s[i] = 0x55
+		s[i+1] = 0xAA
 	}
 
 	var cookie uint64
 	cookie, err = h.AioPwrite(buf, 0, nil)
 	if err != nil {
 		t.Fatalf("%s", err)
 	}
 	for {
 		var b bool
 		b, err = h.AioCommandCompleted(cookie)
@@ -62,14 +64,14 @@ func Test510AioPWrite(t *testing.T) {
 	}
 
 	/* We already tested aio_pread, let's just read the data
 	back in the regular synchronous way. */
 	buf2 := make([]byte, 512)
 	err = h.Pread(buf2, 0, nil)
 	if err != nil {
 		t.Fatalf("%s", err)
 	}
 
-	if !bytes.Equal(buf.Bytes(), buf2) {
+	if !bytes.Equal(buf.Slice(), buf2) {
 		t.Fatalf("did not read back same data as written")
 	}
 }
-- 
2.34.1




More information about the Libguestfs mailing list