github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/p2p/net/conn/conn_test.go (about)

     1  package conn
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"runtime"
     7  	"sync"
     8  	"testing"
     9  	"time"
    10  
    11  	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
    12  	travis "github.com/ipfs/go-ipfs/util/testutil/ci/travis"
    13  )
    14  
    15  func testOneSendRecv(t *testing.T, c1, c2 Conn) {
    16  	log.Debugf("testOneSendRecv from %s to %s", c1.LocalPeer(), c2.LocalPeer())
    17  	m1 := []byte("hello")
    18  	if err := c1.WriteMsg(m1); err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	m2, err := c2.ReadMsg()
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	if !bytes.Equal(m1, m2) {
    26  		t.Fatal("failed to send: %s %s", m1, m2)
    27  	}
    28  }
    29  
    30  func testNotOneSendRecv(t *testing.T, c1, c2 Conn) {
    31  	m1 := []byte("hello")
    32  	if err := c1.WriteMsg(m1); err == nil {
    33  		t.Fatal("write should have failed", err)
    34  	}
    35  	_, err := c2.ReadMsg()
    36  	if err == nil {
    37  		t.Fatal("read should have failed", err)
    38  	}
    39  }
    40  
    41  func TestClose(t *testing.T) {
    42  	// t.Skip("Skipping in favor of another test")
    43  
    44  	ctx, cancel := context.WithCancel(context.Background())
    45  	defer cancel()
    46  	c1, c2, _, _ := setupSingleConn(t, ctx)
    47  
    48  	testOneSendRecv(t, c1, c2)
    49  	testOneSendRecv(t, c2, c1)
    50  
    51  	c1.Close()
    52  	testNotOneSendRecv(t, c1, c2)
    53  
    54  	c2.Close()
    55  	testNotOneSendRecv(t, c2, c1)
    56  	testNotOneSendRecv(t, c1, c2)
    57  }
    58  
    59  func TestCloseLeak(t *testing.T) {
    60  	// t.Skip("Skipping in favor of another test")
    61  	if testing.Short() {
    62  		t.SkipNow()
    63  	}
    64  
    65  	if travis.IsRunning() {
    66  		t.Skip("this doesn't work well on travis")
    67  	}
    68  
    69  	var wg sync.WaitGroup
    70  
    71  	runPair := func(num int) {
    72  		ctx, cancel := context.WithCancel(context.Background())
    73  		c1, c2, _, _ := setupSingleConn(t, ctx)
    74  
    75  		for i := 0; i < num; i++ {
    76  			b1 := []byte(fmt.Sprintf("beep%d", i))
    77  			c1.WriteMsg(b1)
    78  			b2, err := c2.ReadMsg()
    79  			if err != nil {
    80  				panic(err)
    81  			}
    82  			if !bytes.Equal(b1, b2) {
    83  				panic(fmt.Errorf("bytes not equal: %s != %s", b1, b2))
    84  			}
    85  
    86  			b2 = []byte(fmt.Sprintf("boop%d", i))
    87  			c2.WriteMsg(b2)
    88  			b1, err = c1.ReadMsg()
    89  			if err != nil {
    90  				panic(err)
    91  			}
    92  			if !bytes.Equal(b1, b2) {
    93  				panic(fmt.Errorf("bytes not equal: %s != %s", b1, b2))
    94  			}
    95  
    96  			<-time.After(time.Microsecond * 5)
    97  		}
    98  
    99  		c1.Close()
   100  		c2.Close()
   101  		cancel() // close the listener
   102  		wg.Done()
   103  	}
   104  
   105  	var cons = 5
   106  	var msgs = 50
   107  	log.Debugf("Running %d connections * %d msgs.\n", cons, msgs)
   108  	for i := 0; i < cons; i++ {
   109  		wg.Add(1)
   110  		go runPair(msgs)
   111  	}
   112  
   113  	log.Debugf("Waiting...\n")
   114  	wg.Wait()
   115  	// done!
   116  
   117  	<-time.After(time.Millisecond * 150)
   118  	if runtime.NumGoroutine() > 20 {
   119  		// panic("uncomment me to debug")
   120  		t.Fatal("leaking goroutines:", runtime.NumGoroutine())
   121  	}
   122  }