~ubuntu-branches/ubuntu/utopic/libxml-bare-perl/utopic-proposed

« back to all changes in this revision

Viewing changes to t/Multithreading.t

  • Committer: Package Import Robot
  • Author(s): Nuno Carvalho, gregor herrmann, Salvatore Bonaccorso, Axel Beckert, Nuno Carvalho
  • Date: 2013-09-17 15:54:28 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130917155428-4d0xb5cissw2323f
Tags: 0.53-1
* Team upload.

[ gregor herrmann ]
* debian/control: update {versioned,alternative} (build) dependencies.

[ Salvatore Bonaccorso ]
* Change Vcs-Git to canonical URI (git://anonscm.debian.org)
* Change search.cpan.org based URIs to metacpan.org based URIs

[ Axel Beckert ]
* debian/copyright: migrate pre-1.0 format to 1.0 using "cme fix dpkg-
  copyright"

[ Nuno Carvalho ]
* New upstream release.
* debian/copyright: update copyright years.
* debian/control: update standards version.
* debian/control: update debhelper required version, in order to pass all
  the hardening flags to EUMM.
* Add lintian override to apparently false-positive warning.
* Add set of patches accepted upstream but still not included in this
  release, visit https://rt.cpan.org/Public/Bug/Display.html?id=88155
  for details.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
use strict;
 
3
use Test::More;
 
4
 
 
5
# Note that the strategy for testing for thread failure here is not very good.
 
6
# It is very timing dependent. On some systems this test will pass even with a parser
 
7
# that is not thread safe. There is some amount of luck in getting the non-thread safe
 
8
# code to crash. As it is now, the test below sucessfully crashes version 0.45 of XML::Bare
 
9
# seemingly every time I run the test.
 
10
 
 
11
my $threads_ok = 0;
 
12
eval("use threads;");
 
13
if( !$@ ) { $threads_ok = 1; }
 
14
 
 
15
my $shared_ok = 0;
 
16
if( $threads_ok ) {
 
17
    eval("use threads::shared;");
 
18
    if( !$@ ) { $shared_ok = 1; }
 
19
}
 
20
 
 
21
my $numok = 0;
 
22
 
 
23
if( !$threads_ok || !$shared_ok ) {
 
24
     plan skip_all => 'Cannot load threads and/or threads::shared; skipping multithreading tests';
 
25
}
 
26
else {
 
27
    #plan 'no_plan';
 
28
    plan tests => 2;
 
29
    use_ok( 'XML::Bare' );
 
30
    threads::shared::share( \$numok );
 
31
    for( my $i=0;$i<20;$i++ ) {
 
32
        threads->create( \&single );
 
33
    }
 
34
    while( 1 ) {
 
35
        my @joinable = threads->list(0);#joinable
 
36
        my @running = threads->list(1);#running
 
37
        
 
38
        for my $thr ( @joinable ) { $thr->join(); }
 
39
        last if( !@running );
 
40
        sleep(1);
 
41
    }
 
42
    is( $numok, 20, 'All threads completed okay' );
 
43
}
 
44
 
 
45
sub single {
 
46
    my $xml = '<xml>';
 
47
    my @arr;
 
48
    for( my $i=0;$i<4000;$i++ ) {
 
49
        my $n = rand(1000);
 
50
        $arr[ $i ] = $n;
 
51
        $xml .= "<node><n>$n</n></node>";
 
52
    }
 
53
    $xml .= "</xml>";
 
54
    my ( $ob, $root ) = XML::Bare->new( text => $xml );
 
55
    $root = $root->{'xml'};
 
56
    my $nodes = $root->{'node'};
 
57
    my $ok = 1;
 
58
    my $i = 0;
 
59
    for my $node ( @$nodes ) {
 
60
        my $n = $node->{'n'}{'value'};
 
61
        $ok = 0 if( $n ne $arr[ $i ] ); # note ne here instead of !=. Because $a=405.69280607542502; $a!="$a"; But $a=405.69280607542501; $a=="$a"; :(
 
62
        $i++;
 
63
    }
 
64
    @arr = ();
 
65
    
 
66
    return if( !$ok );
 
67
    
 
68
    {
 
69
        lock( $numok );
 
70
        $numok++;
 
71
    }
 
72
}