help_test.go 1.4 KB

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