Add Digest::SHA1 to module dependencies
[VCS-Git-Torrent.git] / t / 55-pwp-reels.t
blobd3fa231aa74eafe6eb9ec2ea59fdab9805dd1ea9
1 #!/usr/bin/perl
3 use Test::More no_plan;
4 use strict;
5 use warnings;
7 BEGIN {
8 use_ok('t::TestUtils');
10 use_ok('Coro');
11 use_ok('Coro::Event');
12 use_ok('VCS::Git::Torrent');
13 use_ok('VCS::Git::Torrent::CommitReel::Local');
14 use_ok('VCS::Git::Torrent::Peer::Async');
15 use_ok('VCS::Git::Torrent::PWP', qw(:pwp_constants));
16 # use_ok('VCS::Git::Torrent::PWP::Message::Blocks');
19 # In this test, two peers connect and exchange reel (and reference)
20 # information
22 my $repo_hash = '501d' x 10;
24 my $git_1 = Git->repository('.');
25 my $git_2 = tmp_git();
27 my $torrent_1 = VCS::Git::Torrent->new(
28 repo_hash => $repo_hash,
29 git => $git_1,
32 my $torrent_2 = VCS::Git::Torrent->new(
33 repo_hash => $repo_hash,
34 git => $git_2,
37 # for now, we'll use a hard-coded ref which is known to be in the
38 # history of this project.
39 my $TEST_COMMIT = '7377253c66201c515f723f909830b3557fb6fa74';
41 my $reference_1 = VCS::Git::Torrent::Reference->new(
42 torrent => $torrent_1,
43 tagged_object => $TEST_COMMIT,
44 tagger => 'Nobody <dev@null.org>',
45 tagdate => '2008-07-18 12:00:00-0400',
46 comment => "Created by $0",
47 refs => {
48 'refs/heads/master' => $TEST_COMMIT,
51 ok($reference_1, 'Reference created');
53 my $reel_1 = VCS::Git::Torrent::CommitReel::Local->new(
54 torrent => $torrent_1,
55 end => $reference_1,
57 ok($reel_1, 'made the reel OK');
59 # this also gives the reel a link to the torrent
60 $torrent_1->reels([$reel_1]);
62 my ($port_1, $port_2) = @{ random_port_pair() };
64 my $peer_1 = VCS::Git::Torrent::Peer::Async->new(
65 port => $port_1,
66 torrent => $torrent_1,
67 peer_id => 'AlphaPeer1AlphaPeer1',
68 reels => [ $reel_1 ],
70 ok($peer_1, 'Peer created');
72 my $peer_2 = VCS::Git::Torrent::Peer::Async->new(
73 port => $port_2,
74 torrent => $torrent_2,
75 peer_id => 'BravoPeer2BravoPeer2',
77 ok($peer_2, 'Another peer created');
79 $peer_1->connect("localhost:$port_2");
81 # let the connect get processed
82 Coro::Event::loop(1);
84 # ask $peer_1 for reels
85 my $victim = $peer_2->connections->[0]->remote;
86 $peer_2->send_message($victim, GTP_PWP_REELS);
88 # let our message get there, and the reply back to us
89 Coro::Event::loop(1);
91 is(scalar(@{$peer_2->torrent->reels}), 1, 'peer_2 received a reel');
92 my $reel_2 = $peer_2->torrent->reels->[0];
94 $peer_2->send_message($victim, GTP_PWP_BLOCKS, $reel_2);
96 Coro::Event::loop(1);
98 my $bits = @{ $peer_2->connections->[0]->remote->reels->[0]->commit_info };
99 is($bits, 71, 'received 71 commit bits');