You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
797 B

  1. package p2p
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. func TestListener(t *testing.T) {
  7. // Create a listener
  8. l := NewDefaultListener("tcp", ":8001", true)
  9. // Dial the listener
  10. lAddr := l.ExternalAddress()
  11. connOut, err := lAddr.Dial()
  12. if err != nil {
  13. t.Fatalf("Could not connect to listener address %v", lAddr)
  14. } else {
  15. t.Logf("Created a connection to listener address %v", lAddr)
  16. }
  17. connIn, ok := <-l.Connections()
  18. if !ok {
  19. t.Fatalf("Could not get inbound connection from listener")
  20. }
  21. msg := []byte("hi!")
  22. go connIn.Write(msg)
  23. b := make([]byte, 32)
  24. n, err := connOut.Read(b)
  25. if err != nil {
  26. t.Fatalf("Error reading off connection: %v", err)
  27. }
  28. b = b[:n]
  29. if !bytes.Equal(msg, b) {
  30. t.Fatalf("Got %s, expected %s", b, msg)
  31. }
  32. // Close the server, no longer needed.
  33. l.Stop()
  34. }