Skip to content

Commit 6c85162

Browse files
committed
gotta get down on friday
0 parents  commit 6c85162

File tree

8 files changed

+176
-0
lines changed

8 files changed

+176
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### 1.0.0 (2014-09-19)
2+
3+
* Initial release

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2014 Igor Wiedler
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# retry
2+
3+
A tiny library for retrying failing operations.
4+
5+
Since the network is reliable, things should always work. Am I right? For those cases when they don't, there is *retry*.
6+
7+
```php
8+
<?
9+
use function igorw\retry;
10+
11+
// retry an operation up to 5 times
12+
$user = retry(5, function () use ($id) {
13+
return User::find($id);
14+
});
15+
16+
// here is why you want to start using HHVM
17+
$user = retry(5, () ==> User::find($id));
18+
19+
// this is probably a bad idea
20+
$user = retry(INF, () ==> {
21+
throw new RuntimeException('never gonna give you up');
22+
});
23+
?>
24+
```
25+
26+
I know. You're welcome.

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "igorw/retry",
3+
"description": "A tiny library for retrying failing operations.",
4+
"keywords": ["failure"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Igor Wiedler",
9+
"email": "igor@wiedler.ch"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.4"
14+
},
15+
"autoload": {
16+
"files": ["src/retry.php"]
17+
},
18+
"extra": {
19+
"branch-alias": { "dev-master": "1.0-dev" }
20+
}
21+
}

phpunit.xml.dist

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
colors="true"
5+
bootstrap="vendor/autoload.php"
6+
>
7+
<testsuites>
8+
<testsuite name="unit">
9+
<directory>./tests/</directory>
10+
</testsuite>
11+
</testsuites>
12+
13+
<filter>
14+
<whitelist>
15+
<directory>./src/</directory>
16+
</whitelist>
17+
</filter>
18+
</phpunit>

src/retry.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace igorw;
4+
5+
class FailingTooHardException extends \Exception {}
6+
7+
function retry($retries, callable $fn)
8+
{
9+
beginning:
10+
try {
11+
return $fn();
12+
} catch (\Exception $e) {
13+
if (!$retries) {
14+
throw new FailingTooHardException('', 0, $e);
15+
}
16+
$retries--;
17+
goto beginning;
18+
}
19+
}

tests/RetryTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace igorw;
4+
5+
class RetryTest extends \PHPUnit_Framework_TestCase
6+
{
7+
function testRetryWithoutFailing()
8+
{
9+
$i = 0;
10+
$value = retry(1, function () use (&$i) {
11+
$i++;
12+
return 5;
13+
});
14+
$this->assertSame(1, $i);
15+
$this->assertSame(5, $value);
16+
}
17+
18+
function testRetryFailingOnce()
19+
{
20+
$i = 0;
21+
$failed = false;
22+
$value = retry(1, function () use (&$i, &$failed) {
23+
$i++;
24+
if (!$failed) {
25+
$failed = true;
26+
throw new \RuntimeException('roflcopter');
27+
}
28+
return 5;
29+
});
30+
$this->assertSame(2, $i);
31+
$this->assertSame(5, $value);
32+
}
33+
34+
function testRetryFailingTooHard()
35+
{
36+
$e = null;
37+
$i = 0;
38+
try {
39+
retry(1, function () use (&$i) {
40+
$i++;
41+
throw new \RuntimeException('rofl');
42+
});
43+
} catch (\Exception $e) {
44+
}
45+
46+
$this->assertInstanceof('igorw\FailingTooHardException', $e);
47+
$this->assertInstanceof('RuntimeException', $e->getPrevious());
48+
$this->assertSame('rofl', $e->getPrevious()->getMessage());
49+
$this->assertSame(2, $i);
50+
}
51+
52+
function testRetryManyTimes()
53+
{
54+
$e = null;
55+
$i = 0;
56+
try {
57+
retry(1000, function () use (&$i, &$failed) {
58+
$i++;
59+
throw new \RuntimeException('dogecoin');
60+
});
61+
} catch (\Exception $e) {
62+
}
63+
64+
$this->assertInstanceof('igorw\FailingTooHardException', $e);
65+
$this->assertInstanceof('RuntimeException', $e->getPrevious());
66+
$this->assertSame('dogecoin', $e->getPrevious()->getMessage());
67+
$this->assertSame(1001, $i);
68+
}
69+
}

0 commit comments

Comments
 (0)