ext_test.go 2.29 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3 4 5
package dht

import (
	"testing"

6 7
	"code.google.com/p/goprotobuf/proto"

Jeromy's avatar
Jeromy committed
8 9
	peer "github.com/jbenet/go-ipfs/peer"
	swarm "github.com/jbenet/go-ipfs/swarm"
10 11
	u "github.com/jbenet/go-ipfs/util"
	ma "github.com/jbenet/go-multiaddr"
Jeromy's avatar
Jeromy committed
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

	"time"
)

// fauxNet is a standin for a swarm.Network in order to more easily recreate
// different testing scenarios
type fauxNet struct {
	Chan *swarm.Chan

	swarm.Network

	handlers []mesHandleFunc
}

type mesHandleFunc func(*swarm.Message) *swarm.Message

func newFauxNet() *fauxNet {
	fn := new(fauxNet)
	fn.Chan = swarm.NewChan(8)

	return fn
}

func (f *fauxNet) Listen() error {
	go func() {
		for {
			select {
			case in := <-f.Chan.Outgoing:
40
				for _, h := range f.handlers {
Jeromy's avatar
Jeromy committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
					reply := h(in)
					if reply != nil {
						f.Chan.Incoming <- reply
						break
					}
				}
			}
		}
	}()
	return nil
}

func (f *fauxNet) AddHandler(fn func(*swarm.Message) *swarm.Message) {
	f.handlers = append(f.handlers, fn)
}

func (f *fauxNet) Send(mes *swarm.Message) {
58 59
	f.Chan.Outgoing <- mes
}
Jeromy's avatar
Jeromy committed
60

61 62
func (f *fauxNet) GetChan() *swarm.Chan {
	return f.Chan
Jeromy's avatar
Jeromy committed
63 64
}

65 66 67 68 69
func (f *fauxNet) Connect(addr *ma.Multiaddr) (*peer.Peer, error) {
	return nil, nil
}

func TestGetFailures(t *testing.T) {
Jeromy's avatar
Jeromy committed
70 71 72 73
	fn := newFauxNet()
	fn.Listen()

	local := new(peer.Peer)
74
	local.ID = peer.ID("test_peer")
Jeromy's avatar
Jeromy committed
75 76 77

	d := NewDHT(local, fn)

78 79
	other := &peer.Peer{ID: peer.ID("other_peer")}

Jeromy's avatar
Jeromy committed
80 81
	d.Start()

82 83 84 85
	d.Update(other)

	// This one should time out
	_, err := d.GetValue(u.Key("test"), time.Millisecond*5)
Jeromy's avatar
Jeromy committed
86
	if err != nil {
87 88 89 90 91 92 93 94 95
		nerr, ok := err.(*u.IpfsError)
		if !ok {
			t.Fatal("Got different error than we expected.")
		}
		if nerr.Inner != u.ErrTimeout {
			t.Fatal("Got different error than we expected.")
		}
	} else {
		t.Fatal("Did not get expected error!")
Jeromy's avatar
Jeromy committed
96 97
	}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	fn.AddHandler(func(mes *swarm.Message) *swarm.Message {
		pmes := new(PBDHTMessage)
		err := proto.Unmarshal(mes.Data, pmes)
		if err != nil {
			t.Fatal(err)
		}

		resp := DHTMessage{
			Type:     pmes.GetType(),
			Id:       pmes.GetId(),
			Response: true,
			Success:  false,
		}
		return swarm.NewMessage(mes.Peer, resp.ToProtobuf())
	})

	// This one should fail with NotFound
	_, err = d.GetValue(u.Key("test"), time.Millisecond*5)
	if err != nil {
		if err != u.ErrNotFound {
			t.Fatal("Expected ErrNotFound, got: %s", err)
		}
	} else {
		t.Fatal("expected error, got none.")
	}
Jeromy's avatar
Jeromy committed
123
}