Files
go-cart-actor/tcp-connection_test.go
matst80 9e87351681
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 27s
Build and Publish / BuildAndDeploy (push) Successful in 2m20s
update statuscode
2024-11-13 22:14:04 +01:00

46 lines
1.1 KiB
Go

package main
import "testing"
func TestGenericConnection(t *testing.T) {
conn := NewConnection("localhost:51337")
listener, err := conn.Listen()
if err != nil {
t.Errorf("Error listening: %v\n", err)
}
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
})
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)
}
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++
}
}