This repository was archived by the owner on Jan 24, 2024. It is now read-only.
forked from soffes/RateLimit
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRateLimitTests.swift
More file actions
151 lines (123 loc) · 3.75 KB
/
Copy pathRateLimitTests.swift
File metadata and controls
151 lines (123 loc) · 3.75 KB
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
//
// RateLimitTests.swift
// RateLimitTests
//
// Created by Sam Soffes on 7/15/13.
// Copyright © 2013-2015 Sam Soffes. All rights reserved.
//
import XCTest
import RateLimit
final class RateLimitTests: XCTestCase {
func testLimit() {
let name = "testLimit"
// It should get excuted first
let expectation1 = expectation(description: "Execute 1")
var reported = RateLimit.execute(name: name, limit: 2) {
expectation1.fulfill()
}
XCTAssertTrue(reported)
waitForExpectations(timeout: 0, handler: nil)
// Not right away after
reported = RateLimit.execute(name: name, limit: 1) {
XCTFail("This shouldn't have run.")
}
XCTAssertFalse(reported)
// Sleep for a second
sleep(2)
// Now it should get executed
let expectation2 = expectation(description: "Execute 2")
reported = RateLimit.execute(name: name, limit: 2) {
expectation2.fulfill()
}
XCTAssertTrue(reported)
waitForExpectations(timeout: 0, handler: nil)
}
func testLimitNotResetWhenNotExecuted() {
let name = "testLimitNotResetWhenNotExecuted"
// It should get excuted first
let expectation1 = expectation(description: "Execute 1")
var reported = RateLimit.execute(name: name, limit: 2) {
expectation1.fulfill()
}
XCTAssertTrue(reported)
waitForExpectations(timeout: 0, handler: nil)
// Sleep for a second
sleep(1)
// Not right away after
reported = RateLimit.execute(name: name, limit: 2) {
XCTFail("This shouldn't have run.")
}
XCTAssertFalse(reported)
// Sleep for a second
sleep(1)
// Now it should get executed
let expectation2 = expectation(description: "Execute 2")
reported = RateLimit.execute(name: name, limit: 2) {
expectation2.fulfill()
}
XCTAssertTrue(reported)
waitForExpectations(timeout: 0, handler: nil)
}
func testResetting() {
let name = "testResetting"
// It should get excuted first
let expectation1 = expectation(description: "Execute 1")
let reported1 = RateLimit.execute(name: name, limit: 1) {
expectation1.fulfill()
}
XCTAssertTrue(reported1)
waitForExpectations(timeout: 0, handler: nil)
// Not right away after
let reported2 = RateLimit.execute(name: name, limit: 1) {
XCTFail("This shouldn't have run.")
}
XCTAssertFalse(reported2)
// Reset limit
RateLimit.resetLimitForName(name)
// Now it should get executed
let expectation2 = expectation(description: "Execute 2")
let reported3 = RateLimit.execute(name: name, limit: 1) {
expectation2.fulfill()
}
XCTAssertTrue(reported3)
waitForExpectations(timeout: 0, handler: nil)
}
func testResettingAll() {
// It should get excuted first
let name1 = "TestResettingAll1"
let expecation1 = expectation(description: "Execute 1")
let reported1 = RateLimit.execute(name: name1, limit: 1) {
expecation1.fulfill()
}
XCTAssertTrue(reported1)
let name2 = "TestResettingAll2"
let expecation2 = expectation(description: "Execute 2")
let reported2 = RateLimit.execute(name: name2, limit: 1) {
expecation2.fulfill()
}
XCTAssertTrue(reported2)
// Not right away after
let reported3 = RateLimit.execute(name: name1, limit: 1) {
XCTFail("This shouldn't have run.")
}
XCTAssertFalse(reported3)
let reported4 = RateLimit.execute(name: name2, limit: 1) {
XCTFail("This shouldn't have run.")
}
XCTAssertFalse(reported4)
// Reset limit
RateLimit.resetAllLimits()
// Now it should get executed
let expectation3 = expectation(description: "Execute 3")
let reported5 = RateLimit.execute(name: name1, limit: 1) {
expectation3.fulfill()
}
XCTAssertTrue(reported5)
let expectation4 = expectation(description: "Execute 4")
let reported6 = RateLimit.execute(name: name2, limit: 1) {
expectation4.fulfill()
}
XCTAssertTrue(reported6)
waitForExpectations(timeout: 1, handler: nil)
}
}