PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/t/CXGN/Tools/wget.t

https://github.com/rayleigh/cxgn-corelibs
Perl | 135 lines | 95 code | 28 blank | 12 comment | 3 complexity | 99b05f229dc229dde7cbfe466df1e3be MD5 | raw file
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Temp qw/tempfile/;
  5. use Test::More tests => 16;
  6. use Test::Exception;
  7. use IO::Pipe;
  8. use POSIX;
  9. use File::Slurp qw/slurp/;
  10. BEGIN {
  11. use_ok( 'CXGN::Tools::Wget', 'wget_filter', 'clear_cache' );
  12. }
  13. my (undef,$tempfile) = tempfile(UNLINK => 1);
  14. lives_ok( sub { wget_filter( 'http://www.sgn.cornell.edu/' => $tempfile ); }, 'fetched http without error' );
  15. ok( -f $tempfile, 'download target exists');
  16. ok( slurp($tempfile) =~ /solanaceae/i, 'download worked');
  17. lives_ok( sub { wget_filter( 'http://www.sgn.cornell.edu/' => $tempfile,
  18. sub {
  19. my $line = shift;
  20. $line =~ s/solanaceae/monkeys in the middle of the desert/i;
  21. return $line;
  22. }
  23. );
  24. },'fetched http without error' );
  25. ok( slurp($tempfile) =~ /monkeys in the middle of the desert/, 'download filters work');
  26. #test downloading from ftp
  27. lives_ok( sub { wget_filter( 'ftp://ftp.sgn.cornell.edu/tomato_genome/bacs/validate_submission.v*.pl' => $tempfile ); },'fetch from ftp ' . $@ );
  28. ok( slurp($tempfile) =~ /BACSubmission/, 'ftp download worked');
  29. SKIP: {
  30. eval { wget_filter('cxgn-resource://nyarlathotep') };
  31. if ($@ =~ m/DBI connect/) {
  32. skip "Could not connect to database", 3;
  33. }
  34. #try to get a nonexistent cxgn-resource url
  35. throws_ok( sub { wget_filter( 'cxgn-resource://no-existe!'); }, qr/no cxgn-resource found/, 'wget of nonexistent resource dies');
  36. #try to get one that exists
  37. my $file = wget_filter( 'cxgn-resource://test' );
  38. ok( -f $file, 'wget of existing resource succeeds' );
  39. unlink $file;
  40. #test test-fetching
  41. # this should die if unsuccessful, we just need to see that it took a
  42. # sufficiently short time
  43. my $begin = time;
  44. wget_filter( 'cxgn-resource://tom_pot_combined_ests', {test_only => 1});
  45. my $fetch_time = time - $begin;
  46. ok( $fetch_time < 10, 'test fetching seems to be working' );
  47. }
  48. #test caching and aging
  49. wget_filter( 'http://tycho.usno.navy.mil/cgi-bin/timer.pl' => $tempfile );
  50. my (undef,$tempfile2) = tempfile(UNLINK => 1);
  51. sleep int rand 3;
  52. wget_filter( 'http://tycho.usno.navy.mil/cgi-bin/timer.pl' => $tempfile2 );
  53. unlike(`diff -q $tempfile $tempfile2`,qr/differ/,'caching works');
  54. sleep 1;
  55. wget_filter( 'http://tycho.usno.navy.mil/cgi-bin/timer.pl' => $tempfile2, {max_age => 1} );
  56. like(`diff -q $tempfile $tempfile2`, qr/differ/, 'caching expiry works');
  57. TODO: {
  58. local $TODO = 'filters do not work with cxgn-resource URLS';
  59. ok( 0 );
  60. }
  61. package TestWebServer;
  62. use HTTP::Server::Simple::CGI;
  63. use base qw(HTTP::Server::Simple::CGI);
  64. use strict;
  65. use warnings;
  66. my $hits = 0;
  67. $|++;
  68. sub handle_request {
  69. my $self = shift;
  70. my $cgi = shift;
  71. print "HTTP/1.0 200 OK\r\n";
  72. respond($cgi);
  73. }
  74. sub respond {
  75. my $cgi = shift;
  76. return if !ref $cgi;
  77. $hits++;
  78. print $cgi->header(), "$hits\n";
  79. }
  80. package main;
  81. TEST_WGET_FILTER_CONCURRENCY();
  82. sub TEST_WGET_FILTER_CONCURRENCY {
  83. # We don't want to use any previously-generated cache
  84. clear_cache();
  85. my $webpid = TestWebServer->new(8080)->background();
  86. diag "Starting a test web server on port 8080, pid = $webpid";
  87. my $pipe = IO::Pipe->new;
  88. if (my $testpid = fork()) { # parent
  89. $pipe->reader;
  90. my $file = wget_filter( 'http://localhost:8080' );
  91. my ($hits) = split /\n/, slurp($file);
  92. is($hits,1,'wget_filter concurrency');
  93. # We don't need the webserver anymore
  94. kill 9, $webpid;
  95. # Read from the child
  96. while( <$pipe> ) {
  97. is($_,1,'wget_filter concurrency');
  98. }
  99. } elsif (defined $testpid) { # child
  100. $pipe->writer;
  101. my $file = wget_filter( 'http://localhost:8080' );
  102. my ($hits) = split /\n/, slurp($file);
  103. print $pipe $hits;
  104. # Don't run END/DESTROY blocks
  105. POSIX::exit(0);
  106. }
  107. }