forked from appmode/twitterStream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitterTest.php
264 lines (218 loc) · 8.36 KB
/
twitterTest.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
// enable unit test mode
define("UNIT_TEST", true);
// include the script
require_once("twitterStream.php");
//============================================================================//
// Tests
//============================================================================//
//----------------------------------------------------------------------------//
// Twitter stream class
//----------------------------------------------------------------------------//
class twitterTest extends PHPUnit_Framework_TestCase
{
//------------------------------------------------------------------------//
// backoff
//------------------------------------------------------------------------//
function testBackoff()
{
$objTwitter = new twitterTestStream("void", "void");
// set up exceptions
$objTcpException = new NetworkTcpException();
$objDnsException = new NetworkDnsException();
$objTimeException = new OutOfTimeException();
// unusable exception should be re-thrown
try
{
$objTwitter->public_backoff($objTimeException);
$this->fail("an Exception was expected");
}
catch (OutOfTimeException $e)
{
}
// 1st backoff should be 250ms
$objTwitter->public_backoff($objTcpException);
$this->assertEquals($objTwitter->intLastBackoff, 250);
// linear backoff
// 2nd backoff should be 500ms
$objTwitter->public_backoff($objTcpException);
$this->assertEquals($objTwitter->intLastBackoff, 500);
// new error type should reset the backoff
// 1st backoff should be 1000ms
$objTwitter->public_backoff($objDnsException);
$this->assertEquals($objTwitter->intLastBackoff, 1000);
// exponential backoff
// 2nd backoff should be 2000ms
$objTwitter->public_backoff($objDnsException);
$this->assertEquals($objTwitter->intLastBackoff, 2000);
// exponential backoff
// 3rd backoff should be 4000ms
$objTwitter->public_backoff($objDnsException);
$this->assertEquals($objTwitter->intLastBackoff, 4000);
// 1st backoff should be 250ms
$objTwitter->public_backoff($objTcpException);
$this->assertEquals($objTwitter->intLastBackoff, 250);
// max backoff should be 16000ms
$objTwitter->intLastBackoff = 16000;
$objTwitter->public_backoff($objTcpException);
$this->assertEquals($objTwitter->intLastBackoff, 16000);
// max retry should be 64
$objTwitter->intBackoffCount = 63;
$objTwitter->public_backoff($objTcpException);
// 64th retry should be allowed
$this->assertEquals($objTwitter->intLastBackoff, 16000);
// 65th retry should throw an exception
try
{
$objTwitter->public_backoff($objTcpException);
$this->fail("a NetworkRetryException was expected");
}
catch (NetworkRetryException $e)
{
}
// 1st backoff should be 1000ms
$objTwitter->public_backoff($objDnsException);
$this->assertEquals($objTwitter->intLastBackoff, 1000);
// max time reached
$objTwitter->setEndTime(time());
try
{
$objTwitter->public_backoff($objTcpException);
$this->fail("an OutOfTimeException was expected");
}
catch (OutOfTimeException $e)
{
}
//TODO!!!! : we should test the output to stderr
//TODO!!!! : we should test that the script is waiting the correct time
}
//------------------------------------------------------------------------//
// checkIfWeNeedToDie
//------------------------------------------------------------------------//
function testCheckIfWeNeedToDie()
{
$objTwitter = new twitterTestStream("void", "void");
// should not throw an exception if we have no reason to die
$objTwitter->public_checkIfWeNeedToDie();
// should not throw an exception if we have no tweets
$objTwitter->setMaxTweets(5);
$objTwitter->public_checkIfWeNeedToDie();
// should not throw an exception if we have not reached maximum tweets
$objTwitter->intTweets = 4;
$objTwitter->public_checkIfWeNeedToDie();
// should throw an exception if we have reached maximum tweets
$objTwitter->intTweets = 5;
try
{
$objTwitter->public_checkIfWeNeedToDie();
$this->fail("an OutOfTweetsException was expected");
}
catch (OutOfTweetsException $e)
{
}
// should not throw an exception if we have not reached maximum tweets
$objTwitter->intTweets = 4;
$objTwitter->public_checkIfWeNeedToDie();
// should throw an exception if max time reached
$objTwitter->setEndTime(time());
try
{
$objTwitter->public_checkIfWeNeedToDie();
$this->fail("an OutOfTimeException was expected");
}
catch (OutOfTimeException $e)
{
}
// should throw an exception if max time exceeded
$objTwitter->setEndTime(time() - 5);
try
{
$objTwitter->public_checkIfWeNeedToDie();
$this->fail("an OutOfTimeException was expected");
}
catch (OutOfTimeException $e)
{
}
// should not throw an exception if max time not reached
$objTwitter->setEndTime(time() + 5);
$objTwitter->public_checkIfWeNeedToDie();
}
//------------------------------------------------------------------------//
// getTimeout
//------------------------------------------------------------------------//
function testGetTimeout()
{
$objTwitter = new twitterTestStream("void", "void");
// should return correct timeout if max time not set
$this->assertEquals($objTwitter->public_getTimeout(30), 30);
// should throw an exception if max time reached
$objTwitter->setEndTime(time());
try
{
$objTwitter->public_getTimeout(30);
$this->fail("an OutOfTimeException was expected");
}
catch (OutOfTimeException $e)
{
}
// should throw an exception if max time exceeded
$objTwitter->setEndTime(time() - 5);
try
{
$objTwitter->public_getTimeout(30);
$this->fail("an OutOfTimeException was expected");
}
catch (OutOfTimeException $e)
{
}
// should not throw an exception if max time not reached
// should return a reduced timeout
$objTwitter->setEndTime(time() + 5);
$this->assertEquals($objTwitter->public_getTimeout(30), 5);
// should not throw an exception if max time not reached
// should return correct timeout
$objTwitter->setEndTime(time() + 60);
$this->assertEquals($objTwitter->public_getTimeout(30), 30);
}
}
//============================================================================//
// Classes
//============================================================================//
//----------------------------------------------------------------------------//
// Twitter test stream class
//----------------------------------------------------------------------------//
/*
* Adds public methods to access protected methods in the twitterStream class
*/
class twitterTestStream extends twitterStream
{
public function public_getHostAddress()
{
return $this->getHostAddress();
}
public function public_readLine()
{
return $this->readLine();
}
public function public_bufferLine()
{
return $this->bufferLine();
}
public function public_reconnect($intTimeout)
{
return $this->reconnect($intTimeout);
}
public function public_backoff($objException)
{
return $this->backoff($objException);
}
public function public_checkIfWeNeedToDie()
{
return $this->checkIfWeNeedToDie();
}
public function public_getTimeout($numTimeout)
{
return $this->getTimeout($numTimeout);
}
}
?>