summaryrefslogtreecommitdiff
path: root/indra/newview/tests/llhttpretrypolicy_test.cpp
blob: ed7f3ba326875bd4895b2f73687f3f3f6c266344 (plain)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/** 
 * @file llhttpretrypolicy_test.cpp
 * @brief Header tests to exercise the LLHTTPRetryPolicy classes.
 *
 * $LicenseInfo:firstyear=2013&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2013, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "../llviewerprecompiledheaders.h"
#include "../llhttpretrypolicy.h"
#include "lltut.h"

namespace tut
{
struct TestData
{
};

typedef test_group<TestData>	RetryPolicyTestGroup;
typedef RetryPolicyTestGroup::object		RetryPolicyTestObject;
RetryPolicyTestGroup retryPolicyTestGroup("retry_policy");

template<> template<>
void RetryPolicyTestObject::test<1>()
{
	LLAdaptiveRetryPolicy never_retry(1.0,1.0,1.0,0);
	LLSD headers;
	F32 wait_seconds;
	
	// No retry until we've failed a try.
	ensure("never retry 0", !never_retry.shouldRetry(wait_seconds));

	// 0 retries max.
	never_retry.onFailure(500,headers);
	ensure("never retry 1", !never_retry.shouldRetry(wait_seconds)); 
}

template<> template<>
void RetryPolicyTestObject::test<2>()
{
	LLAdaptiveRetryPolicy retry404(1.0,2.0,3.0,10);
	LLSD headers;
	F32 wait_seconds;
	
	retry404.onFailure(404,headers);
	ensure("no retry on 404", !retry404.shouldRetry(wait_seconds)); 
}

template<> template<>
void RetryPolicyTestObject::test<3>()
{
	// Should retry after 1.0, 2.0, 3.0, 3.0 seconds.
	LLAdaptiveRetryPolicy basic_retry(1.0,3.0,2.0,4);
	LLSD headers;
	F32 wait_seconds;
	bool should_retry;
	U32 frac_bits = 6;

	// No retry until we've failed a try.
	ensure("basic_retry 0", !basic_retry.shouldRetry(wait_seconds));

	// Starting wait 1.0
	basic_retry.onFailure(500,headers);
	should_retry = basic_retry.shouldRetry(wait_seconds);
	ensure("basic_retry 1", should_retry);
	ensure_approximately_equals("basic_retry 1", wait_seconds, 1.0F, frac_bits);

	// Double wait to 2.0
	basic_retry.onFailure(500,headers);
	should_retry = basic_retry.shouldRetry(wait_seconds);
	ensure("basic_retry 2", should_retry);
	ensure_approximately_equals("basic_retry 2", wait_seconds, 2.0F, frac_bits);

	// Hit max wait of 3.0 (4.0 clamped to max 3)
	basic_retry.onFailure(500,headers);
	should_retry = basic_retry.shouldRetry(wait_seconds);
	ensure("basic_retry 3", should_retry);
	ensure_approximately_equals("basic_retry 3", wait_seconds, 3.0F, frac_bits);

	// At max wait, should stay at 3.0
	basic_retry.onFailure(500,headers);
	should_retry = basic_retry.shouldRetry(wait_seconds);
	ensure("basic_retry 4", should_retry);
	ensure_approximately_equals("basic_retry 4", wait_seconds, 3.0F, frac_bits);

	// Max retries, should fail now.
	basic_retry.onFailure(500,headers);
	should_retry = basic_retry.shouldRetry(wait_seconds);
	ensure("basic_retry 5", !should_retry);

	// Max retries, should fail now.
	basic_retry.onFailure(500,headers);
	should_retry = basic_retry.shouldRetry(wait_seconds);
	ensure("basic_retry 5", !should_retry);

	// After a success, should reset to the starting state.
	basic_retry.onSuccess();

	// No retry until we've failed a try.
	ensure("basic_retry 6", !basic_retry.shouldRetry(wait_seconds));

	// Starting wait 1.0
	basic_retry.onFailure(500,headers);
	should_retry = basic_retry.shouldRetry(wait_seconds);
	ensure("basic_retry 7", should_retry);
	ensure_approximately_equals("basic_retry 7", wait_seconds, 1.0F, frac_bits);

	// Double wait to 2.0
	basic_retry.onFailure(500,headers);
	should_retry = basic_retry.shouldRetry(wait_seconds);
	ensure("basic_retry 8", should_retry);
	ensure_approximately_equals("basic_retry 8", wait_seconds, 2.0F, frac_bits);
}

// Retries should stop as soon as a non-5xx error is received.
template<> template<>
void RetryPolicyTestObject::test<4>()
{
	// Should retry after 1.0, 2.0, 3.0, 3.0 seconds.
	LLAdaptiveRetryPolicy killer404(1.0,3.0,2.0,4);
	LLSD headers;
	F32 wait_seconds;
	bool should_retry;
	U32 frac_bits = 6;

	// Starting wait 1.0
	killer404.onFailure(500,headers);
	should_retry = killer404.shouldRetry(wait_seconds);
	ensure("killer404 1", should_retry);
	ensure_approximately_equals("killer404 1", wait_seconds, 1.0F, frac_bits);

	// Double wait to 2.0
	killer404.onFailure(500,headers);
	should_retry = killer404.shouldRetry(wait_seconds);
	ensure("killer404 2", should_retry);
	ensure_approximately_equals("killer404 2", wait_seconds, 2.0F, frac_bits);

	// Should fail on non-5xx
	killer404.onFailure(404,headers);
	should_retry = killer404.shouldRetry(wait_seconds);
	ensure("killer404 3", !should_retry);

	// After a non-5xx, should keep failing.
	killer404.onFailure(500,headers);
	should_retry = killer404.shouldRetry(wait_seconds);
	ensure("killer404 4", !should_retry);
}

// Test handling of "retry-after" header. If present, this header
// value overrides the computed delay, but does not affect the
// progression of delay values.  For example, if the normal
// progression of delays would be 1,2,4,8..., but the 2nd and 3rd calls
// get a retry header of 33, the pattern would become 1,33,33,8...
template<> template<>
void RetryPolicyTestObject::test<5>()
{
	LLAdaptiveRetryPolicy policy(1.0,25.0,2.0,6);
	LLSD headers_with_retry;
	headers_with_retry[HTTP_IN_HEADER_RETRY_AFTER] = "666";
	LLSD headers_without_retry;
	F32 wait_seconds;
	bool should_retry;
	U32 frac_bits = 6;

	policy.onFailure(500,headers_without_retry);
	should_retry = policy.shouldRetry(wait_seconds);
	ensure("retry header 1", should_retry);
	ensure_approximately_equals("retry header 1", wait_seconds, 1.0F, frac_bits);

	policy.onFailure(500,headers_without_retry);
	should_retry = policy.shouldRetry(wait_seconds);
	ensure("retry header 2", should_retry);
	ensure_approximately_equals("retry header 2", wait_seconds, 2.0F, frac_bits);

	policy.onFailure(500,headers_with_retry);
	should_retry = policy.shouldRetry(wait_seconds);
	ensure("retry header 3", should_retry);
	// 4.0 overrides by header -> 666.0
	ensure_approximately_equals("retry header 3", wait_seconds, 666.0F, frac_bits);

	policy.onFailure(500,headers_with_retry);
	should_retry = policy.shouldRetry(wait_seconds);
	ensure("retry header 4", should_retry);
	// 8.0 overrides by header -> 666.0
	ensure_approximately_equals("retry header 4", wait_seconds, 666.0F, frac_bits);

	policy.onFailure(500,headers_without_retry);
	should_retry = policy.shouldRetry(wait_seconds);
	ensure("retry header 5", should_retry);
	ensure_approximately_equals("retry header 5", wait_seconds, 16.0F, frac_bits);

	policy.onFailure(500,headers_without_retry);
	should_retry = policy.shouldRetry(wait_seconds);
	ensure("retry header 6", should_retry);
	ensure_approximately_equals("retry header 6", wait_seconds, 25.0F, frac_bits);

	policy.onFailure(500,headers_with_retry);
	should_retry = policy.shouldRetry(wait_seconds);
	ensure("retry header 7", !should_retry);
}

// Test getSecondsUntilRetryAfter(const std::string& retry_after, F32& seconds_to_wait),
// used by header parsing of the retry policy.
template<> template<>
void RetryPolicyTestObject::test<6>()
{
	F32 seconds_to_wait;
	bool success;

	std::string str1("0");
	seconds_to_wait = F32_MAX;
	success = getSecondsUntilRetryAfter(str1, seconds_to_wait);
	ensure("parse 1", success);
	ensure_equals("parse 1", seconds_to_wait, 0.0);

	std::string str2("999.9");
	seconds_to_wait = F32_MAX;
	success = getSecondsUntilRetryAfter(str2, seconds_to_wait);
	ensure("parse 2", success);
	ensure_approximately_equals("parse 2", seconds_to_wait, 999.9F, 8);

	time_t nowseconds;
	time(&nowseconds);
	std::string str3 = LLDate((F64)(nowseconds+44)).asRFC1123();
	seconds_to_wait = F32_MAX;
	success = getSecondsUntilRetryAfter(str3, seconds_to_wait);
	std::cerr << " str3 [" << str3 << "]" << std::endl;
	ensure("parse 3", success);
	ensure_approximately_equals_range("parse 3", seconds_to_wait, 44.0F, 2.0F);
}

// Test retry-after field in both llmessage and CoreHttp headers.
template<> template<>
void RetryPolicyTestObject::test<7>()
{
	std::cerr << "7 starts" << std::endl;
	
	LLSD sd_headers;
	time_t nowseconds;
	time(&nowseconds);
	LLAdaptiveRetryPolicy policy(17.0,644.0,3.0,5);
	F32 seconds_to_wait;
	bool should_retry;

	// No retry until we've failed a try.
	ensure("header 0", !policy.shouldRetry(seconds_to_wait));
	
	// no retry header, use default.
	policy.onFailure(500,LLSD());
	should_retry = policy.shouldRetry(seconds_to_wait);
	ensure("header 1", should_retry);
	ensure_approximately_equals("header 1", seconds_to_wait, 17.0F, 6);

	// retry header should override, give delay of 0
	std::string date_string = LLDate((F64)(nowseconds+7)).asRFC1123();
	sd_headers[HTTP_IN_HEADER_RETRY_AFTER] = date_string;
	policy.onFailure(503,sd_headers);
	should_retry = policy.shouldRetry(seconds_to_wait);
	ensure("header 2", should_retry);
	ensure_approximately_equals_range("header 2", seconds_to_wait, 7.0F, 2.0F);

	LLCore::HttpResponse *response;
	LLCore::HttpHeaders *headers;

	response = new LLCore::HttpResponse();
	headers = new LLCore::HttpHeaders();
	response->setStatus(503);
	response->setHeaders(headers);
	headers->append(HTTP_IN_HEADER_RETRY_AFTER, std::string("600"));
	policy.onFailure(response);
	should_retry = policy.shouldRetry(seconds_to_wait);
	ensure("header 3",should_retry);
	ensure_approximately_equals("header 3", seconds_to_wait, 600.0F, 6);
	response->release();

	response = new LLCore::HttpResponse();
	headers = new LLCore::HttpHeaders();
	response->setStatus(503);
	response->setHeaders(headers);
	time(&nowseconds);
	date_string = LLDate((F64)(nowseconds+77)).asRFC1123();
	std::cerr << "date_string [" << date_string << "]" << std::endl;
	headers->append(HTTP_IN_HEADER_RETRY_AFTER,date_string);
	policy.onFailure(response);
	should_retry = policy.shouldRetry(seconds_to_wait);
	ensure("header 4",should_retry);
	ensure_approximately_equals_range("header 4", seconds_to_wait, 77.0F, 2.0F);
	response->release();

	// Timeout should be clamped at max.
	policy.onFailure(500,LLSD());
	should_retry = policy.shouldRetry(seconds_to_wait);
	ensure("header 5", should_retry);
	ensure_approximately_equals("header 5", seconds_to_wait, 644.0F, 6);

	// No more retries.
	policy.onFailure(500,LLSD());
	should_retry = policy.shouldRetry(seconds_to_wait);
	ensure("header 6", !should_retry);
}

}