/test/kilim/test/ex/ExCatch.java

http://github.com/kilim/kilim · Java · 557 lines · 504 code · 39 blank · 14 comment · 37 complexity · bdc78e56c403f64e8963a75dcc985b0c MD5 · raw file

  1. package kilim.test.ex;
  2. import java.io.IOException;
  3. import java.lang.reflect.Method;
  4. import kilim.Fiber;
  5. import kilim.Pausable;
  6. import kilim.Continuation;
  7. import kilim.Task;
  8. public class ExCatch extends ExYieldBase {
  9. public ExCatch(int test) {
  10. testCase = test;
  11. }
  12. /*
  13. public void execute() throws pausable {
  14. if (testCase == 3) {
  15. doPause = false;
  16. tryCatchFinally();
  17. }
  18. }
  19. */
  20. public void execute() throws Pausable {
  21. doPause = false;
  22. test();
  23. doPause = true;
  24. test();
  25. }
  26. private void test() throws Pausable {
  27. switch(testCase) {
  28. case 0: normalCatch(); break;
  29. case 1: pausableCatch(); break;
  30. case 2: nestedPausableCatch(); break;
  31. case 3: tryCatchFinally(); break;
  32. case 4: pausableBeforeCatch(); break;
  33. case 5: tryDefUse(); break;
  34. case 6: whileCatch(); break;
  35. case 7: restoreArgument(fd); break;
  36. case 8: correctException(); break;
  37. case 9: pausableInvokeCatch(); break;
  38. default: throw new IllegalStateException("Unknown test case: " + testCase);
  39. }
  40. }
  41. void normalCatch() throws Pausable {
  42. double d = fd;
  43. String[][] sa = fa;
  44. String s = fs;
  45. try {
  46. pausable(d);
  47. } catch (ExException eye) {
  48. String es = eye.getMessage();
  49. verify(es);
  50. s = es;
  51. }
  52. verify(d);
  53. verify(sa);
  54. verify(s);
  55. }
  56. void pausableCatch() throws Pausable {
  57. double d = fd;
  58. String s = fs;
  59. String[][] sa = fa;
  60. long l = fl;
  61. try {
  62. pausable(d);
  63. // throw new ExException("10");
  64. } catch (ExException eye) {
  65. String es = eye.getMessage();
  66. if (doPause) {
  67. Task.sleep(50);
  68. }
  69. verify(es);
  70. s = es;
  71. }
  72. verify(d);
  73. verify(sa);
  74. verify(s);
  75. verify(l);
  76. }
  77. // Issue#6 on github. A pausable block before the catch block.
  78. void pausableBeforeCatch() throws Pausable {
  79. int foo = 0;
  80. Task.sleep(1);
  81. if (foo != 0) throw new RuntimeException("Expected 0");
  82. foo = 1;
  83. Task.sleep(1);
  84. if (foo != 1) throw new RuntimeException("Expected 1");
  85. foo = 2;
  86. Task.sleep(1);
  87. if (foo != 2) throw new RuntimeException("Expected 2");
  88. try {
  89. foo = 3;
  90. throwEx();
  91. } catch (Throwable t) {
  92. if (foo != 3) throw new RuntimeException("Expected 3");
  93. }
  94. }
  95. private static void throwEx() throws Pausable {
  96. Task.sleep(1);
  97. throw new RuntimeException();
  98. }
  99. public static void restoreArgument(Double resp) throws Pausable {
  100. try {
  101. throwEx();
  102. }
  103. catch (Exception ex) {}
  104. verify(resp);
  105. }
  106. void tryCatchFinally() throws Pausable {
  107. short sh = fsh;
  108. String s = fs;
  109. double d = fd;
  110. long l = fl;
  111. try {
  112. try {
  113. pausable(d);
  114. // throw new ExException("10");
  115. } catch (ExException eye) {
  116. if (doPause) {
  117. Task.sleep(50);
  118. }
  119. throw new Exception("");
  120. } finally {
  121. if (doPause) {
  122. Task.sleep(50);
  123. }
  124. }
  125. } catch (Exception e) {
  126. }
  127. verify(d);
  128. verify(sh);
  129. verify(s);
  130. verify(l);
  131. }
  132. public static void same(int v1,int t1,int v2) {
  133. if (v1==(v2 > 0 ? 0:t1)) {
  134. throw new RuntimeException("false");
  135. }
  136. }
  137. public static void diff(int v1,int t1,int v2) {
  138. if (v1==(v2 > 0 ? t1:0)) {
  139. throw new RuntimeException("false");
  140. }
  141. }
  142. static class Thrower {
  143. int base;
  144. int result;
  145. Thrower(int base) { this.base = base; }
  146. void execute() throws Pausable {
  147. result |= 0x01;
  148. if ((base & 0x04) > 0)
  149. Task.sleep(10);
  150. if ((base & 0x01) > 0)
  151. throw new RuntimeException();
  152. result |= 0x02;
  153. }
  154. void executeFallback() throws Pausable {
  155. result |= 0x04;
  156. if ((base & 0x08) > 0)
  157. Task.sleep(10);
  158. if ((base & 0x02) > 0)
  159. throw new RuntimeException();
  160. result |= 0x08;
  161. }
  162. void check() {
  163. same(result & 0x01,1,1);
  164. diff(result & 0x02,2,base & 0x01);
  165. same(result & 0x04,4,base & 0x01);
  166. same(result & 0x08,8,(base & 0x01) > 0 & (base & 0x02)==0 ? 1:0);
  167. }
  168. }
  169. public static class TryThrow extends ExYieldBase {
  170. int base;
  171. public TryThrow(int base) { this.base = base; }
  172. public void execute() throws Pausable {
  173. Thrower obj;
  174. tryThrow(obj = new Thrower(base));
  175. obj.check();
  176. }
  177. void tryThrow(Thrower obj) throws Pausable {
  178. try {
  179. obj.execute();
  180. }
  181. catch (Exception ex1) {
  182. try {
  183. obj.executeFallback();
  184. }
  185. catch (Exception ex2) {
  186. }
  187. finally {
  188. }
  189. }
  190. finally {
  191. }
  192. }
  193. }
  194. public static class TryThrowVars extends ExYieldBase {
  195. int base;
  196. public TryThrowVars(int base) { this.base = base; }
  197. public void execute() throws Pausable {
  198. Thrower obj;
  199. tryThrow(obj = new Thrower(base));
  200. obj.check();
  201. }
  202. void tryThrow(Thrower obj) throws Pausable {
  203. short sh = 0;
  204. String s = null;
  205. double d = 0;
  206. long l = 0;
  207. byte b = 0;
  208. try {
  209. obj.execute();
  210. sh = fsh;
  211. }
  212. catch (Exception ex1) {
  213. try {
  214. obj.executeFallback();
  215. b = fb;
  216. }
  217. catch (Exception ex2) {
  218. d = fd;
  219. }
  220. finally {
  221. l = fl;
  222. }
  223. }
  224. finally {
  225. s = fs;
  226. }
  227. boolean x = (base & 0x01)==0;
  228. boolean y = (base & 0x02)==0;
  229. verify(s);
  230. verify(sh, x ? fsh:0);
  231. verify(b, !x & y ? fb:0);
  232. verify(d, x|y ? 0:fd);
  233. verify(l, x ? 0:fd);
  234. }
  235. }
  236. private class MyFile {
  237. private int isFile = 0;
  238. boolean isDirectory() throws Pausable, IOException {
  239. if (doPause)
  240. Task.sleep(50);
  241. isFile++;
  242. if (isFile % 15==0) throw new IOException();
  243. int val = isFile % 6;
  244. return val==0 | val==2 | val==3;
  245. }
  246. }
  247. private MyFile baseDirectory = new MyFile();
  248. private void readRequest(String obj) throws Pausable, IOException {
  249. if (doPause)
  250. Task.sleep(50);
  251. verify(obj);
  252. }
  253. public void whileCatch() throws Pausable {
  254. int vi = fi;
  255. byte vb = fb;
  256. try {
  257. double d = fd;
  258. String s = fs;
  259. String[][] sa = fa;
  260. long l = fl;
  261. String req = fs;
  262. while (true) {
  263. verify(d);
  264. readRequest(req);
  265. verify(s);
  266. MyFile file = baseDirectory;
  267. if (file.isDirectory()) {
  268. verify(sa);
  269. file.isDirectory();
  270. verify(l);
  271. }
  272. }
  273. } catch (Exception ioe) {
  274. verify(vi);
  275. }
  276. verify(vb);
  277. }
  278. void nestedPausableCatch() throws Pausable {
  279. double d = fd;
  280. String s = fs;
  281. String[][] sa = fa;
  282. long l = fl;
  283. try {
  284. throw new ExException("10");
  285. } catch (final ExException eye) {
  286. String es = null;;
  287. try {
  288. throw new ExException("10");
  289. } catch (Exception e) {
  290. es = eye.getMessage();
  291. verify(es);
  292. es = e.getMessage();
  293. if (doPause) {
  294. Task.sleep(50);
  295. }
  296. }
  297. verify(es);
  298. s = es;
  299. }
  300. verify(d);
  301. verify(sa);
  302. verify(s);
  303. verify(l);
  304. }
  305. // use after a define that can be skipped by an exception
  306. void tryDefUse() throws Pausable {
  307. {
  308. double d = fd, d2 = d+1;
  309. try {
  310. pausable(d);
  311. d = d2;
  312. }
  313. catch (Exception e) {}
  314. verify(d);
  315. }
  316. {
  317. double d = fd, d2 = d+1;
  318. try {
  319. pausable(d);
  320. }
  321. catch (Exception e) { d2 = d; }
  322. verify(d2);
  323. }
  324. {
  325. double d = fd, d2 = d+1;
  326. try {
  327. pausable(d);
  328. }
  329. catch (Exception e) { d2 = d; }
  330. verify(d2);
  331. }
  332. try { doFinally(); }
  333. catch (NullPointerException ex) {}
  334. try { doFinally2(); }
  335. catch (NullPointerException ex) {}
  336. try { doFinally3(); }
  337. catch (NullPointerException ex) {}
  338. doFinally4();
  339. double d = fd, d2 = d+1;
  340. if (doPause)
  341. Task.sleep(50);
  342. try {
  343. ((Object) null).toString();
  344. d = d2;
  345. }
  346. catch (Throwable ex) {}
  347. verify(d);
  348. }
  349. void doFinally() throws Pausable {
  350. double d = fd, d2 = d+1;
  351. if (!doPause)
  352. Task.sleep(50);
  353. try {
  354. if (doPause)
  355. Task.sleep(50);
  356. ((Object) null).toString();
  357. d2 = d;
  358. }
  359. finally { verify(d); verify(d2-1); }
  360. }
  361. void doFinally2() throws Pausable {
  362. {
  363. double d = fd, d2 = d+1;
  364. try {
  365. if (doPause)
  366. Task.sleep(50);
  367. ((Object) null).toString();
  368. d = d2;
  369. }
  370. catch (Exception e) {
  371. ((Object) null).toString();
  372. d = d2;
  373. }
  374. finally { verify(d); }
  375. }
  376. }
  377. void doFinally3() throws Pausable {
  378. {
  379. double d = fd, d2 = d+1;
  380. if (doPause)
  381. Task.sleep(50);
  382. try {
  383. ((Object) null).toString();
  384. d = d2;
  385. }
  386. catch (Exception e) {
  387. ((Object) null).toString();
  388. d = d2;
  389. }
  390. finally { verify(d); }
  391. }
  392. }
  393. void doFinally4() throws Pausable {
  394. double d = fd, d2 = d+1;
  395. try {
  396. try {
  397. if (doPause)
  398. Task.sleep(50);
  399. ((Object) null).toString();
  400. d = d2;
  401. }
  402. catch (Exception e) {
  403. ((Object) null).toString();
  404. d = d2;
  405. }
  406. finally { verify(d); }
  407. }
  408. catch (NullPointerException ex) {}
  409. try {
  410. if (doPause)
  411. Task.sleep(50);
  412. try {
  413. ((Object) null).toString();
  414. d = d2;
  415. }
  416. catch (Exception e) {
  417. ((Object) null).toString();
  418. d = d2;
  419. }
  420. finally { verify(d); }
  421. }
  422. catch (NullPointerException ex) {}
  423. if (doPause)
  424. Task.sleep(50);
  425. try {
  426. try {
  427. ((Object) null).toString();
  428. d = d2;
  429. }
  430. catch (Exception e) {
  431. ((Object) null).toString();
  432. d = d2;
  433. }
  434. finally {}
  435. }
  436. catch (NullPointerException ex) {}
  437. verify(d);
  438. }
  439. private void pausable(double d) throws ExException, Pausable {
  440. if (doPause) {
  441. Task.sleep(50);
  442. }
  443. verify(d);
  444. throw new ExException("10");
  445. }
  446. public static class Pure extends Continuation {
  447. int [] count = new int[11];
  448. public void execute() throws Pausable {
  449. double d = fd;
  450. String[][] sa = fa;
  451. String s = fs;
  452. try {
  453. pausableThrow(0);
  454. } catch (Exception kex) {
  455. String es = kex.getMessage();
  456. verify(es);
  457. s = es;
  458. }
  459. verify(d);
  460. verify(sa);
  461. verify(s);
  462. for (int cc : count)
  463. verify(cc,14);
  464. }
  465. public void pausableThrow(int num) throws Pausable, Exception {
  466. count[num]++;
  467. Fiber.yield();
  468. count[num] += 13;
  469. if (num < 10) pausableThrow(num+1);
  470. else throw new Exception("10");
  471. count[num] += 107;
  472. }
  473. }
  474. // crude check for issue 55 - verify exceptions don't get reordered
  475. public void correctException() throws Pausable {
  476. double val = fd;
  477. try {
  478. Task.sleep(1);
  479. throw new RuntimeException();
  480. }
  481. catch (RuntimeException ex) {}
  482. catch (Exception ex) {
  483. throw new RuntimeException("incorrect exception");
  484. }
  485. verify(val);
  486. }
  487. // merged from https://github.com/hyleeon/kilim/tree/fix-invoke
  488. void pausableInvokeCatch() throws Pausable {
  489. String[][] sa = fa;
  490. long l = fl;
  491. try {
  492. Method mthd = ExCatch.class.getDeclaredMethod("pausableInvokeCatch0",new Class[0]);
  493. Task.invoke(mthd,this);
  494. } catch (Exception eye) {
  495. eye.printStackTrace();
  496. }
  497. verify(sa);
  498. verify(l);
  499. }
  500. public void pausableInvokeCatch0() throws Pausable {
  501. double d = fd;
  502. String s = fs;
  503. try {
  504. pausable(d);
  505. }
  506. catch (Exception eye) {
  507. if (eye instanceof java.lang.reflect.InvocationTargetException)
  508. eye = (Exception) eye.getCause();
  509. String es = eye.getMessage();
  510. if (doPause)
  511. Task.sleep(50);
  512. verify(es);
  513. s = es;
  514. }
  515. verify(d);
  516. verify(s);
  517. }
  518. }