Ignore:
Timestamp:
01/23/12 06:00:05 (4 months ago)
Author:
PaulHarvey
Message:

Item11431: Clean UnitTests of Foswiki->new/finish

Committing what was 96 commits rebased on top of Foswikirev:13781. You
can view this full commit history at
 https://github.com/csirac2/UnitTestContrib/commits/Item11431-rebased

Tackling memory leaks, this effort seems to have saved 300MB and 10
minutes from the cost of running a full FoswikiSuite.

The recipe was:

  • Find and remove all Foswiki->new/finish calls with $this->createNewFoswikiSession()
  • Generally removed $fatwilly variables and replaced with $this->{session}, to avoid references to ->finish()'d Foswiki singleton/session objects
  • Addressed perlcriticisms in touched files
  • Convert Foswiki::Meta->new/load to Foswiki::Func::readTopic, for the sake of store2 re-integration
  • Call $topicObject->finish() where/whenever we can

Some of the tests in the default plugins need a little work, and
there's some uncommitted stuff against core lib/Foswiki.pm to help
assert SINGLE_SINGLETONS from Foswiki::new/::finish subs, we'll get
to that later...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UnitTestContrib/test/unit/CacheTests.pm

    r13730 r13791  
    11package CacheTests; 
     2use strict; 
     3use warnings; 
    24 
    3 use FoswikiFnTestCase; 
     5use FoswikiFnTestCase(); 
    46our @ISA = qw( FoswikiFnTestCase ); 
    57 
    6 use strict; 
    7 use Foswiki; 
    8 use Foswiki::Meta; 
     8use Foswiki(); 
     9use Foswiki::Meta(); 
     10use File::Spec(); 
     11use Foswiki::OopsException(); 
     12use Foswiki::PageCache(); 
    913use Error qw( :try ); 
    10 use Foswiki::OopsException; 
    11 use Foswiki::PageCache; 
    1214use Benchmark qw(:hireswallclock); 
    1315 
     
    1719    my $this = shift; 
    1820    my @page; 
     21 
    1922    foreach my $dir (@INC) { 
    20         if ( opendir( D, "$dir/Foswiki/Cache" ) ) { 
    21             foreach my $alg ( readdir D ) { 
     23        if ( opendir( my $D, File::Spec->catdir( $dir, 'Foswiki', 'Cache' ) ) ) 
     24        { 
     25            foreach my $alg ( readdir $D ) { 
    2226                next unless $alg =~ s/^(.*)\.pm$/$1/; 
    23                 next if defined &$alg; 
     27                next if defined &{$alg}; 
    2428                $ENV{PATH} =~ /^(.*)$/ms; 
    25                 $ENV{PATH} = $1; 
     29                local $ENV{PATH} = $1; 
    2630                ($alg) = $alg =~ /^(.*)$/ms; 
    27                 eval "require Foswiki::Cache::$alg"; 
    28                 if ($@) { 
    29                     print STDERR 
    30 "Cannot test Foswiki::Cache::$alg\nCompilation error when trying to 'require' it\n"; 
    31                 } 
    32                 else { 
     31 
     32                if ( eval "require Foswiki::Cache::$alg; 1;" ) { 
    3333                    no strict 'refs'; 
    34                     *$alg = sub { 
     34                    *{$alg} = sub { 
    3535                        my $this = shift; 
    3636                        $Foswiki::cfg{CacheManager} = 'Foswiki::Cache::' . $alg; 
     
    3939                    push( @page, $alg ); 
    4040                } 
     41                else { 
     42                    print STDERR 
     43"Cannot test Foswiki::Cache::$alg\nCompilation error when trying to 'require' it\n"; 
     44                } 
    4145            } 
    42             closedir(D); 
     46            closedir($D); 
    4347        } 
    4448    } 
     
    5054sub DBFileMeta { 
    5155    $Foswiki::cfg{MetaCacheManager} = 'Foswiki::Cache::DB_File'; 
     56 
     57    return; 
    5258} 
    5359 
    5460sub BDBMeta { 
    5561    $Foswiki::cfg{MetaCacheManager} = 'Foswiki::Cache::BDB'; 
     62 
     63    return; 
    5664} 
    5765 
     
    5967    $Foswiki::cfg{HttpCompress} = 1; 
    6068    $Foswiki::cfg{Cache}{Compress} = 1; 
     69 
     70    return; 
    6171} 
    6272 
     
    6474    $Foswiki::cfg{HttpCompress} = 0; 
    6575    $Foswiki::cfg{Cache}{Compress} = 0; 
     76 
     77    return; 
    6678} 
    6779 
     
    90102    $Foswiki::cfg{Cache}{Compress} = 0; 
    91103    $UI_FN ||= $this->getUIFn('view'); 
    92 } 
    93104 
    94 sub tear_down { 
    95     my $this = shift; 
    96     $this->SUPER::tear_down(); 
     105    return; 
    97106} 
    98107 
     
    102111    $UI_FN ||= $this->getUIFn('view'); 
    103112 
    104     my $query = new Unit::Request( { skin => ['none'], } ); 
     113    my $query = Unit::Request->new( { skin => ['none'], } ); 
    105114    $query->path_info("/"); 
    106115    $query->method('POST'); 
    107116 
    108     my $fatwilly = new Foswiki( $this->{test_user_login}, $query ); 
     117    $this->createNewFoswikiSession( $this->{test_user_login}, $query ); 
    109118 
    110119    # This first request should *not* be satisfied from the cache, but 
    111120    # the cache should be populated with the result. 
    112     my $p1start = new Benchmark(); 
     121    my $p1start = Benchmark->new(); 
    113122    my ($one) = $this->capture( 
    114123        sub { 
    115124            no strict 'refs'; 
    116             &$UI_FN($fatwilly); 
     125            &{$UI_FN}( $this->{session} ); 
    117126            use strict 'refs'; 
    118             $Foswiki::engine->finalize( $fatwilly->{response}, 
    119                 $fatwilly->{request} ); 
     127            $Foswiki::engine->finalize( $this->{session}{response}, 
     128                $this->{session}{request} ); 
    120129        } 
    121130    ); 
    122131 
    123     my $p1end = new Benchmark(); 
     132    my $p1end = Benchmark->new(); 
    124133    print STDERR "R1 " . timestr( timediff( $p1end, $p1start ) ) . "\n"; 
    125     $fatwilly->finish(); 
    126134 
    127     $fatwilly = new Foswiki( $this->{test_user_login}, $query ); 
     135    $this->createNewFoswikiSession( $this->{test_user_login}, $query ); 
    128136 
    129137    # This second request should be satisfied from the cache 
    130     my $p2start = new Benchmark(); 
     138    my $p2start = Benchmark->new(); 
    131139    my ($two) = $this->capture( 
    132140        sub { 
    133141            no strict 'refs'; 
    134             &$UI_FN($fatwilly); 
     142            &{$UI_FN}( $this->{session} ); 
    135143            use strict 'refs'; 
    136             $Foswiki::engine->finalize( $fatwilly->{response}, 
    137                 $fatwilly->{request} ); 
     144            $Foswiki::engine->finalize( $this->{session}{response}, 
     145                $this->{session}{request} ); 
    138146        } 
    139147    ); 
    140     my $p2end = new Benchmark(); 
     148    my $p2end = Benchmark->new(); 
    141149    print STDERR "R2 " . timestr( timediff( $p2end, $p2start ) ) . "\n"; 
    142     $fatwilly->finish(); 
    143150 
    144151    # Massage the HTML for comparison 
     
    164171 
    165172    $this->assert_html_equals( $one, $two ); 
     173 
     174    return; 
    166175} 
    167176 
Note: See TracChangeset for help on using the changeset viewer.