| 1 |
# -*- cperl -*- |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use lib './t/lib'; |
| 7 |
use TestHelpers qw(utf8_slurp); |
| 8 |
|
| 9 |
use Test::More; |
| 10 |
use Config; |
| 11 |
use constant MAX_THREADS => 10; |
| 12 |
use constant MAX_LOOP => 50; |
| 13 |
# use constant PLAN => 24; |
| 14 |
BEGIN |
| 15 |
{ |
| 16 |
my $will_run = 0; |
| 17 |
if ( $Config{useithreads} ) |
| 18 |
{ |
| 19 |
if ($ENV{THREAD_TEST}) |
| 20 |
{ |
| 21 |
require threads; |
| 22 |
require threads::shared; |
| 23 |
$will_run = 1; |
| 24 |
} |
| 25 |
else |
| 26 |
{ |
| 27 |
plan skip_all => "optional (set THREAD_TEST=1 to run these tests)"; |
| 28 |
} |
| 29 |
} |
| 30 |
else |
| 31 |
{ |
| 32 |
plan skip_all => "no ithreads in this Perl"; |
| 33 |
} |
| 34 |
|
| 35 |
if ($will_run) |
| 36 |
{ |
| 37 |
plan tests => 24; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
use XML::LibXML qw(:threads_shared); |
| 42 |
# TEST |
| 43 |
ok(1, 'Loaded'); |
| 44 |
|
| 45 |
my $p = XML::LibXML->new(); |
| 46 |
# TEST |
| 47 |
ok($p, 'Parser initted.'); |
| 48 |
|
| 49 |
{ |
| 50 |
for(1..MAX_THREADS) |
| 51 |
{ |
| 52 |
threads->new(sub {}); |
| 53 |
} |
| 54 |
$_->join for(threads->list); |
| 55 |
# TEST |
| 56 |
ok(1, "Simple spawn threads with a parser in scope"); |
| 57 |
} |
| 58 |
|
| 59 |
{ |
| 60 |
my $grammar = <<'EOF'; |
| 61 |
<grammar xmlns="http://relaxng.org/ns/structure/1.0"> |
| 62 |
<start> |
| 63 |
<element name="foo"><empty/></element> |
| 64 |
</start> |
| 65 |
</grammar> |
| 66 |
EOF |
| 67 |
my $r = XML::LibXML::RelaxNG->new(string=>$grammar); |
| 68 |
for(1..MAX_THREADS) { |
| 69 |
threads->new(sub { XML::LibXML::RelaxNG->new(string=>$grammar) }); |
| 70 |
} |
| 71 |
$_->join for(threads->list); |
| 72 |
# TEST |
| 73 |
ok(1, "RelaxNG"); |
| 74 |
} |
| 75 |
|
| 76 |
{ |
| 77 |
eval { XML::LibXML->new->parse_string('foo') }; |
| 78 |
for(1..40) { |
| 79 |
threads->new(sub { eval { XML::LibXML->new->parse_string('foo') } for(1..1000); 1; }); |
| 80 |
} |
| 81 |
$_->join for(threads->list); |
| 82 |
# TEST |
| 83 |
ok(1, "XML error\n"); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
{ |
| 88 |
my $doc=XML::LibXML::Document->new; |
| 89 |
$doc->setDocumentElement($doc->createElement('root')); |
| 90 |
$doc->getDocumentElement->setAttribute('foo','bar'); |
| 91 |
# threads->new(sub { |
| 92 |
# for (1..100000) { |
| 93 |
# # a dictionary of $doc |
| 94 |
# my $el =$doc->createElement('foo'.$_); |
| 95 |
# $el->setAttribute('foo','bar'); |
| 96 |
# } |
| 97 |
# return; |
| 98 |
# }); |
| 99 |
for my $t_no (1..40) { |
| 100 |
threads->new(sub { |
| 101 |
for (1..1000) { |
| 102 |
$doc->getDocumentElement; |
| 103 |
} |
| 104 |
return; |
| 105 |
}); |
| 106 |
} |
| 107 |
$_->join for(threads->list); |
| 108 |
} |
| 109 |
# TEST |
| 110 |
ok(1, "accessing document elements without lock"); |
| 111 |
{ |
| 112 |
my @docs=map { |
| 113 |
my $doc = XML::LibXML::Document->new; |
| 114 |
$doc->setDocumentElement($doc->createElement('root')); |
| 115 |
$doc->getDocumentElement->setAttribute('foo','bar'); |
| 116 |
$doc } 1..40; |
| 117 |
for my $t_no (1..40) { |
| 118 |
threads->new(sub { |
| 119 |
my $doc=$docs[$t_no-1]; |
| 120 |
for (1..10000) { |
| 121 |
# a dictionary of $doc |
| 122 |
my $el =$doc->createElement('foo'.$_); |
| 123 |
$el->setAttribute('foo','bar'); |
| 124 |
$doc->getDocumentElement->getAttribute('foo'); |
| 125 |
$el->getAttribute('foo'); |
| 126 |
} |
| 127 |
return; |
| 128 |
}); |
| 129 |
} |
| 130 |
$_->join for(threads->list); |
| 131 |
} |
| 132 |
# TEST |
| 133 |
ok(1, "operating on different documents without lock\n"); |
| 134 |
|
| 135 |
# operating on the same document with a lock |
| 136 |
{ |
| 137 |
my $lock : shared; |
| 138 |
my $doc=XML::LibXML::Document->new; |
| 139 |
for my $t_no (1..40) { |
| 140 |
threads->new(sub { |
| 141 |
for (1..10000) { |
| 142 |
lock $lock; # must lock since libxml2 uses |
| 143 |
# a dictionary of $doc |
| 144 |
my $el =$doc->createElement('foo'); |
| 145 |
$el->setAttribute('foo','bar'); |
| 146 |
$el->getAttribute('foo'); |
| 147 |
} |
| 148 |
return; |
| 149 |
}); |
| 150 |
} |
| 151 |
$_->join for(threads->list); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
my $xml = <<EOF; |
| 156 |
<?xml version="1.0" encoding="utf-8"?> |
| 157 |
<root><node><leaf/></node></root> |
| 158 |
EOF |
| 159 |
|
| 160 |
{ |
| 161 |
my $doc = $p->parse_string( $xml ); |
| 162 |
for(1..MAX_THREADS) |
| 163 |
{ |
| 164 |
threads->new(sub {}); |
| 165 |
} |
| 166 |
$_->join for(threads->list); |
| 167 |
} |
| 168 |
# TEST |
| 169 |
ok(1, "Spawn threads with a document in scope"); |
| 170 |
|
| 171 |
|
| 172 |
{ |
| 173 |
my $waitfor : shared; |
| 174 |
{ |
| 175 |
lock $waitfor; |
| 176 |
my $doc = $p->parse_string($xml); |
| 177 |
for(1..MAX_THREADS) |
| 178 |
{ |
| 179 |
threads->new(sub { lock $waitfor; $doc->toString; }); |
| 180 |
} |
| 181 |
} |
| 182 |
$_->join for(threads->list); |
| 183 |
# TEST |
| 184 |
ok(1, "Spawn threads that use document that has gone out of scope from where it was created"); |
| 185 |
} |
| 186 |
|
| 187 |
{ |
| 188 |
for(1..MAX_THREADS) |
| 189 |
{ |
| 190 |
threads->new(sub { $p->parse_string($xml) for 1..MAX_LOOP; 1; }); |
| 191 |
} |
| 192 |
$_->join for(threads->list); |
| 193 |
# TEST |
| 194 |
ok(1, "Parse a correct XML document"); |
| 195 |
} |
| 196 |
|
| 197 |
my $xml_bad = <<EOF; |
| 198 |
<?xml version="1.0" encoding="utf-8"?> |
| 199 |
<root><node><leaf/></root> |
| 200 |
EOF |
| 201 |
|
| 202 |
|
| 203 |
{ |
| 204 |
for(1..MAX_THREADS) |
| 205 |
{ |
| 206 |
threads->new(sub { eval { my $x = $p->parse_string($xml_bad)} for(1..MAX_LOOP); 1; }); |
| 207 |
} |
| 208 |
$_->join for(threads->list); |
| 209 |
# TEST |
| 210 |
ok(1, "Parse a bad XML document\n"); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
my $xml_invalid = <<EOF; |
| 215 |
<?xml version="1.0" encoding="utf-8"?> |
| 216 |
<!DOCTYPE root [ |
| 217 |
<!ELEMENT root EMPTY> |
| 218 |
]> |
| 219 |
<root><something/></root> |
| 220 |
EOF |
| 221 |
|
| 222 |
{ |
| 223 |
for(1..MAX_THREADS) |
| 224 |
{ |
| 225 |
threads->new(sub { |
| 226 |
for (1..MAX_LOOP) { |
| 227 |
my $x = $p->parse_string($xml_invalid); |
| 228 |
die if $x->is_valid; |
| 229 |
eval { $x->validate }; |
| 230 |
die unless $@; |
| 231 |
} |
| 232 |
1; |
| 233 |
}); |
| 234 |
} |
| 235 |
$_->join for(threads->list); |
| 236 |
# TEST |
| 237 |
ok(1, "Parse an invalid XML document"); |
| 238 |
} |
| 239 |
|
| 240 |
my $rngschema = <<EOF; |
| 241 |
<?xml version="1.0"?> |
| 242 |
<r:grammar xmlns:r="http://relaxng.org/ns/structure/1.0"> |
| 243 |
<r:start> |
| 244 |
<r:element name="root"> |
| 245 |
<r:attribute name="id"/> |
| 246 |
</r:element> |
| 247 |
</r:start> |
| 248 |
</r:grammar> |
| 249 |
EOF |
| 250 |
|
| 251 |
{ |
| 252 |
for(1..MAX_THREADS) |
| 253 |
{ |
| 254 |
threads->new( |
| 255 |
sub { |
| 256 |
for (1..MAX_LOOP) { |
| 257 |
my $x = $p->parse_string($xml); |
| 258 |
eval { XML::LibXML::RelaxNG->new( string => $rngschema )->validate( $x ) }; |
| 259 |
die unless $@; |
| 260 |
}; 1; |
| 261 |
}); |
| 262 |
} |
| 263 |
$_->join for(threads->list); |
| 264 |
# TEST |
| 265 |
ok(1, "test RNG validation errors are thread safe"); |
| 266 |
} |
| 267 |
|
| 268 |
my $xsdschema = <<EOF; |
| 269 |
<?xml version="1.0"?> |
| 270 |
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
| 271 |
<xsd:element name="root"> |
| 272 |
<xsd:attribute name="partNum" type="SKU" use="required"/> |
| 273 |
</xsd:element> |
| 274 |
</xsd:schema> |
| 275 |
EOF |
| 276 |
|
| 277 |
{ |
| 278 |
for(1..MAX_THREADS) |
| 279 |
{ |
| 280 |
threads->new( |
| 281 |
sub { |
| 282 |
for (1..MAX_LOOP) { |
| 283 |
my $x = $p->parse_string($xml); |
| 284 |
eval { XML::LibXML::Schema->new( string => $xsdschema )->validate( $x ) }; |
| 285 |
die unless $@; |
| 286 |
}; 1; |
| 287 |
}); |
| 288 |
} |
| 289 |
$_->join for(threads->list); |
| 290 |
# TEST |
| 291 |
ok(1, "test Schema validation errors are thread safe"); |
| 292 |
} |
| 293 |
|
| 294 |
my $bigfile = "docs/libxml.dbk"; |
| 295 |
$xml = utf8_slurp($bigfile); |
| 296 |
# TEST |
| 297 |
ok($xml, 'bigfile was slurped fine.'); |
| 298 |
sub use_dom |
| 299 |
{ |
| 300 |
my $d = shift; |
| 301 |
my @nodes = $d->getElementsByTagName("title",1); |
| 302 |
for(@nodes) |
| 303 |
{ |
| 304 |
my $title = $_->toString; |
| 305 |
} |
| 306 |
die unless $nodes[0]->toString eq '<title>XML::LibXML</title>'; |
| 307 |
} |
| 308 |
|
| 309 |
{ |
| 310 |
for(1..MAX_THREADS) { |
| 311 |
threads->new(sub { my $dom = do { $p->parse_string($xml); }; use_dom($dom) for 1..5; 1; }); |
| 312 |
} |
| 313 |
$_->join for(threads->list); |
| 314 |
# TEST |
| 315 |
ok(1, 'Joined all threads.'); |
| 316 |
} |
| 317 |
|
| 318 |
{ |
| 319 |
package MyHandler; |
| 320 |
|
| 321 |
use parent 'XML::SAX::Base'; |
| 322 |
|
| 323 |
sub AUTOLOAD |
| 324 |
{ |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
use XML::LibXML::SAX; |
| 329 |
$p = XML::LibXML::SAX->new( |
| 330 |
Handler=>MyHandler->new(), |
| 331 |
); |
| 332 |
# TEST |
| 333 |
ok($p, 'XML::LibXML::SAX was initted.'); |
| 334 |
|
| 335 |
{ |
| 336 |
for(1..MAX_THREADS) |
| 337 |
{ |
| 338 |
threads->new(sub { $p->parse_string($xml) for (1..5); 1; }); |
| 339 |
} |
| 340 |
$_->join for threads->list; |
| 341 |
|
| 342 |
# TEST |
| 343 |
ok(1, 'After XML::LibXML::SAX - join.'); |
| 344 |
} |
| 345 |
|
| 346 |
$p = XML::LibXML->new( |
| 347 |
Handler=>MyHandler->new(), |
| 348 |
); |
| 349 |
$p->parse_chunk($xml); |
| 350 |
$p->parse_chunk("",1); |
| 351 |
|
| 352 |
{ |
| 353 |
for(1..MAX_THREADS) |
| 354 |
{ |
| 355 |
threads->new(sub { |
| 356 |
$p = XML::LibXML->new(); |
| 357 |
$p->parse_chunk($xml); |
| 358 |
use_dom($p->parse_chunk("",1)); |
| 359 |
1; |
| 360 |
}); |
| 361 |
} |
| 362 |
$_->join for(threads->list); |
| 363 |
# TEST |
| 364 |
ok(1, 'XML::LibXML thread.'); |
| 365 |
} |
| 366 |
|
| 367 |
$p = XML::LibXML->new(); |
| 368 |
# parse a big file using the same parser |
| 369 |
{ |
| 370 |
for(1..MAX_THREADS) |
| 371 |
{ |
| 372 |
threads->new(sub { |
| 373 |
open my $fh, '<', $bigfile |
| 374 |
or die "Cannot open '$bigfile'!"; |
| 375 |
my $doc = $p->parse_fh($fh); |
| 376 |
close $fh; |
| 377 |
2; |
| 378 |
}); |
| 379 |
} |
| 380 |
my @results = $_->join for(threads->list); |
| 381 |
# TEST |
| 382 |
ok(1, 'threads->join after opening bigfile.'); |
| 383 |
} |
| 384 |
|
| 385 |
# create elements |
| 386 |
{ |
| 387 |
my @n = map XML::LibXML::Element->new('bar'.$_), 1..1000; |
| 388 |
for(1..MAX_THREADS) |
| 389 |
{ |
| 390 |
threads->new(sub { |
| 391 |
push @n, map XML::LibXML::Element->new('foo'.$_), 1..1000; |
| 392 |
1; |
| 393 |
}); |
| 394 |
} |
| 395 |
$_->join for(threads->list); |
| 396 |
# TEST |
| 397 |
ok(1, 'create elements'); |
| 398 |
} |
| 399 |
|
| 400 |
{ |
| 401 |
my $e = XML::LibXML::Element->new('foo'); |
| 402 |
for(1..MAX_THREADS) { |
| 403 |
threads->new(sub { |
| 404 |
if ($_[0]==1) { |
| 405 |
my $d = XML::LibXML::Document->new(); |
| 406 |
$d->setDocumentElement($d->createElement('root')); |
| 407 |
$d->documentElement->appendChild($e); |
| 408 |
} |
| 409 |
1; |
| 410 |
},$_); |
| 411 |
} |
| 412 |
$_->join for(threads->list); |
| 413 |
# TEST |
| 414 |
ok(1, "docfrag"); |
| 415 |
} |
| 416 |
|
| 417 |
{ |
| 418 |
my $e = XML::LibXML::Element->new('foo'); |
| 419 |
my $d = XML::LibXML::Document->new(); |
| 420 |
$d->setDocumentElement($d->createElement('root')); |
| 421 |
for(1..MAX_THREADS) { |
| 422 |
threads->new(sub { |
| 423 |
if ($_[0]==1) { |
| 424 |
$d->documentElement->appendChild($e); |
| 425 |
} |
| 426 |
1; |
| 427 |
},$_); |
| 428 |
} |
| 429 |
$_->join for(threads->list); |
| 430 |
# TEST |
| 431 |
ok(1, "docfrag2"); |
| 432 |
} |
| 433 |
|
| 434 |
{ |
| 435 |
my $e = XML::LibXML::Element->new('foo'); |
| 436 |
for(1..MAX_THREADS) { |
| 437 |
threads->new(sub { |
| 438 |
if ($_[0]==1) { |
| 439 |
XML::LibXML::Element->new('root')->appendChild($e); |
| 440 |
} |
| 441 |
1; |
| 442 |
},$_); |
| 443 |
} |
| 444 |
$_->join for(threads->list); |
| 445 |
# TEST |
| 446 |
ok(1, "docfrag3"); |
| 447 |
} |
| 448 |
|