github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/p2p/peers_test.go (about) 1 package p2p 2 3 import ( 4 "context" 5 "testing" 6 7 p2ptest "github.com/qri-io/qri/p2p/test" 8 ) 9 10 // Convert from test nodes to non-test nodes. 11 func asQriNodes(testPeers []p2ptest.TestablePeerNode) []*QriNode { 12 // Convert from test nodes to non-test nodes. 13 peers := make([]*QriNode, len(testPeers)) 14 for i, node := range testPeers { 15 peers[i] = node.(*QriNode) 16 } 17 return peers 18 } 19 20 // this test is the poster child for re-vamping how we do our p2p test networks 21 func TestConnectedQriProfiles(t *testing.T) { 22 t.Skip("TODO (ramfox): test is flakey. See comments for full details") 23 ctx := context.Background() 24 // TODO (ramfox): commented out my work on this test to show what attempts 25 // have been made and why they aren't currently working 26 // In order to make sure that we have all the Qri Profiles of the nodes we 27 // have connected to, we need to make sure that the main node has upgraded 28 // each node to a qri connection. However, this is not the only way to have 29 // all the profiles in the network, because of the way 30 // `upgradeToQriConnection` works, after upgrading a connection, the node 31 // then requests all the profiles that the connected peer has, and connects 32 // to all those peers) 33 // unfortunately, this makes the test super non-deterministic, and it passes 34 // and fails for different reasons 35 36 // bus := event.NewBus(ctx) 37 38 // // IMPORTANT: we are waiting for 4 instances of the ETP2PQriPeerConnected events 39 // // because currently there are 5 nodes in the 40 // numConns := 4 41 // waitCh := make(chan struct{}, 1) 42 // // This actually fails and causes the test to hang because calling `upgradeQriConnection` 43 // // can sometimes result in a non-error response, with NO event publication that a connection 44 // // was upgraded (because the peer was theoretically already seen) 45 // watchP2PQriPeersConnected := func(_ context.Context, t event.Type, payload interface{}) error { 46 // if t == event.ETP2PQriPeerConnected { 47 // pro, ok := payload.(*profile.Profile) 48 // if !ok { 49 // return fmt.Errorf("payload for event.ETP2PQriPeerConnected not a *profile.Profile as expected") 50 // } 51 // fmt.Println("Qri Peer Connected: ", pro.PeerIDs[0]) 52 // numConns-- 53 // if numConns == 0 { 54 // waitCh <- struct{}{} 55 // } 56 // } 57 // return nil 58 // } 59 // bus.Subscribe(watchP2PQriPeersConnected, event.ETP2PQriPeerConnected) 60 61 f := p2ptest.NewTestNodeFactory(NewTestableQriNode) 62 testPeers, err := p2ptest.NewTestDirNetwork(ctx, f) 63 if err != nil { 64 t.Fatalf("error creating network: %s", err.Error()) 65 } 66 nodes := asQriNodes(testPeers) 67 68 // nodes[0].pub = bus 69 70 if err := p2ptest.ConnectNodes(ctx, testPeers); err != nil { 71 t.Fatalf("error connecting peers: %s", err.Error()) 72 } 73 74 // // this can sometimes hang forever 75 // <-waitCh 76 pros := nodes[0].ConnectedQriProfiles(ctx) 77 if len(pros) != len(nodes)-1 { 78 t.Log("My ID:", nodes[0].host.ID()) 79 t.Log("Conns:") 80 for _, conn := range nodes[0].host.Network().Conns() { 81 t.Log(" ", conn) 82 } 83 t.Log("Profiles:") 84 for _, pro := range pros { 85 t.Log(" ", pro) 86 } 87 t.Errorf("wrong number of connected profiles. expected: %d, got: %d", len(nodes), len(pros)) 88 return 89 } 90 91 for _, pro := range pros { 92 if !pro.Online { 93 t.Errorf("expected profile %s to have Online == true", pro.Peername) 94 } 95 } 96 }