Files
go-cart-actor/tcp-connection_test.go
matst80 3d55c4ed1f
Some checks failed
Build and Publish / BuildAndDeploy (push) Successful in 2m28s
Build and Publish / BuildAndDeployAmd64 (push) Failing after 15m0s
Merge branch 'main' of git-ssh.tornberg.me:mats/go-cart-actor into test/net-pool
2024-11-21 21:44:04 +01:00

69 lines
1.7 KiB
Go

package main
import (
"fmt"
"net"
"testing"
"github.com/yudhasubki/netpool"
)
func TestGenericConnection(t *testing.T) {
listenConn := NewConnection("127.0.0.1:51337", nil)
listener, err := listenConn.Listen()
if err != nil {
t.Errorf("Error listening: %v\n", err)
}
pool, err := netpool.New(func() (net.Conn, error) {
return net.Dial("tcp", "127.0.0.1:51337")
}, netpool.WithMaxPool(512))
if err != nil {
t.Errorf("Error creating pool: %v\n", err)
}
conn := NewConnection("127.0.0.1:51337", pool)
datta := []byte("Hello, world!")
listener.AddHandler(Ping, func(input *FrameWithPayload, resultChan chan<- FrameWithPayload) error {
resultChan <- MakeFrameWithPayload(Pong, 200, nil)
return nil
})
listener.AddHandler(1, func(input *FrameWithPayload, resultChan chan<- FrameWithPayload) error {
resultChan <- MakeFrameWithPayload(2, 200, datta)
return nil
})
listener.AddHandler(3, func(input *FrameWithPayload, resultChan chan<- FrameWithPayload) error {
return fmt.Errorf("Error in custom handler")
})
r, err := conn.Call(1, datta)
if err != nil {
t.Errorf("Error calling: %v\n", err)
}
if r.Type != 2 {
t.Errorf("Expected type 2, got %d\n", r.Type)
}
response, err := conn.Call(Ping, nil)
if err != nil || response.StatusCode != 200 || response.Type != Pong {
t.Errorf("Error connecting to remote %v\n", response)
}
res, err := conn.Call(3, datta)
if res.StatusCode == 200 {
t.Errorf("Expected error, got %v\n", res)
}
i := 100
results := make(chan FrameWithPayload, i)
for i > 0 {
go conn.CallAsync(1, datta, results)
i--
}
for i < 100 {
r := <-results
if r.Type != 2 {
t.Errorf("Expected type 2, got %d\n", r.Type)
}
i++
}
}