dht_test.go 1.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
package dht

import (
	"testing"
	peer "github.com/jbenet/go-ipfs/peer"
	ma "github.com/jbenet/go-multiaddr"
	u "github.com/jbenet/go-ipfs/util"

	"time"
)

func TestPing(t *testing.T) {
	u.Debug = false
	addr_a,err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1234")
	if err != nil {
		t.Fatal(err)
	}
	addr_b,err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5678")
	if err != nil {
		t.Fatal(err)
	}

	peer_a := new(peer.Peer)
	peer_a.AddAddress(addr_a)
	peer_a.ID = peer.ID([]byte("peer_a"))

	peer_b := new(peer.Peer)
	peer_b.AddAddress(addr_b)
	peer_b.ID = peer.ID([]byte("peer_b"))

	dht_a,err := NewDHT(peer_a)
	if err != nil {
		t.Fatal(err)
	}

	dht_b,err := NewDHT(peer_b)
	if err != nil {
		t.Fatal(err)
	}

	dht_a.Start()
	dht_b.Start()

Jeromy's avatar
Jeromy committed
44
	_,err = dht_a.Connect(addr_b)
45 46 47 48 49 50 51 52 53
	if err != nil {
		t.Fatal(err)
	}

	//Test that we can ping the node
	err = dht_a.Ping(peer_b, time.Second * 2)
	if err != nil {
		t.Fatal(err)
	}
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

	dht_a.Halt()
	dht_b.Halt()
}

func TestValueGetSet(t *testing.T) {
	u.Debug = false
	addr_a,err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1235")
	if err != nil {
		t.Fatal(err)
	}
	addr_b,err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5679")
	if err != nil {
		t.Fatal(err)
	}

	peer_a := new(peer.Peer)
	peer_a.AddAddress(addr_a)
	peer_a.ID = peer.ID([]byte("peer_a"))

	peer_b := new(peer.Peer)
	peer_b.AddAddress(addr_b)
	peer_b.ID = peer.ID([]byte("peer_b"))

	dht_a,err := NewDHT(peer_a)
	if err != nil {
		t.Fatal(err)
	}

	dht_b,err := NewDHT(peer_b)
	if err != nil {
		t.Fatal(err)
	}

	dht_a.Start()
	dht_b.Start()

Jeromy's avatar
Jeromy committed
91
	_,err = dht_a.Connect(addr_b)
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	if err != nil {
		t.Fatal(err)
	}

	err = dht_a.PutValue("hello", []byte("world"))
	if err != nil {
		t.Fatal(err)
	}

	val, err := dht_a.GetValue("hello", time.Second * 2)
	if err != nil {
		t.Fatal(err)
	}

	if string(val) != "world" {
		t.Fatalf("Expected 'world' got %s", string(val))
	}
109
}