craps
This commit is contained in:
56
tcp-connection_test.go
Normal file
56
tcp-connection_test.go
Normal 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++
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user