This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathRetryTest.php
164 lines (149 loc) · 4.97 KB
/
RetryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Doctrine\MongoDB\Tests;
use Doctrine\MongoDB\Collection;
use Doctrine\MongoDB\Configuration;
use Doctrine\MongoDB\Connection;
use Doctrine\MongoDB\Cursor;
class RetryTest extends DatabaseTestCase
{
public function setUp()
{
$config = new Configuration();
$config->setRetryConnect(1);
$config->setRetryQuery(1);
$this->conn = new Connection(null, [], $config);
}
public function testFunctional()
{
$test = $this->conn->selectDatabase('test')->selectCollection('test');
$doc = ['test' => 'test'];
$test->insert($doc);
$check = $test->findOne(['test' => 'test']);
$this->assertEquals('test', $check['test']);
$check = $test->find(['test' => 'test']);
$this->assertInstanceOf('Doctrine\MongoDB\Cursor', $check);
$array = $check->toArray();
$this->assertInternalType('array', $array);
$array = array_values($array);
$this->assertEquals('test', $array[0]['test']);
}
public function testCollectionRetries()
{
$database = $this->conn->selectDatabase('test');
$mongoCollection = $database->selectCollection('test')->getMongoCollection();
$collection = new CollectionStub($database, $mongoCollection, $this->conn->getEventManager(), 1);
$exception = new \MongoException('Test');
try {
$collection->testRetries($exception);
$this->fail();
} catch (\Exception $e) {
}
$this->assertSame($e, $exception);
$this->assertEquals(2, $collection->numTimesTried);
}
public function testConnectionRetries()
{
$config = new Configuration();
$config->setRetryConnect(1);
$config->setRetryQuery(1);
$conn = new ConnectionStub(null, [], $config);
$exception = new \MongoException('Test');
try {
$conn->testRetries($exception);
$this->fail();
} catch (\Exception $e) {
}
$this->assertSame($e, $exception);
$this->assertEquals(2, $conn->numTimesTried);
}
public function testCursorCursorExceptionRetries()
{
$collection = $this->conn->selectDatabase('test')->selectCollection('test');
$mongoCursor = $collection->find()->getMongoCursor();
$cursor = new CursorStub($collection, $mongoCursor, [], [], 1);
$exception = new \MongoCursorException('Test');
try {
$cursor->testCursorExceptionRetries($exception);
$this->fail();
} catch (\Exception $e) {
}
$this->assertSame($e, $exception);
$this->assertEquals(2, $cursor->numTimesTried);
}
public function testCursorConnectionExceptionRetries()
{
$collection = $this->conn->selectDatabase('test')->selectCollection('test');
$mongoCursor = $collection->find()->getMongoCursor();
$cursor = new CursorStub($collection, $mongoCursor, [], [], 1);
$exception = new \MongoConnectionException('Test');
try {
$cursor->testConnectionExceptionRetries($exception);
$this->fail();
} catch (\Exception $e) {
}
$this->assertSame($e, $exception);
$this->assertEquals(2, $cursor->numTimesTried);
}
}
class CollectionStub extends Collection
{
public $numTimesTried = 0;
public function testRetries($exception)
{
$numTimesTried = 0;
try {
$this->retry(function() use(&$numTimesTried, $exception) {
$numTimesTried++;
throw $exception;
});
} catch (\MongoException $e) {}
$this->numTimesTried = $numTimesTried;
throw $e;
}
}
class ConnectionStub extends Connection
{
public $numTimesTried = 0;
public function testRetries($exception)
{
$numTimesTried = 0;
try {
$this->retry(function() use(&$numTimesTried, $exception) {
$numTimesTried++;
throw $exception;
});
} catch (\MongoException $e) {}
$this->numTimesTried = $numTimesTried;
throw $e;
}
}
class CursorStub extends Cursor
{
public $numTimesTried = 0;
public function testCursorExceptionRetries($exception)
{
$this->numTimesTried = 0;
$numTimesTried = 0;
try {
$this->retry(function() use(&$numTimesTried, $exception) {
$numTimesTried++;
throw $exception;
}, true);
} catch (\MongoCursorException $e) {}
$this->numTimesTried = $numTimesTried;
throw $e;
}
public function testConnectionExceptionRetries($exception)
{
$this->numTimesTried = 0;
$numTimesTried = 0;
try {
$this->retry(function() use(&$numTimesTried, $exception) {
$numTimesTried++;
throw $exception;
}, true);
} catch (\MongoConnectionException $e) {}
$this->numTimesTried = $numTimesTried;
throw $e;
}
}