craps
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 29s
Build and Publish / BuildAndDeploy (push) Successful in 2m25s

This commit is contained in:
matst80
2024-11-13 20:16:40 +01:00
parent dafefa377f
commit 9f7c8227c2
6 changed files with 299 additions and 22 deletions

56
tcp-connection_test.go Normal file
View File

@@ -0,0 +1,56 @@
package main
import "testing"
type StringData string
func (s StringData) ToBytes() []byte {
return []byte(s)
}
func (s StringData) FromBytes(data []byte) error {
s = StringData(data)
return nil
}
func TestGenericConnection(t *testing.T) {
conn := NewConnection("localhost:51337")
listener, err := conn.Listen()
if err != nil {
t.Errorf("Error listening: %v\n", err)
}
listener.AddHandler(1, func(input *FrameWithPayload, resultChan chan<- *FrameWithPayload) error {
payload := []byte("Hello, world!")
resultChan <- &FrameWithPayload{
Frame: Frame{
Type: 2,
Id: input.Id,
StatusCode: 200,
Length: uint32(len("Hello, world!")),
},
Payload: payload,
}
return nil
})
r, err := conn.Call(1, StringData("Hello, world!"))
if err != nil {
t.Errorf("Error calling: %v\n", err)
}
if r.Type != 2 {
t.Errorf("Expected type 2, got %d\n", r.Type)
}
i := 100
results := make(chan *FrameWithPayload, i)
for i > 0 {
conn.CallAsync(1, StringData("Hello, world!"), results)
i--
}
for i < 100 {
r := <-results
if r.Type != 2 {
t.Errorf("Expected type 2, got %d\n", r.Type)
}
i++
}
}