チェンジセット 507

差分発生行の前後
無視リスト:
コミット日時:
2008/05/25 16:18:59 (6 ヶ月前)
コミッタ:
yossy
ログメッセージ:

Thread(soumen): テスト追加

ファイル:

凡例:

変更無し
追加
削除
更新
コピー
移動
  • as3/Thread/branches/soumen/src/org/libspark/thread/ThreadState.as

    r464 r507  
    1212                 
    1313                /** 
    14                  * 実行可能なスレッドの状態です 
     14                 * 実行可能なスレッドの状態(実行フェーズ)です 
    1515                 */ 
    1616                public static const RUNNABLE:uint      = 1; 
     
    2727                 
    2828                /** 
    29                  * 終了処理中のスレッドの状態です 
     29                 * 終了処理中のスレッドの状態(終了フェーズ)です 
    3030                 */ 
    3131                public static const TERMINATING:uint   = 4; 
  • as3/Thread/branches/soumen/tests/org/libspark/thread/TesterThread.as

    r464 r507  
    11package org.libspark.thread 
    22{ 
     3        import org.libspark.thread.Thread; 
     4 
    35        import flash.events.Event; 
    4         import org.libspark.as3unit.assert.*; 
    5         import org.libspark.as3unit.before; 
    6         import org.libspark.as3unit.test; 
     6        import flash.events.EventDispatcher; 
    77         
    8         use namespace before; 
    9         use namespace test; 
    10          
    11         public class ThreadExecutionTest 
     8        public class TesterThread extends Thread 
    129        { 
    13                 before function initialize():void 
     10                public function TesterThread(t:Thread) 
    1411                { 
    15                         Thread.initialize(new EnterFrameThreadExectuor()); 
     12                        _t = t; 
     13                        _e = new EventDispatcher(); 
    1614                } 
    1715                 
    18                 test function tester():void 
     16                private var _t:Thread; 
     17                private var _e:EventDispatcher; 
     18                 
     19                public function addEventListener(type:String, func:Function):void 
    1920                { 
    20                         var t:TesterThread = new TesterThread(null); 
    21                         t.addEventListener(Event.COMPLETE, async(function(e:Event):void 
    22                         { 
    23                                 assertTrue(true); 
    24                         }, 1000)); 
    25                         t.start(); 
     21                        _e.addEventListener(type, func); 
    2622                } 
    2723                 
    28                 test function start():void 
     24                protected override function run():void  
    2925                { 
    30                          
     26                        if (_t != null) { 
     27                                _t.start(); 
     28                                _t.join(); 
     29                        } 
     30                } 
     31                 
     32                protected override function finalize():void  
     33                { 
     34                        _e.dispatchEvent(new Event(Event.COMPLETE)); 
    3135                } 
    3236        } 
    3337} 
    34  
    35 import org.libspark.thread.Thread; 
    36  
    37 import flash.events.Event; 
    38 import flash.events.EventDispatcher; 
    39  
    40 class TesterThread extends Thread 
    41 { 
    42         public function TesterThread(t:Thread) 
    43         { 
    44                 _t = t; 
    45                 _e = new EventDispatcher(); 
    46         } 
    47          
    48         private var _t:Thread; 
    49         private var _e:EventDispatcher; 
    50          
    51         public function addEventListener(type:String, func:Function):void 
    52         { 
    53                 _e.addEventListener(type, func); 
    54         } 
    55          
    56         protected override function run():void  
    57         { 
    58                 if (_t != null) { 
    59                         _t.start(); 
    60                         _t.join(); 
    61                 } 
    62         } 
    63          
    64         protected override function finalize():void  
    65         { 
    66                 _e.dispatchEvent(new Event(Event.COMPLETE)); 
    67         } 
    68 } 
  • as3/Thread/branches/soumen/tests/org/libspark/thread/TesterThreadTest.as

    r464 r507  
    99        use namespace test; 
    1010         
    11         public class ThreadExecutionTest 
     11        public class TesterThreadTest 
    1212        { 
    1313                before function initialize():void 
     
    2525                        t.start(); 
    2626                } 
    27                  
    28                 test function start():void 
    29                 { 
    30                          
    31                 } 
    3227        } 
    3328} 
    34  
    35 import org.libspark.thread.Thread; 
    36  
    37 import flash.events.Event; 
    38 import flash.events.EventDispatcher; 
    39  
    40 class TesterThread extends Thread 
    41 { 
    42         public function TesterThread(t:Thread) 
    43         { 
    44                 _t = t; 
    45                 _e = new EventDispatcher(); 
    46         } 
    47          
    48         private var _t:Thread; 
    49         private var _e:EventDispatcher; 
    50          
    51         public function addEventListener(type:String, func:Function):void 
    52         { 
    53                 _e.addEventListener(type, func); 
    54         } 
    55          
    56         protected override function run():void  
    57         { 
    58                 if (_t != null) { 
    59                         _t.start(); 
    60                         _t.join(); 
    61                 } 
    62         } 
    63          
    64         protected override function finalize():void  
    65         { 
    66                 _e.dispatchEvent(new Event(Event.COMPLETE)); 
    67         } 
    68 } 
  • as3/Thread/branches/soumen/tests/org/libspark/thread/ThreadAllTests.as

    r464 r507  
    77                public static const RunWith:Class = Suite; 
    88                public static const SuiteClasses:Array = [ 
     9                        TesterThreadTest, 
    910                        ThreadExecutionTest 
    1011                ]; 
  • as3/Thread/branches/soumen/tests/org/libspark/thread/ThreadExecutionTest.as

    r464 r507  
    1111        public class ThreadExecutionTest 
    1212        { 
     13                /** 
     14                 * テストに相互作用が出ないようにテスト毎にスレッドライブラリを初期化。 
     15                 * 通常であれば、initializeの呼び出しは一度きり。 
     16                 */ 
    1317                before function initialize():void 
    1418                { 
     
    1620                } 
    1721                 
    18                 test function tester():void 
    19                 { 
    20                         var t:TesterThread = new TesterThread(null); 
    21                         t.addEventListener(Event.COMPLETE, async(function(e:Event):void 
    22                         { 
    23                                 assertTrue(true); 
    24                         }, 1000)); 
    25                         t.start(); 
    26                 } 
    27                  
     22                /** 
     23                 * start したら run が実行されるか 
     24                 */ 
    2825                test function start():void 
    2926                { 
    30                          
     27                        Static.run = false; 
     28                         
     29                        var t:TesterThread = new TesterThread(new StartTestThread()); 
     30                         
     31                        assertFalse(Static.run); 
     32                         
     33                        t.addEventListener(Event.COMPLETE, async(function(e:Event):void 
     34                        { 
     35                                assertTrue(Static.run); 
     36                        }, 1000)); 
     37                         
     38                        t.start(); 
     39                } 
     40                 
     41                /** 
     42                 * currentThread にきちんと現在実行中のスレッドが設定されているか。 
     43                 * 実行中のスレッドが無い(擬似スレッドなのでこういうことが起こりうる)場合は null が設定される。 
     44                 */ 
     45                test function currentThread():void 
     46                { 
     47                        var c:CurrentThreadTestThread = new CurrentThreadTestThread(); 
     48                        var t:TesterThread = new TesterThread(c); 
     49                         
     50                        t.addEventListener(Event.COMPLETE, async(function(e:Event):void 
     51                        { 
     52                                assertSame(c, c.current); 
     53                                assertNull(Thread.currentThread); 
     54                        }, 1000)); 
     55                         
     56                        assertNull(Thread.currentThread); 
     57                         
     58                        t.start(); 
     59                } 
     60                 
     61                /** 
     62                 * next による実行関数の切り替えが行えているか。 
     63                 * next を呼び出さない場合、実行フェーズ → 終了フェーズ → 終了 という順で遷移する。 
     64                 * next は finalize の中(終了フェーズ, state == ThreadState.TERMINATING)でも有効。 
     65                 */ 
     66                test function next():void 
     67                { 
     68                        Static.log = ''; 
     69                         
     70                        var t:TesterThread = new TesterThread(new NextTestThread()); 
     71                         
     72                        t.addEventListener(Event.COMPLETE, async(function(e:Event):void 
     73                        { 
     74                                assertEquals('run run2 run3 finalize finalize2 ', Static.log); 
     75                        }, 1000)); 
     76                         
     77                        t.start(); 
     78                } 
     79                 
     80                /** 
     81                 * state が NEW → RUNNABLE → TERMINATING → TERMINATED という順で切り替わっているか。 
     82                 */ 
     83                test function state():void 
     84                { 
     85                        var s:StateTestThread = new StateTestThread(); 
     86                        var t:TesterThread = new TesterThread(s); 
     87                         
     88                        t.addEventListener(Event.COMPLETE, async(function(e:Event):void 
     89                        { 
     90                                assertEquals(ThreadState.RUNNABLE, s.state1); 
     91                                assertEquals(ThreadState.RUNNABLE, s.state2); 
     92                                assertEquals(ThreadState.TERMINATING, s.state3); 
     93                                assertEquals(ThreadState.TERMINATING, s.state4); 
     94                                assertEquals(ThreadState.TERMINATED, s.state); 
     95                        }, 1000)); 
     96                         
     97                        assertEquals(ThreadState.NEW, s.state); 
     98                         
     99                        t.start(); 
     100                } 
     101                 
     102                /** 
     103                 * 子スレッドが正しく呼び出されているか。 
     104                 * あるスレッドが別のスレッドの start を呼び出した際、start を呼び出したほうのスレッドを親スレッド、start が呼び出されたほうのスレッドを子スレッドと呼ぶ。 
     105                 * 子スレッドは、その親スレッドよりも前に、start された順で実行されることが保証される。 
     106                 */ 
     107                test function childThread():void 
     108                { 
     109                        Static.log = ''; 
     110                         
     111                        var t:TesterThread = new TesterThread(new ChildThreadTestParentThread()); 
     112                         
     113                        t.addEventListener(Event.COMPLETE, async(function(e:Event):void 
     114                        { 
     115                                assertEquals('p.run c1.run c2.run p.run2 c1.run2 c2.run2 p.run3 c1.finalize c2.finalize p.finalize ', Static.log); 
     116                        }, 1000)); 
     117                         
     118                        t.start(); 
     119                } 
     120                 
     121                /** 
     122                 * 孤児スレッドが正しく呼び出されているか。 
     123                 * 子スレッドの終了より先に親スレッドが終了した場合、子スレッドは孤児スレッドとなる。 
     124                 * 孤児スレッドは親スレッドから切り離され、トップレベルに移されて実行が継続される。 
     125                 */ 
     126                test function orphanThread():void 
     127                { 
     128                        Static.log = ''; 
     129                         
     130                        var t:TesterThread = new TesterThread(new OrphanTestThread()); 
     131                         
     132                        t.addEventListener(Event.COMPLETE, async(function(e:Event):void 
     133                        { 
     134                                assertEquals('p.run c.run p.finalize c.run2 c.finalize ', Static.log); 
     135                        }, 1000)); 
     136                         
     137                        t.start(); 
    31138                } 
    32139        } 
     
    34141 
    35142import org.libspark.thread.Thread; 
    36  
    37 import flash.events.Event; 
    38 import flash.events.EventDispatcher; 
    39  
    40 class TesterThread extends Thread 
    41 
    42         public function TesterThread(t:Thread) 
    43         { 
    44                 _t = t; 
    45                 _e = new EventDispatcher(); 
    46         } 
    47          
    48         private var _t:Thread; 
    49         private var _e:EventDispatcher; 
    50          
    51         public function addEventListener(type:String, func:Function):void 
    52         { 
    53                 _e.addEventListener(type, func); 
    54         } 
    55          
    56         protected override function run():void  
    57         { 
    58                 if (_t != null) { 
    59                         _t.start(); 
    60                         _t.join(); 
    61                 } 
    62         } 
    63          
    64         protected override function finalize():void  
    65         { 
    66                 _e.dispatchEvent(new Event(Event.COMPLETE)); 
    67         } 
    68 
     143import org.libspark.thread.ThreadState; 
     144 
     145class Static 
     146
     147        public static var run:Boolean; 
     148        public static var log:String; 
     149
     150 
     151class StartTestThread extends Thread 
     152
     153        override protected function run():void 
     154        { 
     155                Static.run = true; 
     156        } 
     157
     158 
     159class CurrentThreadTestThread extends Thread 
     160
     161        public var current:Thread = null; 
     162         
     163        override protected function run():void 
     164        { 
     165                current = currentThread; 
     166        } 
     167
     168 
     169class NextTestThread extends Thread 
     170
     171        override protected function run():void 
     172        { 
     173                Static.log += 'run '; 
     174                next(run2); 
     175        } 
     176         
     177        private function run2():void 
     178        { 
     179                Static.log += 'run2 '; 
     180                next(run3); 
     181        } 
     182         
     183        private function run3():void 
     184        { 
     185                Static.log += 'run3 '; 
     186        } 
     187         
     188        override protected function finalize():void 
     189        { 
     190                Static.log += 'finalize '; 
     191                next(finalize2); 
     192        } 
     193         
     194        private function finalize2():void 
     195        { 
     196                Static.log += 'finalize2 '; 
     197        } 
     198
     199 
     200class StateTestThread extends Thread 
     201
     202        public var state1:uint = ThreadState.NEW; 
     203        public var state2:uint = ThreadState.NEW; 
     204        public var state3:uint = ThreadState.NEW; 
     205        public var state4:uint = ThreadState.NEW; 
     206         
     207        override protected function run():void 
     208        { 
     209                state1 = state; 
     210                next(run2); 
     211        } 
     212         
     213        private function run2():void 
     214        { 
     215                state2 = state; 
     216        } 
     217         
     218        override protected function finalize():void 
     219        { 
     220                state3 = state; 
     221                next(finalize2); 
     222        } 
     223         
     224        private function finalize2():void 
     225        { 
     226                state4 = state; 
     227        } 
     228
     229 
     230class ChildThreadTestParentThread extends Thread 
     231
     232        override protected function run():void 
     233        { 
     234                Static.log += 'p.run '; 
     235                 
     236                new ChildThreadTestChildThread('c1').start(); 
     237                new ChildThreadTestChildThread('c2').start(); 
     238                 
     239                next(run2); 
     240        } 
     241         
     242        private function run2():void 
     243        { 
     244                Static.log += 'p.run2 '; 
     245                next(run3); 
     246        } 
     247         
     248        private function run3():void 
     249        { 
     250                Static.log += 'p.run3 '; 
     251        } 
     252         
     253        override protected function finalize():void 
     254        { 
     255                Static.log += 'p.finalize '; 
     256        } 
     257
     258 
     259class ChildThreadTestChildThread extends Thread 
     260
     261        public function ChildThreadTestChildThread(name:String) 
     262        { 
     263                _name = name; 
     264        } 
     265         
     266        private var _name:String; 
     267         
     268        override protected function run():void 
     269        { 
     270                Static.log += _name + '.run '; 
     271                next(run2); 
     272        } 
     273         
     274        private function run2():void 
     275        { 
     276                Static.log += _name + '.run2 '; 
     277        } 
     278         
     279        override protected function finalize():void 
     280        { 
     281                Static.log += _name + '.finalzie '; 
     282        } 
     283
     284 
     285class OrphanTestThread extends Thread 
     286
     287        private var _c:OrphanTestChildThread; 
     288         
     289        override protected function run():void 
     290        { 
     291                var t:OrphanTestParentThread = new OrphanTestParentThread(); 
     292                _c = t.child; 
     293                t.start(); 
     294                next(waitChild); 
     295        } 
     296         
     297        private function waitChild():void 
     298        { 
     299                if (!_c.isFinished) { 
     300                        next(waitChild); 
     301                } 
     302        } 
     303
     304 
     305class OrphanTestParentThread extends Thread 
     306
     307        public var child:OrphanTestChildThread = new OrphanTestChildThread(); 
     308         
     309        override protected function run():void 
     310        { 
     311                Static.log += 'p.run '; 
     312                child.start(); 
     313        } 
     314         
     315        override protected function finalize():void 
     316        { 
     317                Static.log += 'p.finalize '; 
     318        } 
     319
     320 
     321class OrphanTestChildThread extends Thread 
     322
     323        public var isFinished:Boolean = false; 
     324         
     325        override protected function run():void 
     326        { 
     327                Static.log += 'c.run '; 
     328                next(run2); 
     329        } 
     330         
     331        private function run2():void 
     332        { 
     333                Static.log += 'c.run2 '; 
     334                next(run2); 
     335        } 
     336         
     337        override protected function finalize():void 
     338        { 
     339                Static.log += 'c.finalize '; 
     340                isFinished = true; 
     341        } 
     342