Add packfile caching
[VCS-Git-Torrent.git] / t / 52-pwp-references.t
blob7650bfe2cac214831edcec648b62ee65bcd99ee2
1 #!/usr/bin/perl
3 use Test::More no_plan;
4 use strict;
5 use warnings;
6 use MooseX::TimestampTZ;
8 # In this test script, a node joins a swarm that has a more up-to-date
9 # reference list than the one it got from the tracker, and updates its
10 # reference list from a peer.
12 BEGIN {
13 use_ok('t::TestUtils');
15 use_ok('Coro');
16 use_ok('Coro::Event');
17 use_ok('VCS::Git::Torrent');
18 use_ok('VCS::Git::Torrent::Peer::Async');
19 use_ok('VCS::Git::Torrent::PWP', qw(:pwp_constants));
20 use_ok('VCS::Git::Torrent::Reference');
23 my $path = mk_tmp_repo();
24 my $git = Git->repository($path);
25 ok($git, 'git repo created');
27 my $t = VCS::Git::Torrent->new(
28 repo_hash => '501d' x 10,
29 git => $git,
31 ok($t, 'gittorrent created');
33 qx(echo foo >> $path/bar);
34 $git->command('add', 'bar');
35 $git->command('commit', '-a', '-m baz');
36 my $sha1 = $git->command_oneline('show-ref', '-s', 'master');
37 like($sha1, qr/[[:xdigit:]]{40}/, 'object inserted successfully');
39 my $ref = VCS::Git::Torrent::Reference->new(
40 torrent => $t,
41 tagged_object => $sha1,
42 refs => { 'HEAD' => $sha1 },
43 tagger => 'Elanour Rigby <nowhereman@example.com>',
44 tagdate => timestamptz,
45 comment => "Ah, look at all the lonely people\n",
47 ok($ref, 'Reference created');
49 $t->references([$ref]);
51 my ($port_1, $port_2) = @{ random_port_pair() };
53 my $peer_1 = VCS::Git::Torrent::Peer::Async->new(
54 port => $port_1,
55 torrent => $t,
56 peer_id => 'AlphaPeer1AlphaPeer1',
58 ok($peer_1, 'Peer created');
60 my $path2 = mk_tmp_repo();
61 my $git2 = Git->repository($path2);
62 my $t2 = VCS::Git::Torrent->new(
63 repo_hash => '501d' x 10,
64 git => $git2,
67 my $peer_2 = VCS::Git::Torrent::Peer::Async->new(
68 port => $port_2,
69 torrent => $t2,
70 peer_id => 'BravoPeer2BravoPeer2',
72 ok($peer_2, 'Another peer created');
74 $peer_1->connect("localhost:$port_2");
76 # let the connect get processed
77 Coro::Event::loop(1);
79 my $victim = $peer_2->connections->[0]->remote;
80 $peer_2->send_message($victim, GTP_PWP_REFERENCES);
82 Coro::Event::sweep;
83 cede;
85 Coro::Event::loop(1);
87 is(scalar(@{ $peer_2->references }), 1, 'we received a reference from our peer');
89 for my $attr ( qw(tag_id tagged_object tagger tagdate comment refs) ) {
90 is_deeply(
91 $peer_2->references->[0]->$attr,
92 $ref->$attr,
93 'reconstructed Reference: ' . $attr . ' matches'