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
|
namespace slskd.Tests.Unit.Common
{
using System;
using System.Threading.Tasks;
using AutoFixture.Xunit2;
using Xunit;
public class TokenBucketTests
{
[Trait("Category", "Instantiation")]
[Fact(DisplayName = "Throws ArgumentOutOfRangeException given 0 count")]
public void Throws_ArgumentOutOfRangeException_Given_0_Count()
{
var ex = Record.Exception(() => new TokenBucket(0, 1000));
Assert.NotNull(ex);
Assert.IsType<ArgumentOutOfRangeException>(ex);
Assert.Equal("capacity", ((ArgumentOutOfRangeException)ex).ParamName);
}
[Trait("Category", "Instantiation")]
[Fact(DisplayName = "Throws ArgumentOutOfRangeException given negative count")]
public void Throws_ArgumentOutOfRangeException_Given_Negative_Count()
{
var ex = Record.Exception(() => new TokenBucket(-1, 1000));
Assert.NotNull(ex);
Assert.IsType<ArgumentOutOfRangeException>(ex);
Assert.Equal("capacity", ((ArgumentOutOfRangeException)ex).ParamName);
}
[Trait("Category", "Instantiation")]
[Fact(DisplayName = "Throws ArgumentOutOfRangeException given 0 interval")]
public void Throws_ArgumentOutOfRangeException_Given_0_Interval()
{
var ex = Record.Exception(() => new TokenBucket(1000, 0));
Assert.NotNull(ex);
Assert.IsType<ArgumentOutOfRangeException>(ex);
Assert.Equal("interval", ((ArgumentOutOfRangeException)ex).ParamName);
}
[Trait("Category", "Instantiation")]
[Fact(DisplayName = "Throws ArgumentOutOfRangeException given negative interval")]
public void Throws_ArgumentOutOfRangeException_Given_Negative_Interval()
{
var ex = Record.Exception(() => new TokenBucket(1000, -1));
Assert.NotNull(ex);
Assert.IsType<ArgumentOutOfRangeException>(ex);
Assert.Equal("interval", ((ArgumentOutOfRangeException)ex).ParamName);
}
[Trait("Category", "Instantiation")]
[Theory(DisplayName = "Sets properties"), AutoData]
public void Sets_Properties(int count, int interval)
{
using (var t = new TokenBucket(count, interval))
{
Assert.Equal(count, t.Capacity);
Assert.Equal(interval, t.GetProperty<System.Timers.Timer>("Clock").Interval);
Assert.Equal(count, t.GetProperty<long>("CurrentCount"));
}
}
[Trait("Category", "SetCount")]
[Fact(DisplayName = "SetCount throws ArgumentOutOfRangeException given 0 count")]
public void SetCount_Throws_ArgumentOutOfRangeException_Given_0_Count()
{
using (var t = new TokenBucket(10, 1000))
{
var ex = Record.Exception(() => t.SetCapacity(0));
Assert.NotNull(ex);
Assert.IsType<ArgumentOutOfRangeException>(ex);
Assert.Equal("capacity", ((ArgumentOutOfRangeException)ex).ParamName);
}
}
[Trait("Category", "SetCount")]
[Fact(DisplayName = "SetCount throws ArgumentOutOfRangeException given negative count")]
public void SetCount_Throws_ArgumentOutOfRangeException_Given_Negative_Count()
{
using (var t = new TokenBucket(10, 1000))
{
var ex = Record.Exception(() => t.SetCapacity(-1));
Assert.NotNull(ex);
Assert.IsType<ArgumentOutOfRangeException>(ex);
Assert.Equal("capacity", ((ArgumentOutOfRangeException)ex).ParamName);
}
}
[Trait("Category", "SetCapacity")]
[Theory(DisplayName = "SetCapacity sets capacity"), AutoData]
public void SetCapacity_Sets_Capacity(int count)
{
using (var t = new TokenBucket(10, 1000))
{
t.SetCapacity(count);
Assert.Equal(count, t.Capacity);
}
}
[Trait("Category", "GetAsync")]
[Fact(DisplayName = "GetAsync decrements count by requested count")]
public async Task GetAsync_Decrements_Count_By_Requested_Count()
{
using (var t = new TokenBucket(10, 10000))
{
await t.GetAsync(5);
Assert.Equal(5, t.GetProperty<long>("CurrentCount"));
}
}
[Trait("Category", "GetAsync")]
[Fact(DisplayName = "GetAsync returns capacity if request exceeds capacity")]
public async Task GetAsync_Returns_Capacity_If_Request_Exceeds_Capacity()
{
using (var t = new TokenBucket(10, 10000))
{
int tokens = 0;
var ex = await Record.ExceptionAsync(async () => tokens = await t.GetAsync(11));
Assert.Null(ex);
Assert.Equal(10, tokens);
}
}
[Trait("Category", "GetAsync")]
[Fact(DisplayName = "GetAsync returns available tokens if request exceeds available count")]
public async Task GetAsync_Returns_Available_Tokens_If_Request_Exceeds_Available_Count()
{
using (var t = new TokenBucket(10, 10000))
{
await t.GetAsync(6);
var count = await t.GetAsync(6);
Assert.Equal(4, count);
}
}
[Trait("Category", "GetAsync")]
[Fact(DisplayName = "GetAsync waits for reset if bucket is depleted")]
public async Task GetAsync_Waits_For_Reset_If_Bucket_Is_Depleted()
{
using (var t = new TokenBucket(1, 10))
{
await t.GetAsync(1);
await t.GetAsync(1);
await t.GetAsync(1);
Assert.True(true);
}
}
[Trait("Category", "Return")]
[Fact(DisplayName = "Return does not change count given negative")]
public async Task Return_Does_Not_Change_Count_Given_Negative()
{
using (var t = new TokenBucket(10, 1000000))
{
await t.GetAsync(5);
Assert.Equal(5, t.GetProperty<long>("CurrentCount"));
t.Return(-5);
Assert.Equal(5, t.GetProperty<long>("CurrentCount"));
}
}
[Trait("Category", "Return")]
[Fact(DisplayName = "Return adds capacity given value larger than capacity")]
public async Task Return_Adds_Capacity_Given_Value_Larger_Than_Capacity()
{
using (var t = new TokenBucket(10, 1000000))
{
await t.GetAsync(5);
Assert.Equal(5, t.GetProperty<long>("CurrentCount"));
t.Return(50);
Assert.Equal(15, t.GetProperty<long>("CurrentCount"));
}
}
[Trait("Category", "Return")]
[Fact(DisplayName = "Return adds given value")]
public async Task Return_Adds_Given_Value()
{
using (var t = new TokenBucket(10, 1000000))
{
await t.GetAsync(5);
Assert.Equal(5, t.GetProperty<long>("CurrentCount"));
t.Return(5);
Assert.Equal(10, t.GetProperty<long>("CurrentCount"));
}
}
}
}
|