listenbuffer: add test
Signed-off-by: Simon Eskildsen <sirup@sirupsen.com>
This commit is contained in:
parent
c0c4f68893
commit
688b143a36
1 changed files with 41 additions and 0 deletions
41
listenbuffer/listen_buffer_test.go
Normal file
41
listenbuffer/listen_buffer_test.go
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
package listenbuffer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestListenBufferAllowsAcceptingWhenActivated(t *testing.T) {
|
||||||
|
lock := make(chan struct{})
|
||||||
|
buffer, err := NewListenBuffer("tcp", "", lock)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Unable to create listen buffer: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
conn, err := net.Dial("tcp", buffer.Addr().String())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Client failed to establish connection to server: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.Write([]byte("ping"))
|
||||||
|
conn.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
close(lock)
|
||||||
|
|
||||||
|
client, err := buffer.Accept()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Failed to accept client: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := ioutil.ReadAll(client)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Failed to read from client: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if string(response) != "ping" {
|
||||||
|
t.Fatal("Expected to receive ping from client, received: ", string(response))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue