use correct packages
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m51s

This commit is contained in:
matst80
2024-11-10 21:58:43 +01:00
parent c70c5cd930
commit ab0e708d00
5 changed files with 65 additions and 11 deletions

47
tcp-cart_test.go Normal file
View File

@@ -0,0 +1,47 @@
package main
import (
"log"
"testing"
)
func TestCartTcpHelpers(t *testing.T) {
server, err := CartListen("localhost:51337")
if err != nil {
t.Errorf("Error listening: %v\n", err)
}
client, err := CartDial("localhost:51337")
if err != nil {
t.Errorf("Error dialing: %v\n", err)
}
var messageData string
server.ListenFor(1, func(id CartId, data []byte) error {
log.Printf("Received message: %s\n", string(data))
messageData = string(data)
return nil
})
server.HandleCall(2, func(id CartId, data []byte) (uint32, []byte, error) {
log.Printf("Received call: %s\n", string(data))
return 3, []byte("Hello, client!"), nil
})
server.HandleCall(Ping, func(id CartId, data []byte) (uint32, []byte, error) {
return Pong, nil, nil
})
id := ToCartId("kalle")
client.SendPacket(1, id, []byte("Hello, world!"))
answer, err := client.Call(2, id, 3, []byte("Hello, server!"))
if err != nil {
t.Errorf("Error calling: %v\n", err)
}
_, err = client.Call(Ping, id, Pong, nil)
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)
}
}