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.

47 lines
920 B

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