help_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package door
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. )
  7. // helper routines - to help with testing.
  8. func setupSockets() (server net.Conn, client net.Conn) {
  9. // the better way to set up connections.
  10. return net.Pipe()
  11. }
  12. // setupSockets: return server and client (that's connected to server)
  13. func Old_setupSockets() (server net.Conn, client net.Conn) {
  14. // establish network socket connection to set Comm_handle
  15. var err error
  16. var sock net.Listener
  17. sock, err = net.Listen("tcp", "127.0.0.1:0")
  18. if err != nil {
  19. panic(err)
  20. }
  21. // I only need address for making the connection.
  22. // Get address of listening socket
  23. address := sock.Addr().String()
  24. client, err = net.Dial("tcp", address)
  25. if err != nil {
  26. panic(err)
  27. }
  28. server, err = sock.Accept()
  29. if err != nil {
  30. panic(err)
  31. }
  32. sock.Close()
  33. return server, client
  34. }
  35. func clear_socket(socket net.Conn, t *testing.T) string {
  36. // Clear out the data that's pending in the socket.
  37. buffer := make([]byte, 1204)
  38. var r int
  39. var err error
  40. err = socket.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
  41. if err != nil {
  42. t.Error("socket.SetReadDeadLine:", err)
  43. }
  44. r, err = socket.Read(buffer)
  45. if err != nil {
  46. t.Errorf("socket.Read: %#v", err)
  47. }
  48. // t.Errorf("Buffer : %#v\n", buffer[:r])
  49. err = socket.SetReadDeadline(time.Time{})
  50. if err != nil {
  51. t.Error("socket.SetReadDeadLine:", err)
  52. }
  53. return string(buffer[:r])
  54. }