mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-13 12:18:00 +08:00
this is a major refactor of the entire codebase it changes the monolithic peer.Peer into using a peer.ID and a peer.Peerstore. Other changes: - removed handshake3. - testutil vastly simplified peer - secio bugfix + debugging logs - testutil: RandKeyPair - backpressure bugfix: w.o.w. - peer: added hex enc/dec - peer: added a PeerInfo struct PeerInfo is a small struct used to pass around a peer with a set of addresses and keys. This is not meant to be a complete view of the system, but rather to model updates to the peerstore. It is used by things like the routing system. - updated peer/queue + peerset - latency metrics - testutil: use crand for PeerID gen RandPeerID generates random "valid" peer IDs. it does not NEED to generate keys because it is as if we lost the key right away. fine to read some randomness and hash it. to generate proper keys and an ID, use: sk, pk, _ := testutil.RandKeyPair() id, _ := peer.IDFromPublicKey(pk) Also added RandPeerIDFatal helper - removed old spipe - updated seccat - core: cleanup initIdentity - removed old getFromPeerList
123 lines
2.4 KiB
Go
123 lines
2.4 KiB
Go
package conn
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os"
|
|
"runtime"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
|
)
|
|
|
|
func testOneSendRecv(t *testing.T, c1, c2 Conn) {
|
|
log.Debugf("testOneSendRecv from %s to %s", c1.LocalPeer(), c2.LocalPeer())
|
|
m1 := []byte("hello")
|
|
if err := c1.WriteMsg(m1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m2, err := c2.ReadMsg()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.Equal(m1, m2) {
|
|
t.Fatal("failed to send: %s %s", m1, m2)
|
|
}
|
|
}
|
|
|
|
func testNotOneSendRecv(t *testing.T, c1, c2 Conn) {
|
|
m1 := []byte("hello")
|
|
if err := c1.WriteMsg(m1); err == nil {
|
|
t.Fatal("write should have failed", err)
|
|
}
|
|
_, err := c2.ReadMsg()
|
|
if err == nil {
|
|
t.Fatal("read should have failed", err)
|
|
}
|
|
}
|
|
|
|
func TestClose(t *testing.T) {
|
|
// t.Skip("Skipping in favor of another test")
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
c1, c2, _, _ := setupSingleConn(t, ctx)
|
|
|
|
testOneSendRecv(t, c1, c2)
|
|
testOneSendRecv(t, c2, c1)
|
|
|
|
c1.Close()
|
|
testNotOneSendRecv(t, c1, c2)
|
|
|
|
c2.Close()
|
|
testNotOneSendRecv(t, c2, c1)
|
|
testNotOneSendRecv(t, c1, c2)
|
|
}
|
|
|
|
func TestCloseLeak(t *testing.T) {
|
|
// t.Skip("Skipping in favor of another test")
|
|
if testing.Short() {
|
|
t.SkipNow()
|
|
}
|
|
|
|
if os.Getenv("TRAVIS") == "true" {
|
|
t.Skip("this doesn't work well on travis")
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
runPair := func(num int) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
c1, c2, _, _ := setupSingleConn(t, ctx)
|
|
|
|
for i := 0; i < num; i++ {
|
|
b1 := []byte(fmt.Sprintf("beep%d", i))
|
|
c1.WriteMsg(b1)
|
|
b2, err := c2.ReadMsg()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
if !bytes.Equal(b1, b2) {
|
|
panic(fmt.Errorf("bytes not equal: %s != %s", b1, b2))
|
|
}
|
|
|
|
b2 = []byte(fmt.Sprintf("boop%d", i))
|
|
c2.WriteMsg(b2)
|
|
b1, err = c1.ReadMsg()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
if !bytes.Equal(b1, b2) {
|
|
panic(fmt.Errorf("bytes not equal: %s != %s", b1, b2))
|
|
}
|
|
|
|
<-time.After(time.Microsecond * 5)
|
|
}
|
|
|
|
c1.Close()
|
|
c2.Close()
|
|
cancel() // close the listener
|
|
wg.Done()
|
|
}
|
|
|
|
var cons = 5
|
|
var msgs = 50
|
|
log.Debugf("Running %d connections * %d msgs.\n", cons, msgs)
|
|
for i := 0; i < cons; i++ {
|
|
wg.Add(1)
|
|
go runPair(msgs)
|
|
}
|
|
|
|
log.Debugf("Waiting...\n")
|
|
wg.Wait()
|
|
// done!
|
|
|
|
<-time.After(time.Millisecond * 150)
|
|
if runtime.NumGoroutine() > 20 {
|
|
// panic("uncomment me to debug")
|
|
t.Fatal("leaking goroutines:", runtime.NumGoroutine())
|
|
}
|
|
}
|