Esempio n. 1
0
func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
	net := VirtualNetwork()
	idOfResponder := []byte("responder")
	waiter := net.Adapter(peer.WithIDString("waiter"))
	responder := net.Adapter(peer.WithID(idOfResponder))

	var wg sync.WaitGroup

	wg.Add(1)

	expectedStr := "received async"

	responder.SetDelegate(lambda(func(
		ctx context.Context,
		fromWaiter peer.Peer,
		msgFromWaiter bsmsg.BitSwapMessage) (
		peer.Peer, bsmsg.BitSwapMessage) {

		msgToWaiter := bsmsg.New()
		msgToWaiter.AddBlock(*blocks.NewBlock([]byte(expectedStr)))

		return fromWaiter, msgToWaiter
	}))

	waiter.SetDelegate(lambda(func(
		ctx context.Context,
		fromResponder peer.Peer,
		msgFromResponder bsmsg.BitSwapMessage) (
		peer.Peer, bsmsg.BitSwapMessage) {

		// TODO assert that this came from the correct peer and that the message contents are as expected
		ok := false
		for _, b := range msgFromResponder.Blocks() {
			if string(b.Data) == expectedStr {
				wg.Done()
				ok = true
			}
		}

		if !ok {
			t.Fatal("Message not received from the responder")

		}
		return nil, nil
	}))

	messageSentAsync := bsmsg.New()
	messageSentAsync.AddBlock(*blocks.NewBlock([]byte("data")))
	errSending := waiter.SendMessage(
		context.Background(), peer.WithID(idOfResponder), messageSentAsync)
	if errSending != nil {
		t.Fatal(errSending)
	}

	wg.Wait() // until waiter delegate function is executed
}
Esempio n. 2
0
func TestSendRequestToCooperativePeer(t *testing.T) {
	net := VirtualNetwork()

	idOfRecipient := []byte("recipient")

	t.Log("Get two network adapters")

	initiator := net.Adapter(peer.WithIDString("initiator"))
	recipient := net.Adapter(peer.WithID(idOfRecipient))

	expectedStr := "response from recipient"
	recipient.SetDelegate(lambda(func(
		ctx context.Context,
		from peer.Peer,
		incoming bsmsg.BitSwapMessage) (
		peer.Peer, bsmsg.BitSwapMessage) {

		t.Log("Recipient received a message from the network")

		// TODO test contents of incoming message

		m := bsmsg.New()
		m.AddBlock(*blocks.NewBlock([]byte(expectedStr)))

		return from, m
	}))

	t.Log("Build a message and send a synchronous request to recipient")

	message := bsmsg.New()
	message.AddBlock(*blocks.NewBlock([]byte("data")))
	response, err := initiator.SendRequest(
		context.Background(), peer.WithID(idOfRecipient), message)
	if err != nil {
		t.Fatal(err)
	}

	t.Log("Check the contents of the response from recipient")

	for _, blockFromRecipient := range response.Blocks() {
		if string(blockFromRecipient.Data) == expectedStr {
			return
		}
	}
	t.Fatal("Should have returned after finding expected block data")
}
Esempio n. 3
0
func newPeer(t *testing.T, id string) peer.Peer {
	mh, err := mh.FromHexString(id)
	if err != nil {
		t.Error(err)
		return nil
	}

	return peer.WithID(peer.ID(mh))
}
Esempio n. 4
0
func TestSetAndGet(t *testing.T) {
	pid := peer.ID([]byte("the peer id"))
	p := peer.WithID(pid)
	k := u.Key("42")
	rs := VirtualRoutingServer()
	err := rs.Announce(p, k)
	if err != nil {
		t.Fatal(err)
	}
	providers := rs.Providers(k)
	if len(providers) != 1 {
		t.Fatal("should be one")
	}
	for _, elem := range providers {
		if bytes.Equal(elem.ID(), pid) {
			return
		}
	}
	t.Fatal("ID should have matched")
}
Esempio n. 5
0
func TestSimultOpen(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}

	addrs := []string{
		"/ip4/127.0.0.1/tcp/1244",
		"/ip4/127.0.0.1/tcp/1245",
	}

	ctx := context.Background()
	swarms, _ := makeSwarms(ctx, t, addrs)

	// connect everyone
	{
		var wg sync.WaitGroup
		connect := func(s *Swarm, dst peer.Peer) {
			// copy for other peer
			cp := peer.WithID(dst.ID())
			cp.AddAddress(dst.Addresses()[0])

			if _, err := s.Dial(cp); err != nil {
				t.Fatal("error swarm dialing to peer", err)
			}
			wg.Done()
		}

		log.Info("Connecting swarms simultaneously.")
		wg.Add(2)
		go connect(swarms[0], swarms[1].local)
		go connect(swarms[1], swarms[0].local)
		wg.Wait()
	}

	for _, s := range swarms {
		s.Close()
	}
}
Esempio n. 6
0
// session creates a test bitswap session.
//
// NB: It's easy make mistakes by providing the same peer ID to two different
// sessions. To safeguard, use the SessionGenerator to generate sessions. It's
// just a much better idea.
func session(net tn.Network, rs mock.RoutingServer, id peer.ID) instance {
	p := peer.WithID(id)

	adapter := net.Adapter(p)
	htc := rs.Client(p)

	blockstore := bstore.NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
	const alwaysSendToPeer = true
	bs := &bitswap{
		blockstore:    blockstore,
		notifications: notifications.New(),
		strategy:      strategy.New(alwaysSendToPeer),
		routing:       htc,
		sender:        adapter,
		wantlist:      util.NewKeySet(),
	}
	adapter.SetDelegate(bs)
	return instance{
		peer:       p,
		exchange:   bs,
		blockstore: blockstore,
	}
}
Esempio n. 7
0
// TODO: Maybe put these in some sort of "ipfs_testutil" package
func _randPeer() peer.Peer {
	id := make(peer.ID, 16)
	crand.Read(id)
	p := peer.WithID(id)
	return p
}
Esempio n. 8
0
func newPeerTime(t time.Time) peer.Peer {
	s := fmt.Sprintf("hmmm time: %v", t)
	h := u.Hash([]byte(s))
	return peer.WithID(peer.ID(h))
}
Esempio n. 9
0
func RandPeer() peer.Peer {
	id := make([]byte, 16)
	crand.Read(id)
	mhid := u.Hash(id)
	return peer.WithID(peer.ID(mhid))
}