github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/p2p/listener_test.go (about)

     1  // +build !network
     2  
     3  package p2p
     4  
     5  import (
     6  	"bytes"
     7  	"testing"
     8  )
     9  
    10  func TestListener(t *testing.T) {
    11  	t.Skip()
    12  	// Create a listener
    13  	l, _ := NewDefaultListener("tcp", "localhost:8001", true)
    14  
    15  	// Dial the listener
    16  	lAddr := l.InternalAddress()
    17  	connOut, err := lAddr.Dial()
    18  	if err != nil {
    19  		t.Fatalf("Could not connect to listener address %v", lAddr)
    20  	}
    21  
    22  	connIn, ok := <-l.Connections()
    23  	if !ok {
    24  		t.Fatalf("Could not get inbound connection from listener")
    25  	}
    26  
    27  	msg := []byte("hi!")
    28  	go connIn.Write(msg)
    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  
    35  	b = b[:n]
    36  	if !bytes.Equal(msg, b) {
    37  		t.Fatalf("Got %s, expected %s", b, msg)
    38  	}
    39  
    40  	// Close the server, no longer needed.
    41  	l.Stop()
    42  }