tcp mux and stuff
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m49s
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m49s
This commit is contained in:
40
tcp-mux_test.go
Normal file
40
tcp-mux_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTcpHelpers(t *testing.T) {
|
||||
|
||||
server, err := Listen(":1337")
|
||||
if err != nil {
|
||||
t.Errorf("Error listening: %v\n", err)
|
||||
}
|
||||
client, err := Dial("localhost:1337")
|
||||
if err != nil {
|
||||
t.Errorf("Error dialing: %v\n", err)
|
||||
}
|
||||
var messageData string
|
||||
server.ListenFor(1, func(data []byte) error {
|
||||
log.Printf("Received message: %s\n", string(data))
|
||||
messageData = string(data)
|
||||
return nil
|
||||
})
|
||||
server.HandleCall(2, func(data []byte) (uint16, []byte, error) {
|
||||
log.Printf("Received call: %s\n", string(data))
|
||||
return 3, []byte("Hello, client!"), nil
|
||||
})
|
||||
|
||||
client.SendPacket(1, []byte("Hello, world!"))
|
||||
answer, err := client.Call(2, 3, []byte("Hello, server!"))
|
||||
if err != nil {
|
||||
t.Errorf("Error calling: %v\n", err)
|
||||
}
|
||||
if string(answer) != "Hello, client!" {
|
||||
t.Errorf("Expected answer 'Hello, client!', got %s\n", string(answer))
|
||||
}
|
||||
if messageData != "Hello, world!" {
|
||||
t.Errorf("Expected message 'Hello, world!', got %s\n", messageData)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user