File: Multithreading.t

package info (click to toggle)
libxml-bare-perl 0.53-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,388 kB
  • sloc: xml: 15,836; perl: 1,331; ansic: 1,025; cpp: 41; makefile: 3
file content (72 lines) | stat: -rwxr-xr-x 1,943 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/perl -w
use strict;
use Test::More;

# Note that the strategy for testing for thread failure here is not very good.
# It is very timing dependent. On some systems this test will pass even with a parser
# that is not thread safe. There is some amount of luck in getting the non-thread safe
# code to crash. As it is now, the test below sucessfully crashes version 0.45 of XML::Bare
# seemingly every time I run the test.

my $threads_ok = 0;
eval("use threads;");
if( !$@ ) { $threads_ok = 1; }

my $shared_ok = 0;
if( $threads_ok ) {
    eval("use threads::shared;");
    if( !$@ ) { $shared_ok = 1; }
}

my $numok = 0;

if( !$threads_ok || !$shared_ok ) {
     plan skip_all => 'Cannot load threads and/or threads::shared; skipping multithreading tests';
}
else {
    #plan 'no_plan';
    plan tests => 2;
    use_ok( 'XML::Bare' );
    threads::shared::share( \$numok );
    for( my $i=0;$i<20;$i++ ) {
        threads->create( \&single );
    }
    while( 1 ) {
        my @joinable = threads->list(0);#joinable
        my @running = threads->list(1);#running
        
        for my $thr ( @joinable ) { $thr->join(); }
        last if( !@running );
        sleep(1);
    }
    is( $numok, 20, 'All threads completed okay' );
}

sub single {
    my $xml = '<xml>';
    my @arr;
    for( my $i=0;$i<4000;$i++ ) {
        my $n = rand(1000);
        $arr[ $i ] = $n;
        $xml .= "<node><n>$n</n></node>";
    }
    $xml .= "</xml>";
    my ( $ob, $root ) = XML::Bare->new( text => $xml );
    $root = $root->{'xml'};
    my $nodes = $root->{'node'};
    my $ok = 1;
    my $i = 0;
    for my $node ( @$nodes ) {
        my $n = $node->{'n'}{'value'};
        $ok = 0 if( $n ne $arr[ $i ] ); # note ne here instead of !=. Because $a=405.69280607542502; $a!="$a"; But $a=405.69280607542501; $a=="$a"; :(
        $i++;
    }
    @arr = ();
    
    return if( !$ok );
    
    {
        lock( $numok );
        $numok++;
    }
}