convert manual tests into automated tests

This commit is contained in:
Josh Bleecher Snyder 2021-07-06 13:16:24 -07:00
parent b668e5d185
commit 83742afabf
5 changed files with 79 additions and 67 deletions

38
file.go
View File

@ -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)
}
}

33
net/uring/file_test.go Normal file
View File

@ -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)
}

1
net/uring/testdata/voltaire.txt vendored Normal file
View File

@ -0,0 +1 @@
If io_uring did not exist, it would be necessary to invent it.

45
net/uring/udp_test.go Normal file
View File

@ -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)
}

29
udp.go
View File

@ -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)
}
}