LEFT | RIGHT |
1 /* | 1 /* |
2 Copyright 2013 Google Inc | 2 Copyright 2013 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 |
11 distributed under the License is distributed on an "AS IS" BASIS, | 11 distributed under the License is distributed on an "AS IS" BASIS, |
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 See the License for the specific language governing permissions and | 13 See the License for the specific language governing permissions and |
14 limitations under the License. | 14 limitations under the License. |
15 */ | 15 */ |
16 | 16 |
17 using System; | 17 using System; |
18 using System.Collections.Generic; | 18 using System.Collections.Generic; |
19 using System.Linq; | 19 using System.Linq; |
20 using System.Text; | 20 using System.Text; |
21 | 21 |
22 using NUnit.Framework; | 22 using NUnit.Framework; |
23 | 23 |
24 using Google.Apis.Util; | 24 using Google.Apis.Util; |
25 | 25 |
26 namespace Google.Apis.Tests.Apis.Utils | 26 namespace Google.Apis.Tests.Apis.Utils |
27 { | 27 { |
28 /// <summary> Tests for <see cref="Google.Apis.Util.ExponentialBackOff"/>. <
/summary> | 28 /// <summary>Tests for <see cref="Google.Apis.Util.ExponentialBackOff"/>.</s
ummary> |
29 [TestFixture] | 29 [TestFixture] |
30 public class ExponentialBackOffTest | 30 public class ExponentialBackOffTest |
31 { | 31 { |
32 /// <summary> Tests setting invalid value as <c>currentRetry</c> paramet
er. </summary> | 32 /// <summary>Tests setting invalid value as <c>currentRetry</c> paramete
r.</summary> |
33 [Test] | 33 [Test] |
34 public void GetNextBackOff_InvalidValue() | 34 public void GetNextBackOff_InvalidValue() |
35 { | 35 { |
36 ExponentialBackOff backOff = new ExponentialBackOff(); | 36 ExponentialBackOff backOff = new ExponentialBackOff(); |
37 try | 37 try |
38 { | 38 { |
39 backOff.GetNextBackOff(0); | 39 backOff.GetNextBackOff(0); |
40 Assert.Fail(); | 40 Assert.Fail(); |
41 } | 41 } |
42 catch (ArgumentOutOfRangeException) { } | 42 catch (ArgumentOutOfRangeException) { } |
43 | 43 |
44 try | 44 try |
45 { | 45 { |
46 backOff.GetNextBackOff(-2); | 46 backOff.GetNextBackOff(-2); |
47 Assert.Fail(); | 47 Assert.Fail(); |
48 } | 48 } |
49 catch (ArgumentOutOfRangeException) { } | 49 catch (ArgumentOutOfRangeException) { } |
50 } | 50 } |
51 | 51 |
52 /// <summary> Tests constructor with invalid time span object (less then
0 or greater than 1sec). </summary> | 52 /// <summary>Tests constructor with invalid time span object (less then
0 or greater than 1sec).</summary> |
53 [Test] | 53 [Test] |
54 public void Constructor_InvalidValue() | 54 public void Constructor_InvalidValue() |
55 { | 55 { |
56 // invalid delta | 56 // Invalid delta. |
57 SubtestConstructor_InvalidValue(TimeSpan.FromMilliseconds(-1)); | 57 SubtestConstructor_InvalidValue(TimeSpan.FromMilliseconds(-1)); |
58 SubtestConstructor_InvalidValue(TimeSpan.FromDays(-1)); | 58 SubtestConstructor_InvalidValue(TimeSpan.FromDays(-1)); |
59 SubtestConstructor_InvalidValue(TimeSpan.FromMilliseconds(1001)); | 59 SubtestConstructor_InvalidValue(TimeSpan.FromMilliseconds(1001)); |
60 // invalid max | 60 // Invalid max. |
61 SubtestConstructor_InvalidValue(TimeSpan.FromMilliseconds(500), -1); | 61 SubtestConstructor_InvalidValue(TimeSpan.FromMilliseconds(500), -1); |
62 SubtestConstructor_InvalidValue(TimeSpan.FromMilliseconds(500), 21); | 62 SubtestConstructor_InvalidValue(TimeSpan.FromMilliseconds(500), 21); |
63 | 63 |
64 } | 64 } |
65 | 65 |
66 /// <summary> A helper subtest to test invalid value given to the constr
uctor. </summary> | 66 /// <summary>A helper subtest to test invalid value given to the constru
ctor.</summary> |
67 private void SubtestConstructor_InvalidValue(TimeSpan ts, int max = 10) | 67 private void SubtestConstructor_InvalidValue(TimeSpan ts, int max = 10) |
68 { | 68 { |
69 try | 69 try |
70 { | 70 { |
71 ExponentialBackOff backOff = new ExponentialBackOff(ts, max); | 71 ExponentialBackOff backOff = new ExponentialBackOff(ts, max); |
72 Assert.Fail(); | 72 Assert.Fail(); |
73 } | 73 } |
74 catch (ArgumentOutOfRangeException) { } | 74 catch (ArgumentOutOfRangeException) { } |
75 } | 75 } |
76 | 76 |
77 /// <summary> Tests next back-off time span maximum, minimum and average
values for tries 1 to 15. </summary> | 77 /// <summary>Tests next back-off time span maximum, minimum and average
values for tries 1 to 15.</summary> |
78 [Test] | 78 [Test] |
79 public void GetNextBackOff_Retry1To15() | 79 public void GetNextBackOff_Retry1To15() |
80 { | 80 { |
81 foreach (int i in Enumerable.Range(1, 10)) | 81 foreach (int i in Enumerable.Range(1, 10)) |
82 { | 82 { |
83 SubtestGetNextBackOff(i); | 83 SubtestGetNextBackOff(i); |
84 SubtestGetNextBackOff(i, TimeSpan.FromMilliseconds(20)); | 84 SubtestGetNextBackOff(i, TimeSpan.FromMilliseconds(20)); |
85 SubtestGetNextBackOff(i, TimeSpan.FromMilliseconds(0), 0); | 85 SubtestGetNextBackOff(i, TimeSpan.FromMilliseconds(0), 0); |
86 } | 86 } |
87 } | 87 } |
88 | 88 |
89 /// <summary> Test helper for testing retrying using exponential back-of
f.</summary> | 89 /// <summary>Test helper for testing retrying using exponential back-off
.</summary> |
90 /// <param name="retry">Index of current retry.</param> | 90 /// <param name="retry">Index of current retry.</param> |
91 /// <param name="delta">The delta the exponential back-off uses.· | 91 /// <param name="delta">The delta the exponential back-off uses.· |
92 /// <seealso cref="ExponentialBackOff.DeltaBackOff"/> for more details. | 92 /// <seealso cref="ExponentialBackOff.DeltaBackOff"/> for more details. |
93 /// </param> | 93 /// </param> |
94 /// <param name="epsilon">Used for checking the average result of the in
put retry [In milliseconds].</param> | 94 /// <param name="epsilon">Used for checking the average result of the in
put retry [In milliseconds].</param> |
95 private void SubtestGetNextBackOff(int retry, Nullable<TimeSpan> delta =
null, int epsilon = 20) | 95 private void SubtestGetNextBackOff(int retry, Nullable<TimeSpan> delta =
null, int epsilon = 20) |
96 { | 96 { |
97 int expectedMiliis = (int)Math.Pow(2, (retry - 1)) * 1000; | 97 int expectedMiliis = (int)Math.Pow(2, (retry - 1)) * 1000; |
98 ExponentialBackOff backOff = delta.HasValue ? | 98 ExponentialBackOff backOff = delta.HasValue ? |
99 new ExponentialBackOff(delta.Value) : new ExponentialBackOff(); | 99 new ExponentialBackOff(delta.Value) : new ExponentialBackOff(); |
100 | 100 |
101 TimeSpan min = TimeSpan.FromMilliseconds(expectedMiliis - backOff.De
ltaBackOff.TotalMilliseconds); | 101 TimeSpan min = TimeSpan.FromMilliseconds(expectedMiliis - backOff.De
ltaBackOff.TotalMilliseconds); |
102 TimeSpan max = TimeSpan.FromMilliseconds(expectedMiliis + backOff.De
ltaBackOff.TotalMilliseconds); | 102 TimeSpan max = TimeSpan.FromMilliseconds(expectedMiliis + backOff.De
ltaBackOff.TotalMilliseconds); |
103 long total = 0; | 103 long total = 0; |
104 long repeat = 1000; | 104 long repeat = 1000; |
105 for (int i = 0; i < repeat; ++i) | 105 for (int i = 0; i < repeat; ++i) |
106 { | 106 { |
107 var ts = backOff.GetNextBackOff(retry); | 107 var ts = backOff.GetNextBackOff(retry); |
108 Assert.That(ts, Is.InRange(min, max)); | 108 Assert.That(ts, Is.InRange(min, max)); |
109 total += (int)ts.TotalMilliseconds; | 109 total += (int)ts.TotalMilliseconds; |
110 } | 110 } |
111 | 111 |
112 var avarage = (int)(total / repeat); | 112 var avarage = (int)(total / repeat); |
113 Assert.That(avarage, Is.InRange(expectedMiliis - epsilon, expectedMi
liis + epsilon)); | 113 Assert.That(avarage, Is.InRange(expectedMiliis - epsilon, expectedMi
liis + epsilon)); |
114 } | 114 } |
115 | 115 |
116 /// <summary> Tests next back-off time span with specific maximum of ret
ries. </summary> | 116 /// <summary>Tests next back-off time span with specific maximum of retr
ies.</summary> |
117 [Test] | 117 [Test] |
118 public void GetNextBackOff_MaxNumRetries() | 118 public void GetNextBackOff_MaxNumRetries() |
119 { | 119 { |
120 SubtestGetNextBackOff_MaxNumRetries(1); | 120 SubtestGetNextBackOff_MaxNumRetries(1); |
121 SubtestGetNextBackOff_MaxNumRetries(10); | 121 SubtestGetNextBackOff_MaxNumRetries(10); |
122 SubtestGetNextBackOff_MaxNumRetries(11); | 122 SubtestGetNextBackOff_MaxNumRetries(11); |
123 } | 123 } |
124 | 124 |
125 /// <summary> A helper test for testing the <c>GetNextBackOff</c> logic.
</summary> | 125 /// <summary>A helper test for testing the <c>GetNextBackOff</c> logic.<
/summary> |
126 private void SubtestGetNextBackOff_MaxNumRetries(int max) | 126 private void SubtestGetNextBackOff_MaxNumRetries(int max) |
127 { | 127 { |
128 ExponentialBackOff backOff = new ExponentialBackOff(TimeSpan.Zero, m
ax); | 128 ExponentialBackOff backOff = new ExponentialBackOff(TimeSpan.Zero, m
ax); |
129 | 129 |
130 for (int i = 1; i <= 10; ++i) | 130 for (int i = 1; i <= 10; ++i) |
131 { | 131 { |
132 if (i <= max) | 132 if (i <= max) |
133 { | 133 { |
134 Assert.AreNotEqual(TimeSpan.MinValue, backOff.GetNextBackOff
(i)); | 134 Assert.AreNotEqual(TimeSpan.MinValue, backOff.GetNextBackOff
(i)); |
135 } | 135 } |
136 else | 136 else |
137 { | 137 { |
138 Assert.AreEqual(TimeSpan.MinValue, backOff.GetNextBackOff(i)
); | 138 Assert.AreEqual(TimeSpan.MinValue, backOff.GetNextBackOff(i)
); |
139 } | 139 } |
140 } | 140 } |
141 } | 141 } |
142 } | 142 } |
143 } | 143 } |
LEFT | RIGHT |