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.

42 lines
855 B

7 years ago
7 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", true, 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 connIn.Write(msg)
  24. b := make([]byte, 32)
  25. n, err := connOut.Read(b)
  26. if err != nil {
  27. t.Fatalf("Error reading off connection: %v", err)
  28. }
  29. b = b[:n]
  30. if !bytes.Equal(msg, b) {
  31. t.Fatalf("Got %s, expected %s", b, msg)
  32. }
  33. // Close the server, no longer needed.
  34. l.Stop()
  35. }