Improve tests for threads
[gitmo/Mouse.git] / t / 001_mouse / 060-threads.t
CommitLineData
f259aaad 1#!perl
2use strict;
3use warnings;
4use constant HAS_THREADS => eval{ require threads };
5
eea1593f 6use Test::More HAS_THREADS ? (tests => 32) : (skip_all => "This is a test for threads ($@)");
f259aaad 7
8{
9 package MyClass;
10 use Mouse;
11
12 has foo => (
13 is => 'rw',
aff73a9e 14 isa => 'Foo',
15 );
16
17 package Foo;
18 use Mouse;
eea1593f 19 extends qw(MyClass);
aff73a9e 20
21 has value => (
eea1593f 22 is => 'rw',
23 isa => 'Int',
f259aaad 24 );
25}
26
eea1593f 27sub mysleep {
28 select undef, undef, undef, $_[0];
29}\r
30
31
aff73a9e 32my $o = MyClass->new(foo => Foo->new(value => 42));
f259aaad 33
eea1593f 34my @threads;
35
36foreach (1 .. 5){
37 push @threads, threads->create(sub{
38 my $tid = threads->tid;
39
40 ok($tid, "start (tid: $tid)");
41
42 my $x = MyClass->new(foo => Foo->new(value => 1));
43 is $x->foo->value, 1;
f259aaad 44
eea1593f 45 mysleep(0.01);
f259aaad 46
eea1593f 47 $x->foo(Foo->new(value => 2));
f259aaad 48
eea1593f 49 is $x->foo->value, 2;
f259aaad 50
eea1593f 51 MyClass->meta->make_immutable();
52 Foo->meta->make_immutable();
53
54 my $y = MyClass->new(foo => Foo->new(value => 10));
55 is $y->foo->value, 10;
56
57 $y->foo(Foo->new(value => 20));
58
59 is $y->foo->value, 20;
60
61 is $x->foo->value, 2, "end (tid: $tid)";
62 });
63}
f259aaad 64
eea1593f 65$_->join for @threads;
f259aaad 66
aff73a9e 67is $o->foo->value, 42;
f259aaad 68ok !$o->meta->is_immutable;
69