ratelimit_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // Copyright 2014 Canonical Ltd.
  2. // Licensed under the LGPLv3 with static-linking exception.
  3. // See LICENCE file for details.
  4. package ratelimit
  5. import (
  6. "math"
  7. "testing"
  8. "time"
  9. gc "gopkg.in/check.v1"
  10. )
  11. func TestPackage(t *testing.T) {
  12. gc.TestingT(t)
  13. }
  14. type rateLimitSuite struct{}
  15. var _ = gc.Suite(rateLimitSuite{})
  16. type takeReq struct {
  17. time time.Duration
  18. count int64
  19. expectWait time.Duration
  20. }
  21. var takeTests = []struct {
  22. about string
  23. fillInterval time.Duration
  24. capacity int64
  25. reqs []takeReq
  26. }{{
  27. about: "serial requests",
  28. fillInterval: 250 * time.Millisecond,
  29. capacity: 10,
  30. reqs: []takeReq{{
  31. time: 0,
  32. count: 0,
  33. expectWait: 0,
  34. }, {
  35. time: 0,
  36. count: 10,
  37. expectWait: 0,
  38. }, {
  39. time: 0,
  40. count: 1,
  41. expectWait: 250 * time.Millisecond,
  42. }, {
  43. time: 250 * time.Millisecond,
  44. count: 1,
  45. expectWait: 250 * time.Millisecond,
  46. }},
  47. }, {
  48. about: "concurrent requests",
  49. fillInterval: 250 * time.Millisecond,
  50. capacity: 10,
  51. reqs: []takeReq{{
  52. time: 0,
  53. count: 10,
  54. expectWait: 0,
  55. }, {
  56. time: 0,
  57. count: 2,
  58. expectWait: 500 * time.Millisecond,
  59. }, {
  60. time: 0,
  61. count: 2,
  62. expectWait: 1000 * time.Millisecond,
  63. }, {
  64. time: 0,
  65. count: 1,
  66. expectWait: 1250 * time.Millisecond,
  67. }},
  68. }, {
  69. about: "more than capacity",
  70. fillInterval: 1 * time.Millisecond,
  71. capacity: 10,
  72. reqs: []takeReq{{
  73. time: 0,
  74. count: 10,
  75. expectWait: 0,
  76. }, {
  77. time: 20 * time.Millisecond,
  78. count: 15,
  79. expectWait: 5 * time.Millisecond,
  80. }},
  81. }, {
  82. about: "sub-quantum time",
  83. fillInterval: 10 * time.Millisecond,
  84. capacity: 10,
  85. reqs: []takeReq{{
  86. time: 0,
  87. count: 10,
  88. expectWait: 0,
  89. }, {
  90. time: 7 * time.Millisecond,
  91. count: 1,
  92. expectWait: 3 * time.Millisecond,
  93. }, {
  94. time: 8 * time.Millisecond,
  95. count: 1,
  96. expectWait: 12 * time.Millisecond,
  97. }},
  98. }, {
  99. about: "within capacity",
  100. fillInterval: 10 * time.Millisecond,
  101. capacity: 5,
  102. reqs: []takeReq{{
  103. time: 0,
  104. count: 5,
  105. expectWait: 0,
  106. }, {
  107. time: 60 * time.Millisecond,
  108. count: 5,
  109. expectWait: 0,
  110. }, {
  111. time: 60 * time.Millisecond,
  112. count: 1,
  113. expectWait: 10 * time.Millisecond,
  114. }, {
  115. time: 80 * time.Millisecond,
  116. count: 2,
  117. expectWait: 10 * time.Millisecond,
  118. }},
  119. }}
  120. var availTests = []struct {
  121. about string
  122. capacity int64
  123. fillInterval time.Duration
  124. take int64
  125. sleep time.Duration
  126. expectCountAfterTake int64
  127. expectCountAfterSleep int64
  128. }{{
  129. about: "should fill tokens after interval",
  130. capacity: 5,
  131. fillInterval: time.Second,
  132. take: 5,
  133. sleep: time.Second,
  134. expectCountAfterTake: 0,
  135. expectCountAfterSleep: 1,
  136. }, {
  137. about: "should fill tokens plus existing count",
  138. capacity: 2,
  139. fillInterval: time.Second,
  140. take: 1,
  141. sleep: time.Second,
  142. expectCountAfterTake: 1,
  143. expectCountAfterSleep: 2,
  144. }, {
  145. about: "shouldn't fill before interval",
  146. capacity: 2,
  147. fillInterval: 2 * time.Second,
  148. take: 1,
  149. sleep: time.Second,
  150. expectCountAfterTake: 1,
  151. expectCountAfterSleep: 1,
  152. }, {
  153. about: "should fill only once after 1*interval before 2*interval",
  154. capacity: 2,
  155. fillInterval: 2 * time.Second,
  156. take: 1,
  157. sleep: 3 * time.Second,
  158. expectCountAfterTake: 1,
  159. expectCountAfterSleep: 2,
  160. }}
  161. func (rateLimitSuite) TestTake(c *gc.C) {
  162. for i, test := range takeTests {
  163. tb := NewBucket(test.fillInterval, test.capacity)
  164. for j, req := range test.reqs {
  165. d, ok := tb.take(tb.startTime.Add(req.time), req.count, infinityDuration)
  166. c.Assert(ok, gc.Equals, true)
  167. if d != req.expectWait {
  168. c.Fatalf("test %d.%d, %s, got %v want %v", i, j, test.about, d, req.expectWait)
  169. }
  170. }
  171. }
  172. }
  173. func (rateLimitSuite) TestTakeMaxDuration(c *gc.C) {
  174. for i, test := range takeTests {
  175. tb := NewBucket(test.fillInterval, test.capacity)
  176. for j, req := range test.reqs {
  177. if req.expectWait > 0 {
  178. d, ok := tb.take(tb.startTime.Add(req.time), req.count, req.expectWait-1)
  179. c.Assert(ok, gc.Equals, false)
  180. c.Assert(d, gc.Equals, time.Duration(0))
  181. }
  182. d, ok := tb.take(tb.startTime.Add(req.time), req.count, req.expectWait)
  183. c.Assert(ok, gc.Equals, true)
  184. if d != req.expectWait {
  185. c.Fatalf("test %d.%d, %s, got %v want %v", i, j, test.about, d, req.expectWait)
  186. }
  187. }
  188. }
  189. }
  190. type takeAvailableReq struct {
  191. time time.Duration
  192. count int64
  193. expect int64
  194. }
  195. var takeAvailableTests = []struct {
  196. about string
  197. fillInterval time.Duration
  198. capacity int64
  199. reqs []takeAvailableReq
  200. }{{
  201. about: "serial requests",
  202. fillInterval: 250 * time.Millisecond,
  203. capacity: 10,
  204. reqs: []takeAvailableReq{{
  205. time: 0,
  206. count: 0,
  207. expect: 0,
  208. }, {
  209. time: 0,
  210. count: 10,
  211. expect: 10,
  212. }, {
  213. time: 0,
  214. count: 1,
  215. expect: 0,
  216. }, {
  217. time: 250 * time.Millisecond,
  218. count: 1,
  219. expect: 1,
  220. }},
  221. }, {
  222. about: "concurrent requests",
  223. fillInterval: 250 * time.Millisecond,
  224. capacity: 10,
  225. reqs: []takeAvailableReq{{
  226. time: 0,
  227. count: 5,
  228. expect: 5,
  229. }, {
  230. time: 0,
  231. count: 2,
  232. expect: 2,
  233. }, {
  234. time: 0,
  235. count: 5,
  236. expect: 3,
  237. }, {
  238. time: 0,
  239. count: 1,
  240. expect: 0,
  241. }},
  242. }, {
  243. about: "more than capacity",
  244. fillInterval: 1 * time.Millisecond,
  245. capacity: 10,
  246. reqs: []takeAvailableReq{{
  247. time: 0,
  248. count: 10,
  249. expect: 10,
  250. }, {
  251. time: 20 * time.Millisecond,
  252. count: 15,
  253. expect: 10,
  254. }},
  255. }, {
  256. about: "within capacity",
  257. fillInterval: 10 * time.Millisecond,
  258. capacity: 5,
  259. reqs: []takeAvailableReq{{
  260. time: 0,
  261. count: 5,
  262. expect: 5,
  263. }, {
  264. time: 60 * time.Millisecond,
  265. count: 5,
  266. expect: 5,
  267. }, {
  268. time: 70 * time.Millisecond,
  269. count: 1,
  270. expect: 1,
  271. }},
  272. }}
  273. func (rateLimitSuite) TestTakeAvailable(c *gc.C) {
  274. for i, test := range takeAvailableTests {
  275. tb := NewBucket(test.fillInterval, test.capacity)
  276. for j, req := range test.reqs {
  277. d := tb.takeAvailable(tb.startTime.Add(req.time), req.count)
  278. if d != req.expect {
  279. c.Fatalf("test %d.%d, %s, got %v want %v", i, j, test.about, d, req.expect)
  280. }
  281. }
  282. }
  283. }
  284. func (rateLimitSuite) TestPanics(c *gc.C) {
  285. c.Assert(func() { NewBucket(0, 1) }, gc.PanicMatches, "token bucket fill interval is not > 0")
  286. c.Assert(func() { NewBucket(-2, 1) }, gc.PanicMatches, "token bucket fill interval is not > 0")
  287. c.Assert(func() { NewBucket(1, 0) }, gc.PanicMatches, "token bucket capacity is not > 0")
  288. c.Assert(func() { NewBucket(1, -2) }, gc.PanicMatches, "token bucket capacity is not > 0")
  289. }
  290. func isCloseTo(x, y, tolerance float64) bool {
  291. return math.Abs(x-y)/y < tolerance
  292. }
  293. func (rateLimitSuite) TestRate(c *gc.C) {
  294. tb := NewBucket(1, 1)
  295. if !isCloseTo(tb.Rate(), 1e9, 0.00001) {
  296. c.Fatalf("got %v want 1e9", tb.Rate())
  297. }
  298. tb = NewBucket(2*time.Second, 1)
  299. if !isCloseTo(tb.Rate(), 0.5, 0.00001) {
  300. c.Fatalf("got %v want 0.5", tb.Rate())
  301. }
  302. tb = NewBucketWithQuantum(100*time.Millisecond, 1, 5)
  303. if !isCloseTo(tb.Rate(), 50, 0.00001) {
  304. c.Fatalf("got %v want 50", tb.Rate())
  305. }
  306. }
  307. func checkRate(c *gc.C, rate float64) {
  308. tb := NewBucketWithRate(rate, 1<<62)
  309. if !isCloseTo(tb.Rate(), rate, rateMargin) {
  310. c.Fatalf("got %g want %v", tb.Rate(), rate)
  311. }
  312. d, ok := tb.take(tb.startTime, 1<<62, infinityDuration)
  313. c.Assert(ok, gc.Equals, true)
  314. c.Assert(d, gc.Equals, time.Duration(0))
  315. // Check that the actual rate is as expected by
  316. // asking for a not-quite multiple of the bucket's
  317. // quantum and checking that the wait time
  318. // correct.
  319. d, ok = tb.take(tb.startTime, tb.quantum*2-tb.quantum/2, infinityDuration)
  320. c.Assert(ok, gc.Equals, true)
  321. expectTime := 1e9 * float64(tb.quantum) * 2 / rate
  322. if !isCloseTo(float64(d), expectTime, rateMargin) {
  323. c.Fatalf("rate %g: got %g want %v", rate, float64(d), expectTime)
  324. }
  325. }
  326. func (rateLimitSuite) TestNewWithRate(c *gc.C) {
  327. for rate := float64(1); rate < 1e6; rate += 7 {
  328. checkRate(c, rate)
  329. }
  330. for _, rate := range []float64{
  331. 1024 * 1024 * 1024,
  332. 1e-5,
  333. 0.9e-5,
  334. 0.5,
  335. 0.9,
  336. 0.9e8,
  337. 3e12,
  338. 4e18,
  339. } {
  340. checkRate(c, rate)
  341. checkRate(c, rate/3)
  342. checkRate(c, rate*1.3)
  343. }
  344. }
  345. func TestAvailable(t *testing.T) {
  346. for i, tt := range availTests {
  347. tb := NewBucket(tt.fillInterval, tt.capacity)
  348. if c := tb.takeAvailable(tb.startTime, tt.take); c != tt.take {
  349. t.Fatalf("#%d: %s, take = %d, want = %d", i, tt.about, c, tt.take)
  350. }
  351. if c := tb.available(tb.startTime); c != tt.expectCountAfterTake {
  352. t.Fatalf("#%d: %s, after take, available = %d, want = %d", i, tt.about, c, tt.expectCountAfterTake)
  353. }
  354. if c := tb.available(tb.startTime.Add(tt.sleep)); c != tt.expectCountAfterSleep {
  355. t.Fatalf("#%d: %s, after some time it should fill in new tokens, available = %d, want = %d",
  356. i, tt.about, c, tt.expectCountAfterSleep)
  357. }
  358. }
  359. }
  360. func BenchmarkWait(b *testing.B) {
  361. tb := NewBucket(1, 16*1024)
  362. for i := b.N - 1; i >= 0; i-- {
  363. tb.Wait(1)
  364. }
  365. }