1 26 package org.jruby.test; 27 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 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 40 public class TestAdoptedThreading extends TestCase { 41 private static Logger LOGGER = Logger.getLogger(TestAdoptedThreading.class 42 .getName()); 43 44 private static final String 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 _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 { 73 LOGGER.log(Level.FINEST, SCRIPT); 74 BSFManager.registerScriptingEngine("ruby", 75 "org.jruby.javasupport.bsf.JRubyEngine", 76 new String [] { "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 ie) { 93 } 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 { 106 private int count_; 107 private boolean failed; 108 private RuntimeException failureException; 109 110 public Runner(String _name, int _count) { 111 count_ = _count; 112 } 113 114 public boolean isFailed() { 115 return failed; 116 } 117 118 public RuntimeException 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 result = test.exec("foo"); 128 if (!result.toString().equals("VALUE: 5000")) { 129 failed = true; 130 } 131 } catch (RuntimeException 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 ex) { 147 ex.printStackTrace(); 148 } 149 } 150 return result; 151 } 152 } 153 154 public static void main(String [] _args) { 155 junit.textui.TestRunner.run(suite()); 156 } 157 158 } 159 | Popular Tags |