~ubuntu-branches/ubuntu/lucid/libxml-libxml-perl/lucid

« back to all changes in this revision

Viewing changes to t/90threads.t

  • Committer: Bazaar Package Importer
  • Author(s): gregor herrmann
  • Date: 2009-05-31 14:36:13 UTC
  • mfrom: (4.1.7 jaunty)
  • Revision ID: james.westby@ubuntu.com-20090531143613-xxpnwmrz62kwtejq
Tags: 1.69.ds-2
* Remove Florian Ragwitz from Uploaders (closes: #523275).
* Set Standards-Version to 3.8.1 (no changes).
* Remove duplicate fields from debian/control.
* Minimize debian/rules, bump quilt and debhelper build dependencies.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- cperl -*-
1
2
use Test;
2
3
use Config;
3
4
use constant MAX_THREADS => 10;
4
5
use constant MAX_LOOP => 50;
5
 
use constant PLAN => 16;
 
6
use constant PLAN => 24;
6
7
BEGIN {
7
8
  plan tests => PLAN;
8
9
  if( $Config{useithreads} ) {
9
10
    if ($ENV{THREAD_TEST}) {
10
 
      require threads;;
11
 
          require threads::shared;
 
11
      require threads;
 
12
      require threads::shared;
12
13
    } else {
13
14
      skip("optional (set THREAD_TEST=1 to run these tests)\n") for (1..PLAN);
14
15
      exit;
18
19
    exit;
19
20
  }
20
21
}
21
 
use XML::LibXML;
 
22
use XML::LibXML qw(:threads_shared);
22
23
ok(1);
23
24
 
24
25
my $p = XML::LibXML->new();
25
26
ok($p);
26
27
 
27
 
# Simple spawn threads with $p in scope
 
28
 
 
29
 
 
30
print "Simple spawn threads with a parser in scope\n";
28
31
{
29
32
for(1..MAX_THREADS)
30
33
{
34
37
ok(1);
35
38
}
36
39
 
 
40
print "RelaxNG\n";
 
41
{
 
42
  my $grammar = <<'EOF';
 
43
<grammar xmlns="http://relaxng.org/ns/structure/1.0">
 
44
<start>
 
45
  <element name="foo"><empty/></element>
 
46
</start>
 
47
</grammar>
 
48
EOF
 
49
  my $r = XML::LibXML::RelaxNG->new(string=>$grammar);
 
50
  for(1..MAX_THREADS) {
 
51
      threads->new(sub { XML::LibXML::RelaxNG->new(string=>$grammar) });
 
52
    }
 
53
  $_->join for(threads->list);
 
54
  ok(1);
 
55
}
 
56
 
 
57
print "XML error\n";
 
58
{
 
59
eval { XML::LibXML->new->parse_string('foo') };
 
60
for(1..40) {
 
61
        threads->new(sub { eval { XML::LibXML->new->parse_string('foo') } for(1..1000);  1; });
 
62
}
 
63
$_->join for(threads->list);
 
64
ok(1);
 
65
}
 
66
 
 
67
 
 
68
print "accessing document elements without lock\n";
 
69
{
 
70
  my $doc=XML::LibXML::Document->new;
 
71
  $doc->setDocumentElement($doc->createElement('root'));
 
72
  $doc->getDocumentElement->setAttribute('foo','bar');
 
73
#   threads->new(sub {
 
74
#                for (1..100000) {
 
75
#                  # a dictionary of $doc
 
76
#                  my $el =$doc->createElement('foo'.$_);
 
77
#                  $el->setAttribute('foo','bar');
 
78
#                }
 
79
#                return;
 
80
#              });
 
81
  for my $t_no (1..40) {
 
82
    threads->new(sub {
 
83
                   for (1..1000) {
 
84
                     $doc->getDocumentElement;
 
85
                   }
 
86
                   return;
 
87
                 });
 
88
  }
 
89
  $_->join for(threads->list);
 
90
}
 
91
ok(1);
 
92
print "operating on different documents without lock\n";
 
93
{
 
94
  my @docs=map {
 
95
    my $doc = XML::LibXML::Document->new;
 
96
    $doc->setDocumentElement($doc->createElement('root'));
 
97
    $doc->getDocumentElement->setAttribute('foo','bar');
 
98
    $doc } 1..40;
 
99
  for my $t_no (1..40) {
 
100
    threads->new(sub {
 
101
                   my $doc=$docs[$t_no-1];
 
102
                   for (1..10000) {
 
103
                     # a dictionary of $doc
 
104
                     my $el =$doc->createElement('foo'.$_);
 
105
                     $el->setAttribute('foo','bar');
 
106
                     $doc->getDocumentElement->getAttribute('foo');
 
107
                     $el->getAttribute('foo');
 
108
                   }
 
109
                   return;
 
110
                 });
 
111
  }
 
112
  $_->join for(threads->list);
 
113
}
 
114
ok(1);
 
115
print "operating on the same document with a lock\n";
 
116
{
 
117
  my $lock : shared;
 
118
  my $doc=XML::LibXML::Document->new;
 
119
  for my $t_no (1..40) {
 
120
    threads->new(sub {
 
121
                   for (1..10000) {
 
122
                     lock $lock; # must lock since libxml2 uses
 
123
                                 # a dictionary of $doc
 
124
                     my $el =$doc->createElement('foo');
 
125
                     $el->setAttribute('foo','bar');
 
126
                     $el->getAttribute('foo');
 
127
                   }
 
128
                   return;
 
129
                 });
 
130
  }
 
131
  $_->join for(threads->list);
 
132
}
 
133
 
 
134
 
37
135
my $xml = <<EOF;
38
136
<?xml version="1.0" encoding="utf-8"?>
39
137
<root><node><leaf/></node></root>
40
138
EOF
41
139
 
42
 
# Spawn threads with a document in scope
 
140
print "Spawn threads with a document in scope\n";
43
141
{
44
142
my $doc = $p->parse_string( $xml );
45
143
for(1..MAX_THREADS)
50
148
}
51
149
ok(1);
52
150
 
53
 
# Spawn threads that use document that has gone out of scope from where it was
54
 
# created
 
151
print "Spawn threads that use document that has gone out of scope from where it was created\n";
55
152
{
56
153
my $waitfor : shared;
57
154
{
66
163
ok(1);
67
164
}
68
165
 
69
 
# Parse a correct XML document
 
166
print "Parse a correct XML document\n";
70
167
{
71
168
for(1..MAX_THREADS)
72
169
{
81
178
<root><node><leaf/></root>
82
179
EOF
83
180
 
84
 
# Parse a bad XML document
 
181
 
 
182
print "Parse a bad XML document\n";
85
183
{
86
184
for(1..MAX_THREADS)
87
185
{
100
198
<root><something/></root>
101
199
EOF
102
200
 
103
 
# Parse an invalid XML document
 
201
print "Parse an invalid XML document\n";
104
202
{
105
203
for(1..MAX_THREADS)
106
204
{
129
227
</r:grammar>
130
228
EOF
131
229
 
132
 
# test RNG validation errors are thread safe
 
230
print "test RNG validation errors are thread safe\n";
133
231
{
134
232
for(1..MAX_THREADS)
135
233
{
155
253
</xsd:schema>
156
254
EOF
157
255
 
158
 
# test Schema validation errors are thread safe
 
256
print "test Schema validation errors are thread safe\n";
159
257
{
160
258
for(1..MAX_THREADS)
161
259
{
177
275
$xml = join '', <$fh>;
178
276
close $fh;
179
277
ok($xml);
180
 
 
181
278
sub use_dom
182
279
{
183
280
        my $d = shift;
191
288
 
192
289
{
193
290
for(1..MAX_THREADS) {
194
 
        threads->new(sub { use_dom($p->parse_string($xml)) for 1..5; 1; });
 
291
        threads->new(sub { my $dom = do { $p->parse_string($xml); }; use_dom($dom) for 1..5; 1; });
195
292
}
196
293
$_->join for(threads->list);
197
294
ok(1);
216
313
{
217
314
for(1..MAX_THREADS)
218
315
{
219
 
        threads->new(sub { $p->parse_string($xml) for 1..10; 1; });
 
316
        threads->new(sub { $p->parse_string($xml) for (1..5); 1; });     
220
317
}
221
 
$_->join for(threads->list);
 
318
$_->join for threads->list;
 
319
 
222
320
ok(1);
223
321
}
224
322
 
243
341
}
244
342
 
245
343
$p = XML::LibXML->new();
246
 
 
 
344
print "parse a big file using the same parser\n";
247
345
{
248
346
for(1..MAX_THREADS)
249
347
{
250
348
        threads->new(sub {
251
349
open my $fh, "<$bigfile";
252
 
$p->parse_fh($fh);
 
350
my $doc = $p->parse_fh($fh);
253
351
close $fh;
 
352
2;
 
353
});
 
354
}
 
355
my @results = $_->join for(threads->list);
 
356
print@results,"\n";
 
357
ok(1);
 
358
}
 
359
 
 
360
print "create elements\n";
 
361
{
 
362
my @n = map XML::LibXML::Element->new('bar'.$_), 1..1000;
 
363
for(1..MAX_THREADS)
 
364
{
 
365
        threads->new(sub {
 
366
        push @n, map XML::LibXML::Element->new('foo'.$_), 1..1000;
254
367
1;
255
368
});
256
369
}
258
371
ok(1);
259
372
}
260
373
 
 
374
{
 
375
print "docfrag\n";
 
376
my $e = XML::LibXML::Element->new('foo');
 
377
for(1..MAX_THREADS) {
 
378
  threads->new(sub {
 
379
                 if ($_[0]==1) {
 
380
                   my $d = XML::LibXML::Document->new();
 
381
                   $d->setDocumentElement($d->createElement('root'));
 
382
                   $d->documentElement->appendChild($e);
 
383
                 }
 
384
                 1;
 
385
               },$_);
 
386
}
 
387
$_->join for(threads->list);
 
388
ok(1);
 
389
print $e->ownerDocument->toString(),"\n";
 
390
}
 
391
 
 
392
{
 
393
print "docfrag2\n";
 
394
my $e = XML::LibXML::Element->new('foo');
 
395
my $d = XML::LibXML::Document->new();
 
396
$d->setDocumentElement($d->createElement('root'));
 
397
for(1..MAX_THREADS) {
 
398
  threads->new(sub {
 
399
                 if ($_[0]==1) {
 
400
                   $d->documentElement->appendChild($e);
 
401
                 }
 
402
                 1;
 
403
               },$_);
 
404
}
 
405
$_->join for(threads->list);
 
406
ok(1);
 
407
print $e->ownerDocument->toString(),"\n";
 
408
}
 
409
 
 
410
{
 
411
print "docfrag3\n";
 
412
my $e = XML::LibXML::Element->new('foo');
 
413
for(1..MAX_THREADS) {
 
414
  threads->new(sub {
 
415
                 if ($_[0]==1) {
 
416
                   XML::LibXML::Element->new('root')->appendChild($e);
 
417
                 }
 
418
                 1;
 
419
               },$_);
 
420
}
 
421
$_->join for(threads->list);
 
422
ok(1);
 
423
print $e->parentNode->toString(),"\n";
 
424
}
261
425