[doc] lib/VCS/Git/Torrent/Peer/Async/State.pm
[VCS-Git-Torrent.git] / t / 50-pwp-handshake.t
blobf2ad3b8b20711ffee239f50a9e5d1583d225d632
1 #!/usr/bin/perl
3 use Test::Depends qw(Coro);
4 use Test::More no_plan;
5 use strict;
6 use warnings;
7 use t::TestUtils;
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,
18 git => tmp_git,
21 $SIG{PIPE} = sub {
22 print STDERR "OW SIGPIPE\n";
25 my $peer_1 = VCS::Git::Torrent::Peer::Async->new
26 ( port => $port_1,
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
33 ( port => $port_2,
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");
43 use Coro::Event;
44 #use Coro;
45 Coro::Event::loop(1);
46 #diag("finished");
48 SKIP:{
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!");
65 # get 'im!
66 $peer_1->send_message($victim, GTP_PWP_CHOKE);
67 pass("Sent a message to the victim");
69 my $conn = $peer_2->connections->[0];
71 Coro::Event::sweep;
72 cede;
74 ok($conn->choked_in,
75 "Peer 2 felt the choke");
77 # lets revive them.
78 $peer_1->send_message($victim, GTP_PWP_UNCHOKE);
80 Coro::Event::sweep;
81 cede;
83 # did they make it?
84 ok(!$conn->choked_in, "Peer 2 is breathing again!");
86 # peer 2 retaliates
87 $peer_2->send_message($conn->remote, GTP_PWP_CHOKE);
89 # but it's too late!
90 $peer_1->hangup ($victim);
91 is($peer_1->num_connections, 0, "Hung up on the victim");
92 $peer_1->shutdown;
94 Coro::Event::loop(1);
96 is ( $peer_2->num_connections, 0,
97 "Peer 2 detected the hangup");
99 $peer_2->shutdown;
102 is(Coro::nready, 0, "Nothing left running!");