forked from 3rd-Eden/memcached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemcached-incr-decr.test.js
154 lines (121 loc) · 3.77 KB
/
memcached-incr-decr.test.js
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
/**
* Test dependencies
*/
var assert = require('assert')
, common = require('./common')
, Memcached = require('../');
global.testnumbers = global.testnumbers || +(Math.random(10) * 1000000).toFixed();
/**
* Expresso test suite for all `get` related
* memcached commands
*/
describe("Memcached INCR DECR", function() {
/**
* Simple increments.. Just because.. we can :D
*/
it("simple incr", function(done) {
var memcached = new Memcached(common.servers.single)
, testnr = ++global.testnumbers
, callbacks = 0;
memcached.set("test:" + testnr, 1, 1000, function(error, ok){
++callbacks;
assert.ok(!error);
ok.should.be.true;
memcached.incr("test:" + testnr, 1, function(error, ok){
++callbacks;
assert.ok(!error);
ok.should.be.equal(2);
memcached.end(); // close connections
assert.equal(callbacks, 2);
done();
});
});
});
/**
* Simple decrement.. So we know that works as well. Nothing special here
* move on.
*/
it("simple decr", function(done) {
var memcached = new Memcached(common.servers.single)
, testnr = ++global.testnumbers
, callbacks = 0;
memcached.set("test:" + testnr, 0, 1000, function(error, ok){
++callbacks;
assert.ok(!error);
ok.should.be.true;
memcached.incr("test:" + testnr, 10, function(error, ok){
++callbacks;
assert.ok(!error);
ok.should.be.equal(10);
memcached.decr("test:" + testnr, 1, function(error, answer){
++callbacks;
assert.ok(!error);
answer.should.be.equal(9);
memcached.end(); // close connections
assert.equal(callbacks, 3);
done();
});
});
});
});
/**
* According to the spec, incr should just work fine on keys that
* have intergers.. So lets test that.
*/
it("simple increment on a large number", function(done) {
var memcached = new Memcached(common.servers.single)
, message = common.numbers(10)
, testnr = ++global.testnumbers
, callbacks = 0;
memcached.set("test:" + testnr, message, 1000, function(error, ok){
++callbacks;
assert.ok(!error);
ok.should.be.true;
memcached.incr("test:" + testnr, 1, function(error, answer){
++callbacks;
assert.ok(!error);
assert.ok(+answer === (message + 1));
memcached.end(); // close connections
assert.equal(callbacks, 2);
done();
});
});
});
/**
* decrementing on a unkonwn key should fail.
*/
it("decrement on a unknown key", function(done) {
var memcached = new Memcached(common.servers.single)
, testnr = ++global.testnumbers
, callbacks = 0;
memcached.decr("test:" + testnr, 1, function(error, ok){
++callbacks;
assert.ok(!error);
ok.should.be.false;
memcached.end(); // close connections
assert.equal(callbacks, 1);
done();
});
});
/**
* We can only increment on a integer, not on a string.
*/
it("incrementing on a non string value throws a client_error", function(done) {
var memcached = new Memcached(common.servers.single)
, testnr = ++global.testnumbers
, callbacks = 0;
memcached.set("test:" + testnr, "zing!", 0, function(error, ok){
++callbacks;
assert.ok(!error);
ok.should.be.true;
memcached.incr("test:" + testnr, 1, function(error, ok){
++callbacks;
assert.ok(error);
ok.should.be.false;
memcached.end(); // close connections;
assert.equal(callbacks, 2);
done();
});
});
});
});