PageRenderTime 58ms CodeModel.GetById 4ms app.highlight 49ms RepoModel.GetById 1ms app.codeStats 1ms

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