123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package door
- import (
- "net"
- "testing"
- "time"
- )
- // helper routines - to help with testing.
- // Using pipes causes failures.
- func Possible_setupSockets() (server net.Conn, client net.Conn) {
- // the better way to set up connections.
- return net.Pipe()
- }
- // setupSockets: return server and client (that's connected to server)
- func setupSockets() (server net.Conn, client net.Conn) {
- // establish network socket connection to set Comm_handle
- var err error
- var sock net.Listener
- sock, err = net.Listen("tcp", "127.0.0.1:0")
- if err != nil {
- panic(err)
- }
- // I only need address for making the connection.
- // Get address of listening socket
- address := sock.Addr().String()
- client, err = net.Dial("tcp", address)
- if err != nil {
- panic(err)
- }
- server, err = sock.Accept()
- if err != nil {
- panic(err)
- }
- sock.Close()
- return server, client
- }
- func clear_socket(socket net.Conn, t *testing.T) string {
- // Clear out the data that's pending in the socket.
- buffer := make([]byte, 1204)
- var r int
- var err error
- err = socket.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
- if err != nil {
- t.Error("socket.SetReadDeadLine:", err)
- }
- r, err = socket.Read(buffer)
- if err != nil {
- t.Errorf("socket.Read: %#v", err)
- }
- // t.Errorf("Buffer : %#v\n", buffer[:r])
- err = socket.SetReadDeadline(time.Time{})
- if err != nil {
- t.Error("socket.SetReadDeadLine:", err)
- }
- return string(buffer[:r])
- }
|