summaryrefslogtreecommitdiff
path: root/indra/llmessage/tests/llhttpretrypolicy_test.cpp
blob: ebf626d9a894420ea895e89fc7bd13d7ee65fcd8 (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
/** 
 * @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 "linden_common.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;
	
	never_retry.onFailure(500,headers);
	ensure("never retry", !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;

	// 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);
}

// 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).asRFC1123();
	seconds_to_wait = F32_MAX;
	success = getSecondsUntilRetryAfter(str3, seconds_to_wait);
	ensure("parse 3", success);
	ensure_approximately_equals("parse 3", seconds_to_wait, 0.0F, 6);
}

}