Skip to content

Commit c37a85d

Browse files
authored
Merge pull request #352 from animir/test-zero-points
Test all limiters don't allow to consume if points value is zero
2 parents 7134440 + f91845f commit c37a85d

17 files changed

+327
-0
lines changed

test/RateLimiterDrizzle/Postgres/RateLimiterDrizzleNonAtomicPostgres.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ describe('RateLimiterDrizzleNonAtomic Postgres with fixed window', function Rate
5050
}
5151
});
5252

53+
it('does not allow to consume if points is zero', async () => {
54+
const testKey = 'consumezero';
55+
const rateLimiter = new RateLimiterDrizzleNonAtomic({
56+
storeClient: db,
57+
schema: rateLimiterFlexible,
58+
points: 0,
59+
duration: 5,
60+
});
61+
62+
try {
63+
await rateLimiter.consume(testKey, 1);
64+
expect.fail('should have thrown');
65+
} catch (rejRes) {
66+
expect(rejRes.msBeforeNext >= 0).to.equal(true);
67+
}
68+
});
69+
5370
it('execute evenly over duration', async () => {
5471
const testKey = 'consumeEvenly';
5572
const rateLimiter = new RateLimiterDrizzleNonAtomic({

test/RateLimiterDrizzle/Postgres/RateLimiterDrizzlePostgres.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ describe('RateLimiterDrizzle Postgres with fixed window', function RateLimiterDr
5050
}
5151
});
5252

53+
it('does not allow to consume if points is zero', async () => {
54+
const testKey = 'consumezero';
55+
const rateLimiter = new RateLimiterDrizzle({
56+
storeClient: db,
57+
schema: rateLimiterFlexible,
58+
points: 0,
59+
duration: 5,
60+
});
61+
62+
try {
63+
await rateLimiter.consume(testKey, 1);
64+
expect.fail('should have thrown');
65+
} catch (rejRes) {
66+
expect(rejRes.msBeforeNext >= 0).to.equal(true);
67+
}
68+
});
69+
5370
it('execute evenly over duration', async () => {
5471
const testKey = 'consumeEvenly';
5572
const rateLimiter = new RateLimiterDrizzle({

test/RateLimiterDynamo.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,27 @@ describe('RateLimiterDynamo with fixed window', function RateLimiterDynamoTest()
160160
});
161161
});
162162

163+
it('does not allow to consume if points is zero', (done) => {
164+
const testKey = 'consumezero';
165+
166+
const rateLimiter = new RateLimiterDynamo({
167+
storeClient: dynamoClient,
168+
points: 0,
169+
duration: 5
170+
},
171+
() => {
172+
rateLimiter.consume(testKey, 1)
173+
.then(() => {})
174+
.catch((rejRes) => {
175+
expect(rejRes.msBeforeNext >= 0).to.equal(true);
176+
done();
177+
})
178+
.catch((err) => {
179+
done(err);
180+
});
181+
});
182+
});
183+
163184
it('blocks key for block duration when consumed more than points', (done) => {
164185
const testKey = 'block';
165186

test/RateLimiterEtcd.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,25 @@ describe('RateLimiterEtcd', function RateLimiterEtcdTest() {
101101
});
102102
});
103103

104+
it('does not allow to consume if points is zero', (done) => {
105+
const rateLimiter = new RateLimiterEtcd({
106+
storeClient: etcdClient,
107+
points: 0,
108+
duration: 5,
109+
});
110+
111+
rateLimiter
112+
.consume(testKey, 1)
113+
.then(() => {})
114+
.catch((rejRes) => {
115+
expect(rejRes.msBeforeNext >= 0).to.equal(true);
116+
done();
117+
})
118+
.catch((err) => {
119+
done(err);
120+
});
121+
});
122+
104123
it('execute evenly over duration', (done) => {
105124
const rateLimiter = new RateLimiterEtcd({
106125
storeClient: etcdClient,

test/RateLimiterEtcdNonAtomic.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ describe('RateLimiterEtcdNonAtomic', function RateLimiterEtcdNonAtomicTest() {
7575
});
7676
});
7777

78+
it('does not allow to consume if points is zero', (done) => {
79+
const rateLimiter = new RateLimiterEtcdNonAtomic({
80+
storeClient: etcdClient,
81+
points: 0,
82+
duration: 5,
83+
});
84+
85+
rateLimiter
86+
.consume(testKey, 1)
87+
.then(() => {})
88+
.catch((rejRes) => {
89+
expect(rejRes.msBeforeNext >= 0).to.equal(true);
90+
done();
91+
})
92+
.catch((err) => {
93+
done(err);
94+
});
95+
});
96+
7897
it('execute evenly over duration', (done) => {
7998
const rateLimiter = new RateLimiterEtcdNonAtomic({
8099
storeClient: etcdClient,

test/RateLimiterMemcache.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ describe('RateLimiterMemcache', function RateLimiterMemcacheTest() {
5757
});
5858
});
5959

60+
it('does not allow to consume if points is zero', (done) => {
61+
const testKey = 'consumezero';
62+
const rateLimiter = new RateLimiterMemcache({
63+
storeClient: memcacheMockClient,
64+
points: 0,
65+
duration: 5,
66+
});
67+
rateLimiter
68+
.consume(testKey, 1)
69+
.then(() => {})
70+
.catch((rejRes) => {
71+
expect(rejRes.msBeforeNext >= 0).to.equal(true);
72+
done();
73+
})
74+
.catch((err) => {
75+
done(err);
76+
});
77+
});
78+
6079
it('execute evenly over duration', (done) => {
6180
const testKey = 'consumeEvenly';
6281
const rateLimiter = new RateLimiterMemcache({

test/RateLimiterMemory.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,18 @@ describe('RateLimiterMemory with fixed window', function RateLimiterMemoryTest()
414414
done(err);
415415
});
416416
});
417+
418+
it('does not allow to consume if points is zero', (done) => {
419+
const testKey = 'consumezero';
420+
const rateLimiterMemory = new RateLimiterMemory({ points: 0, duration: 5 });
421+
rateLimiterMemory.consume(testKey, 1)
422+
.then(() => {})
423+
.catch((rejRes) => {
424+
expect(rejRes.msBeforeNext >= 0).to.equal(true);
425+
done();
426+
})
427+
.catch((err) => {
428+
done(err);
429+
});
430+
});
417431
});

test/RateLimiterMongo.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,30 @@ describe('RateLimiterMongo with fixed window', function RateLimiterMongoTest() {
100100
});
101101
});
102102

103+
it('does not allow to consume if points is zero', (done) => {
104+
const testKey = 'consumezero';
105+
sinon.stub(mongoCollection, 'findOneAndUpdate').callsFake(() => {
106+
const res = {
107+
value: {
108+
points: 1,
109+
expire: 5000,
110+
},
111+
};
112+
return Promise.resolve(res);
113+
});
114+
115+
const rateLimiter = new RateLimiterMongo({ storeClient: mongoClient, points: 0, duration: 5 });
116+
rateLimiter.consume(testKey, 1)
117+
.then(() => {})
118+
.catch((rejRes) => {
119+
expect(rejRes.msBeforeNext >= 0).to.equal(true);
120+
done();
121+
})
122+
.catch((err) => {
123+
done(err);
124+
});
125+
});
126+
103127
it('makes penalty', (done) => {
104128
const testKey = 'penalty1';
105129
sinon.stub(mongoCollection, 'findOneAndUpdate').callsFake(() => {

test/RateLimiterMySQL.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,35 @@ describe('RateLimiterMySQL with fixed window', function RateLimiterMySQLTest() {
156156
});
157157
});
158158

159+
it('does not allow to consume if points is zero', (done) => {
160+
const testKey = 'consumezero';
161+
162+
const rateLimiter = new RateLimiterMySQL({
163+
storeClient: mysqlClient, storeType: 'connection', points: 0, duration: 5,
164+
}, () => {
165+
mysqlClientStub.restore();
166+
sinon.stub(mysqlClient, 'query').callsFake((q, data, cb) => {
167+
const res = [
168+
{ points: 1, expire: 5000 },
169+
];
170+
if (Array.isArray(data)) {
171+
cb(null, res);
172+
} else {
173+
data(null);
174+
}
175+
});
176+
rateLimiter.consume(testKey, 1)
177+
.then(() => {})
178+
.catch((rejRes) => {
179+
expect(rejRes.msBeforeNext >= 0).to.equal(true);
180+
done();
181+
})
182+
.catch((err) => {
183+
done(err);
184+
});
185+
});
186+
});
187+
159188
it('blocks key for block duration when consumed more than points', (done) => {
160189
const testKey = 'block';
161190

test/RateLimiterPostgres.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,28 @@ describe('RateLimiterPostgres with fixed window', function RateLimiterPostgresTe
103103
});
104104
});
105105

106+
it('does not allow to consume if points is zero', (done) => {
107+
const testKey = 'consumezero';
108+
109+
const rateLimiter = new RateLimiterPostgres({
110+
storeClient: pgClient, storeType: 'client', points: 0, duration: 5,
111+
}, () => {
112+
pgClientStub.restore();
113+
pgClientStub = sinon.stub(pgClient, 'query').resolves({
114+
rows: [{ points: 1, expire: 5000 }],
115+
});
116+
rateLimiter.consume(testKey, 1)
117+
.then(() => {})
118+
.catch((rejRes) => {
119+
expect(rejRes.msBeforeNext >= 0).to.equal(true);
120+
done();
121+
})
122+
.catch((err) => {
123+
done(err);
124+
});
125+
});
126+
});
127+
106128
it('blocks key for block duration when consumed more than points', (done) => {
107129
const testKey = 'block';
108130

0 commit comments

Comments
 (0)