KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > test > TestAdoptedThreading


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Alternatively, the contents of this file may be used under the terms of
15  * either of the GNU General Public License Version 2 or later (the "GPL"),
16  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
17  * in which case the provisions of the GPL or the LGPL are applicable instead
18  * of those above. If you wish to allow use of your version of this file only
19  * under the terms of either the GPL or the LGPL, and not to allow others to
20  * use your version of this file under the terms of the CPL, indicate your
21  * decision by deleting the provisions above and replace them with the notice
22  * and other provisions required by the GPL or the LGPL. If you do not delete
23  * the provisions above, a recipient may use your version of this file under
24  * the terms of any one of the CPL, the GPL or the LGPL.
25  ***** END LICENSE BLOCK *****/

26 package org.jruby.test;
27
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 import org.apache.bsf.BSFManager;
32
33 import junit.framework.Test;
34 import junit.framework.TestCase;
35 import junit.framework.TestSuite;
36
37 /**
38  * A simple "adopted thread" concurrency test case.
39  */

40 public class TestAdoptedThreading extends TestCase {
41     private static Logger JavaDoc LOGGER = Logger.getLogger(TestAdoptedThreading.class
42             .getName());
43
44     // Uncomment the "puts" lines if you want to see more detail
45
private static final String JavaDoc SCRIPT = "require 'java'\n"
46             + "include_class 'org.jruby.test.ITest'\n"
47             + "class TestImpl < ITest\n" + " def exec(_value)\n"
48             + " #puts \"start executing!\"\n"
49             + " 100.times do | item |\n"
50             + " value = \"#{item}\"\n" + " end\n"
51             + " #puts \"end executing1!\"\n" + " exec2(_value)\n"
52             + " end\n" + " def exec2(_value)\n"
53             + " #puts \"start executing2!\"\n"
54             + " 500.times do | item |\n"
55             + " value = \"#{item}\"\n" + " end\n"
56             + " #puts \"end executing2!\"\n"
57             + " \"VALUE: #{_value}\"\n" + " end\n" + "end";
58
59     public TestAdoptedThreading(String JavaDoc _name) {
60         super(_name);
61     }
62
63     public static Test suite() {
64         TestSuite suite;
65         suite = new TestSuite(TestAdoptedThreading.class);
66
67         return suite;
68     }
69
70     private BSFManager manager_;
71
72     protected void setUp() throws Exception JavaDoc {
73         LOGGER.log(Level.FINEST, SCRIPT);
74         BSFManager.registerScriptingEngine("ruby",
75                 "org.jruby.javasupport.bsf.JRubyEngine",
76                 new String JavaDoc[] { "rb" });
77         manager_ = new BSFManager();
78         manager_.exec("ruby", "(java)", 1, 1, SCRIPT);
79     }
80     
81     private static final int RUNNER_COUNT = 10;
82     private static final int RUNNER_LOOP_COUNT = 10;
83
84     public void testThreading() {
85         Runner[] runners = new Runner[RUNNER_COUNT];
86         
87         for (int i = 0; i < RUNNER_COUNT; i++) runners[i] = new Runner("R" + i, RUNNER_LOOP_COUNT);
88         for (int i = 0; i < RUNNER_COUNT; i++) runners[i].start();
89
90         try {
91             for (int i = 0; i < RUNNER_COUNT; i++) runners[i].join();
92         } catch (InterruptedException JavaDoc ie) {
93             // XXX: do something?
94
}
95         
96         for (int i = 0; i < RUNNER_COUNT; i++) {
97             if (runners[i].isFailed()) {
98                 if (runners[i].getFailureException() != null) {
99                     throw runners[i].getFailureException();
100                 }
101             }
102         }
103     }
104
105     class Runner extends Thread JavaDoc {
106         private int count_;
107         private boolean failed;
108         private RuntimeException JavaDoc failureException;
109
110         public Runner(String JavaDoc _name, int _count) {
111             count_ = _count;
112         }
113         
114         public boolean isFailed() {
115             return failed;
116         }
117         
118         public RuntimeException JavaDoc getFailureException() {
119             return failureException;
120         }
121
122         public void run() {
123             for (int i = 0; i < count_; i++) {
124                 ITest test = getTest();
125                 if (test != null) {
126                     try {
127                         Object JavaDoc result = test.exec("foo");
128                         if (!result.toString().equals("VALUE: 5000")) {
129                             failed = true;
130                         }
131                     } catch (RuntimeException JavaDoc re) {
132                         failed = true;
133                         failureException = re;
134                         break;
135                     }
136                 }
137             }
138         }
139
140         private ITest getTest() {
141             ITest result = null;
142             synchronized (manager_) {
143                 try {
144                     result = (ITest) manager_.eval("ruby", "(java)", 1, 1,
145                             "TestImpl.new");
146                 } catch (Exception JavaDoc ex) {
147                     ex.printStackTrace();
148                 }
149             }
150             return result;
151         }
152     }
153
154     public static void main(String JavaDoc[] _args) {
155         junit.textui.TestRunner.run(suite());
156     }
157
158 }
159
Popular Tags