-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
test: net start | ||
test: net end |