Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 Copyright 2011 Google Inc | 2 Copyright 2011 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0 (the "License"); | 4 Licensed under the Apache License, Version 2.0 (the "License"); |
5 you may not use this file except in compliance with the License. | 5 you may not use this file except in compliance with the License. |
6 You may obtain a copy of the License at | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
(...skipping 12 matching lines...) Expand all Loading... | |
23 using System.Net.Http.Headers; | 23 using System.Net.Http.Headers; |
24 using System.Text; | 24 using System.Text; |
25 using System.Threading; | 25 using System.Threading; |
26 using System.Threading.Tasks; | 26 using System.Threading.Tasks; |
27 | 27 |
28 using Ionic.Zlib; | 28 using Ionic.Zlib; |
29 using NUnit.Framework; | 29 using NUnit.Framework; |
30 | 30 |
31 using Google.Apis.Discovery; | 31 using Google.Apis.Discovery; |
32 using Google.Apis.Http; | 32 using Google.Apis.Http; |
33 using Google.Apis.Logging; | |
34 using Google.Apis.Requests; | 33 using Google.Apis.Requests; |
35 using Google.Apis.Services; | 34 using Google.Apis.Services; |
36 using Google.Apis.Testing; | 35 using Google.Apis.Testing; |
37 using Google.Apis.Util; | 36 using Google.Apis.Util; |
38 | 37 |
39 | 38 |
40 namespace Google.Apis.Tests.Apis.Requests | 39 namespace Google.Apis.Tests.Apis.Requests |
41 { | 40 { |
42 /// <summary> Tests for the <see cref="Google.Apis.Requests.ClientServiceReq uest"/>. </summary> | 41 /// <summary> Tests for the <see cref="Google.Apis.Requests.ClientServiceReq uest"/>. </summary> |
43 [TestFixture] | 42 [TestFixture] |
44 public class ClientServiceRequestTest | 43 public class ClientServiceRequestTest |
45 { | 44 { |
46 [TestFixtureSetUp] | 45 [TestFixtureSetUp] |
47 public void SetUp() | 46 public void SetUp() |
48 { | 47 { |
49 // Uncomment to enable logging during tests | 48 // Uncomment to enable logging during tests |
50 // ApplicationContext.RegisterLogger(new Log4NetLogger()); | 49 // ApplicationContext.RegisterLogger(new Google.Apis.Logging.Log4Net Logger()); |
51 } | 50 } |
52 | 51 |
53 /// <summary> Helper method to get a string from the stream. </summary> | 52 /// <summary> Helper method to get a string from the stream. </summary> |
54 private static string ExtractStringFromStream(Stream stream) | 53 private static string ExtractStringFromStream(Stream stream) |
55 { | 54 { |
56 var buffer = new byte[1000]; | 55 var buffer = new byte[1000]; |
57 var len = stream.Read(buffer, 0, 1000); | 56 var len = stream.Read(buffer, 0, 1000); |
58 return Encoding.UTF8.GetString(buffer, 0, len); | 57 return Encoding.UTF8.GetString(buffer, 0, len); |
59 } | 58 } |
60 | 59 |
61 /// <summary> A mock response class. </summary> | 60 /// <summary> A mock response class. </summary> |
62 class MockResponse : IDirectResponseSchema | 61 class MockResponse : IDirectResponseSchema |
63 { | 62 { |
64 [Newtonsoft.Json.JsonPropertyAttribute("etag")] | 63 [Newtonsoft.Json.JsonPropertyAttribute("etag")] |
65 public string ETag { get; set; } | 64 public string ETag { get; set; } |
66 | 65 |
67 [Newtonsoft.Json.JsonPropertyAttribute("name")] | 66 [Newtonsoft.Json.JsonPropertyAttribute("name")] |
68 public string Name { get; set; } | 67 public string Name { get; set; } |
69 | 68 |
70 [Newtonsoft.Json.JsonPropertyAttribute("id")] | 69 [Newtonsoft.Json.JsonPropertyAttribute("id")] |
71 public int Id { get; set; } | 70 public int Id { get; set; } |
72 | 71 |
73 public override bool Equals(object obj) | 72 public override bool Equals(object obj) |
74 { | 73 { |
75 var other = obj as MockResponse; | 74 var other = obj as MockResponse; |
76 return (other != null && other.ETag == ETag && other.Name == Nam e && other.Id == Id); | 75 return (other != null && other.ETag == ETag && other.Name == Nam e && other.Id == Id); |
77 } | 76 } |
77 | |
78 public override int GetHashCode() | |
79 { | |
80 return (ETag ?? string.Empty).GetHashCode() ^ (Name ?? string.Em pty).GetHashCode() Id; | |
81 } | |
78 } | 82 } |
79 | 83 |
80 /// <summary> A mock request class. </summary> | 84 /// <summary> A mock request class. </summary> |
81 class MockRequest : IDirectResponseSchema | 85 class MockRequest : IDirectResponseSchema |
82 { | 86 { |
83 [Newtonsoft.Json.JsonPropertyAttribute("etag")] | 87 [Newtonsoft.Json.JsonPropertyAttribute("etag")] |
84 public string ETag { get; set; } | 88 public string ETag { get; set; } |
85 | 89 |
86 [Newtonsoft.Json.JsonPropertyAttribute("long_name")] | 90 [Newtonsoft.Json.JsonPropertyAttribute("long_name")] |
87 public string Name { get; set; } | 91 public string Name { get; set; } |
88 | 92 |
89 public override bool Equals(object obj) | 93 public override bool Equals(object obj) |
90 { | 94 { |
91 var other = obj as MockRequest; | 95 var other = obj as MockRequest; |
92 return (other != null && other.ETag == ETag && other.Name == Nam e); | 96 return (other != null && other.ETag == ETag && other.Name == Nam e); |
97 } | |
98 | |
99 public override int GetHashCode() | |
100 { | |
101 return (ETag ?? string.Empty).GetHashCode() ^ (Name ?? string.Em pty).GetHashCode(); | |
93 } | 102 } |
94 } | 103 } |
95 | 104 |
96 /// <summary> A mock service request which returns <see cref="MockRespon se"/>. </summary> | 105 /// <summary> A mock service request which returns <see cref="MockRespon se"/>. </summary> |
97 class TestClientServiceRequest : ClientServiceRequest<MockResponse> | 106 class TestClientServiceRequest : ClientServiceRequest<MockResponse> |
98 { | 107 { |
99 /// <summary> Gets or sets a request number. It's used on concurrent tests. </summary> | 108 /// <summary> Gets or sets a request number. It's used on concurrent tests. </summary> |
100 public int CallNum { get; set; } | 109 public int CallNum { get; set; } |
101 private string httpMethod; | 110 private string httpMethod; |
102 private object body; | 111 private object body; |
103 | 112 |
104 public TestClientServiceRequest(IClientService service, string httpM ethod, object body) | 113 public TestClientServiceRequest(IClientService service, string httpM ethod, object body) |
105 : base(service) | 114 : base(service) |
106 { | 115 { |
107 this.httpMethod = httpMethod; | 116 this.httpMethod = httpMethod; |
108 this.body = body; | 117 this.body = body; |
109 | 118 InitParameters(); |
110 _requestParameters = new Dictionary<string, IParameter>(); | |
111 } | 119 } |
112 | 120 |
113 public override string MethodName | 121 public override string MethodName |
114 { | 122 { |
115 get { return httpMethod; } | 123 get { return httpMethod; } |
116 } | 124 } |
117 | 125 |
118 public override string RestPath | 126 public override string RestPath |
119 { | 127 { |
120 get { return "restPath" CallNum; } | 128 get { return "restPath" CallNum; } |
121 } | 129 } |
122 | 130 |
123 public override string HttpMethod | 131 public override string HttpMethod |
124 { | 132 { |
125 get { return httpMethod; } | 133 get { return httpMethod; } |
126 } | 134 } |
127 | 135 |
128 protected override object GetBody() | 136 protected override object GetBody() |
129 { | 137 { |
130 return body; | 138 return body; |
131 } | 139 } |
132 } | 140 } |
133 | 141 |
134 /// <summary> A mock message handler which returns an error. </summary> | 142 /// <summary> A mock message handler which returns an error. </summary> |
135 class ErrorMessageHanlder : CountableMessageHandler | 143 class ErrorMessageHanlder : CountableMessageHandler |
136 { | 144 { |
137 public string ExpectedError = | 145 public string ExpectedError = |
138 @"Message[Login Required] Location[Authorization - header] Reaso n[required] Domain[global]"; | 146 @"Message[Login Required] Location[Authorization - header] Reaso n[required] Domain[global]"; |
139 | 147 |
140 protected override async Task<HttpResponseMessage> SendAsyncCore(Htt pRequestMessage request, | 148 protected override Task<HttpResponseMessage> SendAsyncCore(HttpReque stMessage request, |
141 CancellationToken cancellationToken) | 149 CancellationToken cancellationToken) |
142 { | 150 { |
143 var response = new HttpResponseMessage(); | |
144 var error = @"{ | 151 var error = @"{ |
145 ""error"": { | 152 ""error"": { |
146 ""errors"": [ | 153 ""errors"": [ |
147 { | 154 { |
148 ""domain"": ""global"", | 155 ""domain"": ""global"", |
149 ""reason"": ""required"", | 156 ""reason"": ""required"", |
150 ""message"": ""Login Required"", | 157 ""message"": ""Login Required"", |
151 ""locationType"": ""header"", | 158 ""locationType"": ""header"", |
152 ""location"": ""Authorization"" | 159 ""location"": ""Authorization"" |
153 } | 160 } |
154 ], | 161 ], |
155 ""code"": 401, | 162 ""code"": 401, |
156 ""message"": ""Login Required"" | 163 ""message"": ""Login Required"" |
157 } | 164 } |
158 }"; | 165 }"; |
159 response.Content = new StringContent(error); | 166 |
160 response.StatusCode = System.Net.HttpStatusCode.Unauthorized; | 167 var response = new HttpResponseMessage |
161 return response; | 168 { |
169 Content = new StringContent(error), | |
170 StatusCode = System.Net.HttpStatusCode.Unauthorized | |
171 }; | |
172 | |
173 TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompleti onSource<HttpResponseMessage>(); | |
174 tcs.SetResult(response); | |
175 return tcs.Task; | |
162 } | 176 } |
163 } | 177 } |
164 | 178 |
165 /// <summary> Tests message handler which tests the content on the reque st and the response. </summary> | 179 /// <summary> Tests message handler which tests the content on the reque st and the response. </summary> |
166 class TestBodyMessageHnalder : CountableMessageHandler | 180 class TestBodyMessageHnalder : CountableMessageHandler |
167 { | 181 { |
168 /// <summary> Gets or sets indication is GZip is eanbled. </summary> | 182 /// <summary> Gets or sets indication is GZip is eanbled. </summary> |
169 public bool GZipEnabled { get; set; } | 183 public bool GZipEnabled { get; set; } |
170 | 184 |
171 /// <summary> Gets or sets the expected request object. </summary> | 185 /// <summary> Gets or sets the expected request object. </summary> |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 | 253 |
240 /// <summary> A message handler which returns an Http response message o r throw an exception. </summary> | 254 /// <summary> A message handler which returns an Http response message o r throw an exception. </summary> |
241 class MockMessageHandler : CountableMessageHandler | 255 class MockMessageHandler : CountableMessageHandler |
242 { | 256 { |
243 private bool ThrowException { get; set; } | 257 private bool ThrowException { get; set; } |
244 public MockMessageHandler(bool throwException = false) | 258 public MockMessageHandler(bool throwException = false) |
245 { | 259 { |
246 ThrowException = throwException; | 260 ThrowException = throwException; |
247 } | 261 } |
248 | 262 |
249 protected override async Task<HttpResponseMessage> SendAsyncCore(Htt pRequestMessage request, | 263 protected override Task<HttpResponseMessage> SendAsyncCore(HttpReque stMessage request, |
250 CancellationToken cancellationToken) | 264 CancellationToken cancellationToken) |
251 { | 265 { |
252 if (ThrowException) | 266 if (ThrowException) |
253 { | 267 { |
254 throw new InvalidOperationMockException("INVALID"); | 268 throw new InvalidOperationMockException("INVALID"); |
255 } | 269 } |
256 return new HttpResponseMessage(); | 270 |
271 TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompleti onSource<HttpResponseMessage>(); | |
272 tcs.SetResult(new HttpResponseMessage()); | |
273 return tcs.Task; | |
257 } | 274 } |
258 } | 275 } |
259 | 276 |
260 /// <summary> A message handler which is used to cancel an Http request in the middle.</summary> | 277 /// <summary> A message handler which is used to cancel an Http request in the middle.</summary> |
261 class CancelRedirectMessageHandler : CountableMessageHandler | 278 class CancelRedirectMessageHandler : CountableMessageHandler |
262 { | 279 { |
263 /// <summary> The cancellation token we are going to use to cancel a request.</summary> | 280 /// <summary> The cancellation token we are going to use to cancel a request.</summary> |
264 public CancellationTokenSource CancellationTokenSource { get; set; } | 281 public CancellationTokenSource CancellationTokenSource { get; set; } |
265 | 282 |
266 /// <summary> The request index we are going to cancel.</summary> | 283 /// <summary> The request index we are going to cancel.</summary> |
267 public int CancelRequestNum { get; set; } | 284 public int CancelRequestNum { get; set; } |
268 | 285 |
269 protected override async Task<HttpResponseMessage> SendAsyncCore(Htt pRequestMessage request, | 286 protected override Task<HttpResponseMessage> SendAsyncCore(HttpReque stMessage request, |
270 CancellationToken cancellationToken) | 287 CancellationToken cancellationToken) |
271 { | 288 { |
272 if (Calls == CancelRequestNum) | 289 if (Calls == CancelRequestNum) |
273 { | 290 { |
274 CancellationTokenSource.Cancel(); | 291 CancellationTokenSource.Cancel(); |
275 } | 292 } |
276 var response = new HttpResponseMessage() | 293 var response = new HttpResponseMessage() |
277 { | 294 { |
278 StatusCode = HttpStatusCode.Redirect, | 295 StatusCode = HttpStatusCode.Redirect, |
279 RequestMessage = request | 296 RequestMessage = request |
280 }; | 297 }; |
281 response.Headers.Location = new Uri("http://www.test.com"); | 298 response.Headers.Location = new Uri("http://www.test.com"); |
282 return response; | 299 |
300 TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompleti onSource<HttpResponseMessage>(); | |
301 tcs.SetResult(response); | |
302 return tcs.Task; | |
283 } | 303 } |
284 } | 304 } |
285 | 305 |
286 /// <summary>· | 306 /// <summary>· |
287 /// A message handler which checks concurrent calls (each odd request wi ll succeeded, and even request will· | 307 /// A message handler which checks concurrent calls (each odd request wi ll succeeded, and even request will· |
288 /// fail on the first try and will succeeded in the second try. | 308 /// fail on the first try and will succeeded in the second try. |
289 /// </summary> | 309 /// </summary> |
290 class ConcurrentCallsHandler : CountableMessageHandler | 310 class ConcurrentCallsHandler : CountableMessageHandler |
291 { | 311 { |
292 /// <summary> Gets or sets the Serializer which is used to serialize and deserialize messages. </summary> | 312 /// <summary> Gets or sets the Serializer which is used to serialize and deserialize messages. </summary> |
293 public ISerializer Serializer { get; set; } | 313 public ISerializer Serializer { get; set; } |
294 | 314 |
295 protected override async Task<HttpResponseMessage> SendAsyncCore(Htt pRequestMessage request, | 315 protected override Task<HttpResponseMessage> SendAsyncCore(HttpReque stMessage request, |
296 CancellationToken cancellationToken) | 316 CancellationToken cancellationToken) |
297 { | 317 { |
298 var response = new HttpResponseMessage(); | 318 var response = new HttpResponseMessage(); |
299 var uri = request.RequestUri.AbsoluteUri; | 319 var uri = request.RequestUri.AbsoluteUri; |
300 int lastDigit = 0; | 320 int lastDigit = 0; |
301 if (int.TryParse(uri[uri.Length - 1].ToString(), out lastDigit) && lastDigit % 2 == 0) | 321 if (int.TryParse(uri[uri.Length - 1].ToString(), out lastDigit) && lastDigit % 2 == 0) |
302 { | 322 { |
303 response.StatusCode = System.Net.HttpStatusCode.ServiceUnava ilable; | 323 response.StatusCode = System.Net.HttpStatusCode.ServiceUnava ilable; |
304 request.RequestUri = new Uri(uri (lastDigit 1)); | 324 request.RequestUri = new Uri(uri (lastDigit 1)); |
305 } | 325 } |
306 else | 326 else |
307 { | 327 { |
308 var mockObject = new MockResponse { Name = "Name-" lastDig it }; | 328 var mockObject = new MockResponse { Name = "Name-" lastDig it }; |
309 var serializedObject = Serializer.Serialize(mockObject); | 329 var serializedObject = Serializer.Serialize(mockObject); |
310 response.Content = new StringContent(serializedObject, Encod ing.UTF8, "application/json"); | 330 response.Content = new StringContent(serializedObject, Encod ing.UTF8, "application/json"); |
311 } | 331 } |
312 return response; | 332 |
333 TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompleti onSource<HttpResponseMessage>(); | |
334 tcs.SetResult(response); | |
335 return tcs.Task; | |
313 } | 336 } |
314 | 337 |
315 /// <summary> Unsuccessful response handler which "handles" service unavailable responses. </summary> | 338 /// <summary> Unsuccessful response handler which "handles" service unavailable responses. </summary> |
316 internal class ServiceUnavailableUnsuccessfulResponseHandler : IHttp UnsuccessfulResponseHandler | 339 internal class ServiceUnavailableUnsuccessfulResponseHandler : IHttp UnsuccessfulResponseHandler |
317 { | 340 { |
318 public bool HandleResponse(HandleUnsuccessfulResponseArgs args) | 341 public bool HandleResponse(HandleUnsuccessfulResponseArgs args) |
319 { | 342 { |
320 return args.Response.StatusCode == System.Net.HttpStatusCode .ServiceUnavailable; | 343 return args.Response.StatusCode == System.Net.HttpStatusCode .ServiceUnavailable; |
321 } | 344 } |
322 } | 345 } |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
508 Assert.That(handler.Calls, Is.EqualTo(1)); | 531 Assert.That(handler.Calls, Is.EqualTo(1)); |
509 // the returned response should contain ETag, check that the ser vice add the right ETag property on· | 532 // the returned response should contain ETag, check that the ser vice add the right ETag property on· |
510 // the response | 533 // the response |
511 handler.ResponseObject.ETag = handler.ResponseETag; | 534 handler.ResponseObject.ETag = handler.ResponseETag; |
512 Assert.That(response, Is.EqualTo(handler.ResponseObject)); | 535 Assert.That(response, Is.EqualTo(handler.ResponseObject)); |
513 } | 536 } |
514 } | 537 } |
515 | 538 |
516 /// <summary>· | 539 /// <summary>· |
517 /// A subtest for testing execute when an exception is thrown during sen ding the request, with or without· | 540 /// A subtest for testing execute when an exception is thrown during sen ding the request, with or without· |
518 /// back-off. If back-off handler is attached to the service's message h andler, there are going to be several· | 541 /// back-off. If back-off handler is attached to the service's message h andler, there are going to be 3 tries· |
519 /// retries (up to 5 seconds for a single request or 3 tries in total). | 542 /// (3 is the default value of <seealso cref="ConfigurableMessageHandler .NumTries" />) before the operation· |
543 /// fails. | |
520 /// </summary> | 544 /// </summary> |
521 /// <param name="backOff">Indicates if back-off handler is attached to t he service.</param> | 545 /// <param name="backOff">Indicates if back-off handler is attached to t he service.</param> |
522 private void SubtestExecute_ThrowException(bool backOff) | 546 private void SubtestExecute_ThrowException(bool backOff) |
523 { | 547 { |
524 var handler = new MockMessageHandler(true); | 548 var handler = new MockMessageHandler(true); |
525 var initializer = new BaseClientService.Initializer() | 549 var initializer = new BaseClientService.Initializer() |
526 { | 550 { |
527 HttpClientFactory = new MockHttpClientFactory(handler) | 551 HttpClientFactory = new MockHttpClientFactory(handler) |
528 }; | 552 }; |
529 | 553 |
530 // sets the default exponential back-off policy by the input | 554 // sets the default exponential back-off policy by the input |
531 initializer.DefaultExponentialBackOffPolicy = backOff ? | 555 initializer.DefaultExponentialBackOffPolicy = backOff ? |
532 BaseClientService.ExponentialBackOffPolicy.Exception : | 556 BaseClientService.ExponentialBackOffPolicy.Exception : |
533 BaseClientService.ExponentialBackOffPolicy.None; | 557 BaseClientService.ExponentialBackOffPolicy.None; |
534 | 558 |
535 using (var service = new MockClientService(initializer)) | 559 using (var service = new MockClientService(initializer)) |
536 { | 560 { |
537 var request = new TestClientServiceRequest(service, "GET", null) ; | 561 var request = new TestClientServiceRequest(service, "GET", null) ; |
538 Assert.Throws<InvalidOperationMockException>(() => request.Execu te()); | 562 Assert.Throws<InvalidOperationMockException>(() => request.Execu te()); |
539 | 563 |
540 // if back-off is enabled, we use 5 seconds maximum wait time fo r a request.· | 564 int calls = backOff ? service.HttpClient.MessageHandler.NumTries : 1; |
541 // So we should make minimum of (lg(5) 1 calls and service.Htt pClient.MessageHandler.NumTries tries)· | |
ngmiceli
2013/08/13 19:31:26
This is a test with consistent, predictable behavi
peleyal
2013/08/13 21:46:20
Done.
| |
542 // calls | |
543 int calls = backOff ? (int)Math.Ceiling(Math.Log(5, 2) 1) : 1; | |
544 calls = Math.Min(calls, service.HttpClient.MessageHandler.NumTri es); | |
545 Assert.That(handler.Calls, Is.EqualTo(calls)); | 565 Assert.That(handler.Calls, Is.EqualTo(calls)); |
546 } | 566 } |
547 } | 567 } |
548 | 568 |
549 /// <summary>· | 569 /// <summary>· |
550 /// Tests execute when an exception is thrown during a request and expon ential back-off is enabled. | 570 /// Tests execute when an exception is thrown during a request and expon ential back-off is enabled. |
551 /// </summary> | 571 /// </summary> |
552 [Test] | 572 [Test] |
553 public void Execute_ThrowException_WithBackOff() | 573 public void Execute_ThrowException_WithBackOff() |
554 { | 574 { |
555 SubtestExecute_ThrowException(true); | 575 SubtestExecute_ThrowException(true); |
556 } | 576 } |
557 | 577 |
558 /// <summary>· | 578 /// <summary>· |
559 /// Tests execute when an exception is thrown during a request and expon ential back-off is disabled. | 579 /// Tests execute when an exception is thrown during a request and expon ential back-off is disabled. |
560 /// </summary> | 580 /// </summary> |
561 [Test] | 581 [Test] |
562 public void Execute_ThrowException_WithoutBackOff() | 582 public void Execute_ThrowException_WithoutBackOff() |
563 { | 583 { |
564 SubtestExecute_ThrowException(false); | 584 SubtestExecute_ThrowException(false); |
565 } | 585 } |
566 | 586 |
567 /// <summary>· | 587 /// <summary>· |
568 /// A subtest for testing async execute when an exception is thrown duri ng sending the request, with or without· | 588 /// A subtest for testing async execute when an exception is thrown duri ng sending the request, with or without· |
569 /// back-off handler. If back-off handler is attached to the service's m essage handler, there are going to be· | 589 /// back-off handler. If back-off handler is attached to the service's m essage handler, there are going to be 3 |
570 /// several retries (up to 5 seconds for a single request or 3 tries in total). | 590 /// tries (3 is the default value of <seealso cref="ConfigurableMessageH andler.NumTries" />) before the· |
ngmiceli
2013/08/13 19:31:26
Same as above. This comment is far less confusing,
peleyal
2013/08/13 21:46:20
Done.
| |
591 /// operation fails. | |
571 /// </summary> | 592 /// </summary> |
572 /// <param name="backOff">Indicates if back-off handler is attached to t he service.</param> | 593 /// <param name="backOff">Indicates if back-off handler is attached to t he service.</param> |
573 private void SubtestExecuteAsync_ThrowException(bool backOff) | 863 private void SubtestExecuteAsync_ThrowException(bool backOff) |
574 { | 595 { |
575 var handler = new MockMessageHandler(true); | 596 var handler = new MockMessageHandler(true); |
576 var initializer = new BaseClientService.Initializer() | 597 var initializer = new BaseClientService.Initializer() |
577 { | 598 { |
578 HttpClientFactory = new MockHttpClientFactory(handler) | 599 HttpClientFactory = new MockHttpClientFactory(handler) |
579 }; | 600 }; |
580 | 601 |
581 // configure the back-off behavior by the input | 602 // configure the back-off behavior by the input |
582 initializer.DefaultExponentialBackOffPolicy = backOff ? | 603 initializer.DefaultExponentialBackOffPolicy = backOff ? |
583 BaseClientService.ExponentialBackOffPolicy.Exception : | 604 BaseClientService.ExponentialBackOffPolicy.Exception : |
584 BaseClientService.ExponentialBackOffPolicy.None; | 605 BaseClientService.ExponentialBackOffPolicy.None; |
585 | 606 |
586 using (var service = new MockClientService(initializer)) | 607 using (var service = new MockClientService(initializer)) |
587 { | 608 { |
588 var request = new TestClientServiceRequest(service, "GET", null) ; | 609 var request = new TestClientServiceRequest(service, "GET", null) ; |
589 var task = request.ExecuteAsync(); | 610 var task = request.ExecuteAsync(); |
590 try | 611 try |
591 { | 612 { |
592 var result = task.Result; | 613 var result = task.Result; |
593 Assert.Fail("Exception should be thrown"); | 614 Assert.Fail("Exception should be thrown"); |
863 } | 615 } |
595 catch (AggregateException ex) | 616 catch (AggregateException ex) |
596 { | 617 { |
597 Assert.That(ex.InnerException, Is.AssignableFrom(typeof(Inva lidOperationMockException))); | 618 Assert.That(ex.InnerException, Is.AssignableFrom(typeof(Inva lidOperationMockException))); |
598 } | 619 } |
599 | 620 |
600 // if back-off is enabled, we use 5 seconds maximum wait time fo r a request.· | 621 int calls = backOff ? service.HttpClient.MessageHandler.NumTries : 1; |
601 // So we should make minimum of (lg(5) 1 calls and service.Htt pClient.MessageHandler.NumTries tries)· | |
ngmiceli
2013/08/13 19:31:26
Ditto, confusing comment.
peleyal
2013/08/13 21:46:20
Done.
| |
602 // calls | |
603 int calls = backOff ? (int)Math.Ceiling(Math.Log(5, 2) 1) : 1; | |
604 calls = Math.Min(calls, service.HttpClient.MessageHandler.NumTri es); | |
605 Assert.That(handler.Calls, Is.EqualTo(calls)); | 622 Assert.That(handler.Calls, Is.EqualTo(calls)); |
606 } | 623 } |
607 } | 624 } |
608 | 625 |
609 /// <summary>· | 626 /// <summary>· |
610 /// Tests async execute when an exception is thrown during a request and exponential back-off is enabled. | 627 /// Tests async execute when an exception is thrown during a request and exponential back-off is enabled. |
611 /// </summary> | 628 /// </summary> |
612 [Test] | 629 [Test] |
613 public void ExecuteAsync_ThrowException_WithBackOff() | 630 public void ExecuteAsync_ThrowException_WithBackOff() |
614 { | 631 { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
674 // catch the error | 691 // catch the error |
675 error = t.Exception.InnerException.ToString(); | 692 error = t.Exception.InnerException.ToString(); |
676 resetEvent.Set(); | 693 resetEvent.Set(); |
677 }, TaskContinuationOptions.NotOnRanToCompletion); | 694 }, TaskContinuationOptions.NotOnRanToCompletion); |
678 resetEvent.WaitOne(); | 695 resetEvent.WaitOne(); |
679 Assert.True(error.Contains(handler.ExpectedError), "Error messag e is invalid"); | 696 Assert.True(error.Contains(handler.ExpectedError), "Error messag e is invalid"); |
680 Assert.That(handler.Calls, Is.EqualTo(1)); | 697 Assert.That(handler.Calls, Is.EqualTo(1)); |
681 } | 698 } |
682 } | 699 } |
683 | 700 |
684 /// <summary> tests async execution of multiple request simultaneously. </summary> | 701 /// <summary> Tests async execution of multiple request simultaneously. </summary> |
685 [Test] | 702 [Test] |
686 public void ExecuteAsync_Simultaneously() | 703 public void ExecuteAsync_Simultaneously() |
687 { | 704 { |
688 var tasks = new List<Task<MockResponse>>(); | 705 var tasks = new List<Task<MockResponse>>(); |
689 var handler = new ConcurrentCallsHandler(); | 706 var handler = new ConcurrentCallsHandler(); |
690 var initializer = new BaseClientService.Initializer() | 707 var initializer = new BaseClientService.Initializer() |
691 { | 708 { |
692 HttpClientFactory = new MockHttpClientFactory(handler), | 709 HttpClientFactory = new MockHttpClientFactory(handler), |
693 HttpClientInitializer = new ConcurrentCallsHandler.Initializ er() | 710 HttpClientInitializer = new ConcurrentCallsHandler.Initializ er() |
694 }; | 711 }; |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
811 | 828 |
812 [RequestParameterAttribute("optionalEmpty", Google.Apis.Util.Request ParameterType.Query)] | 829 [RequestParameterAttribute("optionalEmpty", Google.Apis.Util.Request ParameterType.Query)] |
813 public string OptionalEmpty { get; set; } | 830 public string OptionalEmpty { get; set; } |
814 | 831 |
815 [RequestParameterAttribute("optionalNotPressent", Google.Apis.Util.R equestParameterType.Query)] | 832 [RequestParameterAttribute("optionalNotPressent", Google.Apis.Util.R equestParameterType.Query)] |
816 public string OptionalNotPressent { get; set; } | 833 public string OptionalNotPressent { get; set; } |
817 | 834 |
818 public ClientServiceRequestWithQueryParameters(IClientService servic e, string method, object body) | 835 public ClientServiceRequestWithQueryParameters(IClientService servic e, string method, object body) |
819 : base(service, method, body) | 836 : base(service, method, body) |
820 { | 837 { |
821 _requestParameters.Add("required", new MockParameter | 838 RequestParameters.Add("required", new Parameter |
822 { | 839 { |
823 Name = "required", | 840 Name = "required", |
824 IsRequired = true, | 841 IsRequired = true, |
825 ParameterType = "query" | 842 ParameterType = "query" |
826 }); | 843 }); |
827 _requestParameters.Add("optionalWithValue", new MockParameter | 844 RequestParameters.Add("optionalWithValue", new Parameter |
828 { | 845 { |
829 Name = "optionalWithValue", | 846 Name = "optionalWithValue", |
830 IsRequired = false, | 847 IsRequired = false, |
831 ParameterType = "query", | 848 ParameterType = "query", |
832 DefaultValue = "DoesNotDisplay" | 849 DefaultValue = "DoesNotDisplay" |
833 }); | 850 }); |
834 _requestParameters.Add("optionalWithValue2", new MockParameter | 851 RequestParameters.Add("optionalWithValue2", new Parameter |
835 { | 852 { |
836 Name = "optionalWithValue", | 853 Name = "optionalWithValue", |
837 IsRequired = false, | 854 IsRequired = false, |
838 ParameterType = "query", | 855 ParameterType = "query", |
839 DefaultValue = "DoesNotDisplay" | 856 DefaultValue = "DoesNotDisplay" |
840 }); | 857 }); |
841 _requestParameters.Add("optionalWithNull", new MockParameter | 858 RequestParameters.Add("optionalWithNull", new Parameter |
842 { | 859 { |
843 Name = "optionalWithNull", | 860 Name = "optionalWithNull", |
844 IsRequired = false, | 861 IsRequired = false, |
845 ParameterType = "query", | 862 ParameterType = "query", |
846 DefaultValue = "c" | 863 DefaultValue = "c" |
847 }); | 864 }); |
848 _requestParameters.Add("optionalEmpty", new MockParameter | 865 RequestParameters.Add("optionalEmpty", new Parameter |
849 { | 866 { |
850 Name = "optionalEmpty", | 867 Name = "optionalEmpty", |
851 IsRequired = false, | 868 IsRequired = false, |
852 ParameterType = "query", | 869 ParameterType = "query", |
853 DefaultValue = "d" | 870 DefaultValue = "d" |
854 }); | 871 }); |
855 _requestParameters.Add("optionalNotPressent", new MockParameter | 872 RequestParameters.Add("optionalNotPressent", new Parameter |
856 { | 873 { |
857 Name = "optionalNotPressent", | 874 Name = "optionalNotPressent", |
858 IsRequired = false, | 875 IsRequired = false, |
859 ParameterType = "query", | 876 ParameterType = "query", |
860 DefaultValue = "DoesNotDisplay" | 877 DefaultValue = "DoesNotDisplay" |
861 }); | 878 }); |
862 } | 879 } |
863 } | 880 } |
864 | 881 |
865 /// <summary> Tests build request with query parameters. </summary> | 882 /// <summary> Tests build request with query parameters. </summary> |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
912 public int TestParameterA { get; set; } | 929 public int TestParameterA { get; set; } |
913 | 930 |
914 [RequestParameter("path2", RequestParameterType.Path)] | 931 [RequestParameter("path2", RequestParameterType.Path)] |
915 public int TestParameterB { get; set; } | 932 public int TestParameterB { get; set; } |
916 | 933 |
917 public int TestParameterC { get; set; } | 934 public int TestParameterC { get; set; } |
918 | 935 |
919 public ClientServiceRequestWithPathParameters(IClientService service , string method, object body) | 936 public ClientServiceRequestWithPathParameters(IClientService service , string method, object body) |
920 : base(service, method, body) | 937 : base(service, method, body) |
921 { | 938 { |
922 _requestParameters = new Dictionary<string, IParameter>(); | 939 RequestParameters.Add("path1", new Parameter |
923 _requestParameters.Add("path1", new MockParameter | |
924 { | 940 { |
925 Name = "path1", | 941 Name = "path1", |
926 ParameterType = "path" | 942 ParameterType = "path" |
927 }); | 943 }); |
928 _requestParameters.Add("path2", new MockParameter | 944 RequestParameters.Add("path2", new Parameter |
929 { | 945 { |
930 Name = "path2", | 946 Name = "path2", |
931 ParameterType = "path", | 947 ParameterType = "path", |
932 }); | 948 }); |
933 } | 949 } |
934 | 950 |
935 public override string RestPath | 951 public override string RestPath |
936 { | 952 { |
937 get { return "restPath/{path1}/something/{path2}"; } | 953 get { return "restPath/{path1}/something/{path2}"; } |
938 } | 954 } |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1097 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Post)); | 1113 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Post)); |
1098 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Patch)); | 1114 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Patch)); |
1099 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Put)); | 1115 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Put)); |
1100 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Delete)); | 1116 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Delete)); |
1101 Assert.AreEqual(ETagAction.Ignore, ClientServiceRequest<object>.GetD efaultETagAction("INVALID")); | 1117 Assert.AreEqual(ETagAction.Ignore, ClientServiceRequest<object>.GetD efaultETagAction("INVALID")); |
1102 } | 1118 } |
1103 | 1119 |
1104 #endregion | 1120 #endregion |
1105 } | 1121 } |
1106 } | 1122 } |
LEFT | RIGHT |