From 513f947f89ea097d36757d8d674d055118735c89 Mon Sep 17 00:00:00 2001 From: leongross Date: Fri, 11 Oct 2024 13:14:50 +0200 Subject: [PATCH] add integration tests for net --- testdata/net.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ testdata/net.txt | 2 ++ 2 files changed, 84 insertions(+) create mode 100644 testdata/net.go create mode 100644 testdata/net.txt diff --git a/testdata/net.go b/testdata/net.go new file mode 100644 index 0000000000..746becbce4 --- /dev/null +++ b/testdata/net.go @@ -0,0 +1,82 @@ +package main + +import ( + "bytes" + "fmt" + "net" + "strconv" + "time" +) + +// Test golang network package integration for tinygo. +// This test is not exhaustive and only tests the basic functionality of the package. + +const ( + TEST_PORT = 9000 +) + +var ( + testsPassed uint + err error + sendBuf = &bytes.Buffer{} + recvBuf = &bytes.Buffer{} +) + +var ( + testDialListenData = []byte("Hello tinygo :)") +) + +func TestDialListen() { + // listen thread + go func() { + ln, err := net.Listen("tcp", ":"+strconv.FormatInt(TEST_PORT, 10)) + if err != nil { + fmt.Printf("error listening: %v\n", err) + return + } + + conn, err := ln.Accept() + if err != nil { + fmt.Printf("error accepting: %v\n", err) + return + } + + recvBuf.Reset() + _, err = conn.Read(recvBuf.Bytes()) + if err != nil { + fmt.Printf("error reading: %v\n", err) + return + } + + // TODO: this is racy + if recvBuf.String() != string(testDialListenData) { + fmt.Printf("error: received data does not match sent data: '%s' != '%s'\n", recvBuf.String(), string(testDialListenData)) + return + } + conn.Close() + + return + }() + + // hacky way to wait for the listener to start + time.Sleep(1 * time.Second) + + sendBuf.Reset() + fmt.Fprint(sendBuf, testDialListenData) + conn, err := net.Dial("tcp4", "127.0.0.1:"+strconv.FormatInt(TEST_PORT, 10)) + if err != nil { + fmt.Printf("error dialing: %v\n", err) + return + } + + if _, err = conn.Write(sendBuf.Bytes()); err != nil { + fmt.Printf("error writing: %v\n", err) + return + } +} + +func main() { + fmt.Printf("test: net start\n") + TestDialListen() + fmt.Printf("test: net end\n") +} diff --git a/testdata/net.txt b/testdata/net.txt new file mode 100644 index 0000000000..82681e45a0 --- /dev/null +++ b/testdata/net.txt @@ -0,0 +1,2 @@ +test: net start +test: net end \ No newline at end of file