3 use Test
::Depends
qw(Coro);
4 use Test
::More no_plan
;
9 BEGIN { use_ok
("VCS::Git::Torrent::Peer::Async") }
11 # In this test script, we will set up two peers and check that they
12 # can successfully handshake, and disconnect gracefully.
14 my $port_1 = int(rand(2**16-1024-1)+1024);
15 my $port_2 = $port_1 + 1;
17 my $dummy_torrent = VCS
::Git
::Torrent
->new(repo_hash
=> "6549" x
10,
22 print STDERR
"OW SIGPIPE\n";
25 my $peer_1 = VCS
::Git
::Torrent
::Peer
::Async
->new
27 torrent
=> $dummy_torrent,
28 peer_id
=> "AlphaPeer1AlphaPeer1",
30 ok
($peer_1, "Made a new peer");
32 my $peer_2 = VCS
::Git
::Torrent
::Peer
::Async
->new
34 torrent
=> $dummy_torrent,
35 peer_id
=> "BravoPeer2BravoPeer2",
37 ok
($peer_2, "Made another new peer");
39 $peer_2->connect("localhost:$port_1");
41 is
(Coro
::nready
, 3, "All set up and ready to go");
49 ok
($peer_1->num_connections, "Peer 1 found a peer")
50 or skip
"no connection made", 9;
52 ok
(!$peer_2->connections->[0]->choked_in,
53 "Peer 2 is not yet feeling the choke");
55 use VCS
::Git
::Torrent
::PWP
qw(:pwp_constants);
56 my $victim = $peer_1->connections->[0]->remote;
57 ok
($victim, "Peer 1 knows someone")
58 or skip
"doesn't know anyone!", 7;
60 isnt
($victim, $peer_2, "Peer 2 and victim look different");
61 # this won't match. The outgoing ports are different.
62 #is($victim->port, $peer_2->port, "But they share a port");
63 is
($victim->peer_id, $peer_2->peer_id, "And a Peer ID!");
66 $peer_1->send_message($victim, GTP_PWP_CHOKE
);
67 pass
("Sent a message to the victim");
69 my $conn = $peer_2->connections->[0];
75 "Peer 2 felt the choke");
78 $peer_1->send_message($victim, GTP_PWP_UNCHOKE
);
84 ok
(!$conn->choked_in, "Peer 2 is breathing again!");
87 $peer_2->send_message($conn->remote, GTP_PWP_CHOKE
);
90 $peer_1->hangup ($victim);
91 is
($peer_1->num_connections, 0, "Hung up on the victim");
96 is
( $peer_2->num_connections, 0,
97 "Peer 2 detected the hangup");
102 is
(Coro
::nready
, 0, "Nothing left running!");