github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/routing/mock/routing_test.go (about) 1 package mock 2 3 import ( 4 "bytes" 5 "testing" 6 7 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" 8 "github.com/jbenet/go-ipfs/peer" 9 u "github.com/jbenet/go-ipfs/util" 10 ) 11 12 func TestKeyNotFound(t *testing.T) { 13 14 vrs := VirtualRoutingServer() 15 empty := vrs.Providers(u.Key("not there")) 16 if len(empty) != 0 { 17 t.Fatal("should be empty") 18 } 19 } 20 21 func TestSetAndGet(t *testing.T) { 22 pid := peer.ID([]byte("the peer id")) 23 p := peer.WithID(pid) 24 k := u.Key("42") 25 rs := VirtualRoutingServer() 26 err := rs.Announce(p, k) 27 if err != nil { 28 t.Fatal(err) 29 } 30 providers := rs.Providers(k) 31 if len(providers) != 1 { 32 t.Fatal("should be one") 33 } 34 for _, elem := range providers { 35 if bytes.Equal(elem.ID(), pid) { 36 return 37 } 38 } 39 t.Fatal("ID should have matched") 40 } 41 42 func TestClientFindProviders(t *testing.T) { 43 peer := peer.WithIDString("42") 44 rs := VirtualRoutingServer() 45 client := rs.Client(peer) 46 47 k := u.Key("hello") 48 err := client.Provide(context.Background(), k) 49 if err != nil { 50 t.Fatal(err) 51 } 52 max := 100 53 54 providersFromHashTable := rs.Providers(k) 55 56 isInHT := false 57 for _, p := range providersFromHashTable { 58 if bytes.Equal(p.ID(), peer.ID()) { 59 isInHT = true 60 } 61 } 62 if !isInHT { 63 t.Fatal("Despite client providing key, peer wasn't in hash table as a provider") 64 } 65 providersFromClient := client.FindProvidersAsync(context.Background(), u.Key("hello"), max) 66 isInClient := false 67 for p := range providersFromClient { 68 if bytes.Equal(p.ID(), peer.ID()) { 69 isInClient = true 70 } 71 } 72 if !isInClient { 73 t.Fatal("Despite client providing key, client didn't receive peer when finding providers") 74 } 75 } 76 77 func TestClientOverMax(t *testing.T) { 78 rs := VirtualRoutingServer() 79 k := u.Key("hello") 80 numProvidersForHelloKey := 100 81 for i := 0; i < numProvidersForHelloKey; i++ { 82 peer := peer.WithIDString(string(i)) 83 err := rs.Announce(peer, k) 84 if err != nil { 85 t.Fatal(err) 86 } 87 } 88 providersFromHashTable := rs.Providers(k) 89 if len(providersFromHashTable) != numProvidersForHelloKey { 90 t.Log(1 == len(providersFromHashTable)) 91 t.Fatal("not all providers were returned") 92 } 93 94 max := 10 95 peer := peer.WithIDString("TODO") 96 client := rs.Client(peer) 97 98 providersFromClient := client.FindProvidersAsync(context.Background(), k, max) 99 i := 0 100 for _ = range providersFromClient { 101 i++ 102 } 103 if i != max { 104 t.Fatal("Too many providers returned") 105 } 106 } 107 108 // TODO does dht ensure won't receive self as a provider? probably not. 109 func TestCanceledContext(t *testing.T) { 110 rs := VirtualRoutingServer() 111 k := u.Key("hello") 112 113 t.Log("async'ly announce infinite stream of providers for key") 114 i := 0 115 go func() { // infinite stream 116 for { 117 peer := peer.WithIDString(string(i)) 118 err := rs.Announce(peer, k) 119 if err != nil { 120 t.Fatal(err) 121 } 122 i++ 123 } 124 }() 125 126 local := peer.WithIDString("peer id doesn't matter") 127 client := rs.Client(local) 128 129 t.Log("warning: max is finite so this test is non-deterministic") 130 t.Log("context cancellation could simply take lower priority") 131 t.Log("and result in receiving the max number of results") 132 max := 1000 133 134 t.Log("cancel the context before consuming") 135 ctx, cancelFunc := context.WithCancel(context.Background()) 136 cancelFunc() 137 providers := client.FindProvidersAsync(ctx, k, max) 138 139 numProvidersReturned := 0 140 for _ = range providers { 141 numProvidersReturned++ 142 } 143 t.Log(numProvidersReturned) 144 145 if numProvidersReturned == max { 146 t.Fatal("Context cancel had no effect") 147 } 148 }