|
| 1 | +# This file is a part of Julia. License is MIT: http://julialang.org/license |
| 2 | + |
| 3 | + |
| 4 | +@test map(typeof, map(@catch(i->[1,2,3][i]), 1:6)) == |
| 5 | + [Int, Int, Int, BoundsError, BoundsError, BoundsError] |
| 6 | + |
| 7 | +@test typeof(@catch(open)("/no/file/with/this/name")) == SystemError |
| 8 | + |
| 9 | + |
| 10 | +let |
| 11 | + |
| 12 | + function f(c, n) |
| 13 | + c[1] += 1 |
| 14 | + if c[1] <= n |
| 15 | + error("foo") |
| 16 | + end |
| 17 | + return 7 |
| 18 | + end |
| 19 | + |
| 20 | + # Success on first attempt... |
| 21 | + c = [0] |
| 22 | + @test retry(f)(c,0) == 7 |
| 23 | + @test c[1] == 1 |
| 24 | + |
| 25 | + # Success on second attempt... |
| 26 | + c = [0] |
| 27 | + @test retry(f)(c,1) == 7 |
| 28 | + @test c[1] == 2 |
| 29 | + |
| 30 | + # Success on third attempt... |
| 31 | + c = [0] |
| 32 | + @test retry(f)(c,2) == 7 |
| 33 | + @test c[1] == 3 |
| 34 | + |
| 35 | + # 3 failed attempts, so exception is raised... |
| 36 | + c = [0] |
| 37 | + ex = @catch(retry(f))(c,3) |
| 38 | + @test ex.msg == "foo" |
| 39 | + @test c[1] == 3 |
| 40 | + |
| 41 | + c = [0] |
| 42 | + ex = @catch(retry(f, ErrorException))(c,3) |
| 43 | + @test typeof(ex) == ErrorException |
| 44 | + @test ex.msg == "foo" |
| 45 | + @test c[1] == 3 |
| 46 | + |
| 47 | + c = [0] |
| 48 | + ex = @catch(retry(f, e->e.msg == "foo"))(c,3) |
| 49 | + @test typeof(ex) == ErrorException |
| 50 | + @test ex.msg == "foo" |
| 51 | + @test c[1] == 3 |
| 52 | + |
| 53 | + # No retry if condition does not match... |
| 54 | + c = [0] |
| 55 | + ex = @catch(retry(f, e->e.msg == "bar"))(c,3) |
| 56 | + @test typeof(ex) == ErrorException |
| 57 | + @test ex.msg == "foo" |
| 58 | + @test c[1] == 1 |
| 59 | + |
| 60 | + c = [0] |
| 61 | + ex = @catch(retry(f, e->e.http_status_code == "503"))(c,3) |
| 62 | + @test typeof(ex) == ErrorException |
| 63 | + @test ex.msg == "foo" |
| 64 | + @test c[1] == 1 |
| 65 | + |
| 66 | + c = [0] |
| 67 | + ex = @catch(retry(f, SystemError))(c,3) |
| 68 | + @test typeof(ex) == ErrorException |
| 69 | + @test ex.msg == "foo" |
| 70 | + @test c[1] == 1 |
| 71 | + |
| 72 | +end |
0 commit comments