From 83742afabfc8c0e0d39c5bd541a27bc7b50247da Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Tue, 6 Jul 2021 13:16:24 -0700 Subject: [PATCH] convert manual tests into automated tests --- file.go | 38 ---------------------------- net/uring/file_test.go | 33 ++++++++++++++++++++++++ net/uring/testdata/voltaire.txt | 1 + net/uring/udp_test.go | 45 +++++++++++++++++++++++++++++++++ udp.go | 29 --------------------- 5 files changed, 79 insertions(+), 67 deletions(-) delete mode 100644 file.go create mode 100644 net/uring/file_test.go create mode 100644 net/uring/testdata/voltaire.txt create mode 100644 net/uring/udp_test.go delete mode 100644 udp.go diff --git a/file.go b/file.go deleted file mode 100644 index ba9c4ca19..000000000 --- a/file.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -import ( - "os" - - "tailscale.com/net/uring" -) - -func main() { - const msg = "hello, I am here\n" - err := os.WriteFile("junk", []byte(msg), 0644) - check(err) - - f, err := os.Open("junk") - check(err) - defer f.Close() - - uf, err := uring.NewFile(f) - check(err) - for i := 0; i < 1000; i++ { - go func() { - buf := make([]byte, 100) - n, err := uf.Read(buf) - check(err) - if n != len(msg) || string(buf[:n]) != msg { - panic("OOPS") - } - }() - } - // fmt.Println("read", n, "bytes") - // fmt.Println(string(buf[:n])) -} - -func check(err error) { - if err != nil { - panic(err) - } -} diff --git a/net/uring/file_test.go b/net/uring/file_test.go new file mode 100644 index 000000000..978f27a93 --- /dev/null +++ b/net/uring/file_test.go @@ -0,0 +1,33 @@ +package uring + +import ( + "os" + "testing" + + qt "github.com/frankban/quicktest" +) + +func TestFileRead(t *testing.T) { + if !Available() { + t.Skip("io_uring not available") + } + c := qt.New(t) + + const path = "testdata/voltaire.txt" + want, err := os.ReadFile(path) + c.Assert(err, qt.IsNil) + + f, err := os.Open(path) + c.Assert(err, qt.IsNil) + t.Cleanup(func() { f.Close() }) + + uf, err := NewFile(f) + if err != nil { + t.Skipf("io_uring not available: %v", err) + } + t.Cleanup(func() { uf.Close() }) + buf := make([]byte, len(want)+128) + n, err := uf.Read(buf) + c.Assert(err, qt.IsNil) + c.Assert(buf[:n], qt.DeepEquals, want) +} diff --git a/net/uring/testdata/voltaire.txt b/net/uring/testdata/voltaire.txt new file mode 100644 index 000000000..3ba5278c2 --- /dev/null +++ b/net/uring/testdata/voltaire.txt @@ -0,0 +1 @@ +If io_uring did not exist, it would be necessary to invent it. diff --git a/net/uring/udp_test.go b/net/uring/udp_test.go new file mode 100644 index 000000000..0a0a01b3d --- /dev/null +++ b/net/uring/udp_test.go @@ -0,0 +1,45 @@ +package uring + +import ( + "net" + "testing" + + qt "github.com/frankban/quicktest" +) + +func TestUDPSendRecv(t *testing.T) { + if !Available() { + t.Skip("io_uring not available") + } + c := qt.New(t) + + listen, err := net.ListenUDP("udp4", &net.UDPAddr{Port: 9999}) + t.Cleanup(func() { listen.Close() }) + c.Assert(err, qt.IsNil) + + conn, err := NewUDPConn(listen) + t.Cleanup(func() { conn.Close() }) + if err != nil { + t.Skipf("io_uring not available: %v", err) + } + addr := listen.LocalAddr() + sendBuf := make([]byte, 200) + for i := range sendBuf { + sendBuf[i] = byte(i) + } + recvBuf := make([]byte, 200) + + // Write one direction. + _, err = conn.WriteTo(sendBuf, addr) + c.Assert(err, qt.IsNil) + n, ipp, err := conn.ReadFromNetaddr(recvBuf) + c.Assert(err, qt.IsNil) + c.Assert(recvBuf[:n], qt.DeepEquals, sendBuf) + + // Write the other direction, to check that ipp is correct. + _, err = conn.WriteTo(sendBuf, ipp.UDPAddr()) + c.Assert(err, qt.IsNil) + n, _, err = conn.ReadFromNetaddr(recvBuf) + c.Assert(err, qt.IsNil) + c.Assert(recvBuf[:n], qt.DeepEquals, sendBuf) +} diff --git a/udp.go b/udp.go deleted file mode 100644 index 315f0d616..000000000 --- a/udp.go +++ /dev/null @@ -1,29 +0,0 @@ -package main - -import ( - "fmt" - "net" - - "tailscale.com/net/uring" -) - -func main() { - listen, err := net.ListenUDP("udp4", &net.UDPAddr{Port: 9999}) - check(err) - fmt.Println("listening UDP on", listen.LocalAddr()) - - conn, err := uring.NewUDPConn(listen) - check(err) - for { - b := make([]byte, 2000) - n, ipp, err := conn.ReadFromNetaddr(b) - check(err) - fmt.Printf("received %q from %v\n", b[:n], ipp) - } -} - -func check(err error) { - if err != nil { - panic(err) - } -}