@@ -2,8 +2,6 @@ package dual
22
33import (
44 "context"
5- "fmt"
6- "os"
75 "testing"
86 "time"
97
@@ -40,11 +38,9 @@ func MkFilterForPeer() (func(d *dht.IpfsDHT, conns []network.Conn) bool, *custom
4038 f := func (_ * dht.IpfsDHT , conns []network.Conn ) bool {
4139 for _ , c := range conns {
4240 if c .RemotePeer () == helper .allow {
43- fmt .Fprintf (os .Stderr , "allowed conn per filter\n " )
4441 return true
4542 }
4643 }
47- fmt .Fprintf (os .Stderr , "rejected conn per rt filter\n " )
4844 return false
4945 }
5046 return f , & helper
@@ -277,20 +273,89 @@ func TestSearchValue(t *testing.T) {
277273 t .Fatal (ctx .Err ())
278274 }
279275
276+ select {
277+ case _ , ok := <- valCh :
278+ if ok {
279+ t .Errorf ("chan should close" )
280+ }
281+ case <- ctx .Done ():
282+ t .Fatal (ctx .Err ())
283+ }
284+
280285 err = lan .PutValue (ctx , "/v/hello" , []byte ("newer" ))
281286 if err != nil {
282287 t .Error (err )
283288 }
284289
285- select {
286- case v , ok := <- valCh :
287- if string (v ) != "newer" {
288- t .Errorf ("expected 'newer', got '%s'" , string (v ))
289- }
290- if ! ok {
291- t .Errorf ("chan closed early" )
292- }
293- case <- ctx .Done ():
294- t .Fatal (ctx .Err ())
290+ valCh , err = d .SearchValue (ctx , "/v/hello" , dht .Quorum (0 ))
291+ if err != nil {
292+ t .Fatal (err )
293+ }
294+
295+ var lastVal []byte
296+ for c := range valCh {
297+ lastVal = c
298+ }
299+ if string (lastVal ) != "newer" {
300+ t .Fatal ("incorrect best search value" )
301+ }
302+ }
303+
304+ func TestGetPublicKey (t * testing.T ) {
305+ ctx , cancel := context .WithTimeout (context .Background (), 20 * time .Second )
306+ defer cancel ()
307+
308+ d , wan , lan := setupTier (ctx , t )
309+ defer d .Close ()
310+ defer wan .Close ()
311+ defer lan .Close ()
312+
313+ pk , err := d .GetPublicKey (ctx , wan .PeerID ())
314+ if err != nil {
315+ t .Fatal (err )
316+ }
317+ id , err := peer .IDFromPublicKey (pk )
318+ if err != nil {
319+ t .Fatal (err )
320+ }
321+ if id != wan .PeerID () {
322+ t .Fatal ("incorrect PK" )
323+ }
324+
325+ pk , err = d .GetPublicKey (ctx , lan .PeerID ())
326+ if err != nil {
327+ t .Fatal (err )
328+ }
329+ id , err = peer .IDFromPublicKey (pk )
330+ if err != nil {
331+ t .Fatal (err )
332+ }
333+ if id != lan .PeerID () {
334+ t .Fatal ("incorrect PK" )
335+ }
336+ }
337+
338+ func TestFindPeer (t * testing.T ) {
339+ ctx , cancel := context .WithTimeout (context .Background (), 20 * time .Second )
340+ defer cancel ()
341+
342+ d , wan , lan := setupTier (ctx , t )
343+ defer d .Close ()
344+ defer wan .Close ()
345+ defer lan .Close ()
346+
347+ p , err := d .FindPeer (ctx , lan .PeerID ())
348+ if err != nil {
349+ t .Fatal (err )
350+ }
351+ if len (p .Addrs ) == 0 {
352+ t .Fatal ("expeced find peer to find addresses." )
353+ }
354+ p , err = d .FindPeer (ctx , wan .PeerID ())
355+ if err != nil {
356+ t .Fatal (err )
357+ }
358+ if len (p .Addrs ) == 0 {
359+ t .Fatal ("expeced find peer to find addresses." )
295360 }
296361}
0 commit comments