-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
149 lines (107 loc) · 3.74 KB
/
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
describe('HotPath', function () {
'use strict';
var assume = require('assume')
, HotPath = require('../hotpath');
it('is exposed as function', function () {
assume(HotPath).is.a('function');
});
it('can be constructed without the new keyword', function () {
assume(HotPath()).is.instanceOf(HotPath);
});
describe('#get, #set', function () {
it('only stores data if we do not blow out of memory', function () {
var hp = new HotPath({ available: 100 });
assume(hp.set('foo', new Buffer(40))).to.equal(false);
assume(hp.get('foo')).equals(undefined);
assume(hp.set('foo', new Buffer(2))).to.equal(true);
assume(hp.get('foo')).is.a('buffer');
hp.destroy();
});
it('returns undefined when key cannot be found', function () {
var hp = new HotPath();
assume(hp.get('foo')).equals(undefined);
});
it('transforms everything in buffers', function () {
var hp = new HotPath();
hp.set('foo', 'bar');
assume(hp.get('foo')).is.a('buffer');
hp.destroy();
});
it('increments the allocated size', function () {
var hp = new HotPath()
, x = new Buffer('foobar');
assume(hp.allocated).equals(0);
hp.set('foo', x);
assume(hp.allocated).equals(x.length);
hp.destroy();
});
it('increments allocated size including key size', function () {
var hp = new HotPath({ key: true })
, x = new Buffer('foobar');
assume(hp.allocated).equals(0);
hp.set('foo', x);
assume(hp.allocated).equals(x.length + 3 + hp.prefix.length);
hp.destroy();
});
it('increments the bytesize not length of key', function () {
var hp = new HotPath({ key: true })
, x = new Buffer('foobar');
assume(hp.allocated).equals(0);
hp.set('föö', x);
assume(hp.allocated).equals(x.length + 5 + hp.prefix.length);
hp.destroy();
});
it('returns true when stored', function () {
var hp = new HotPath()
, x = new Buffer('foobar');
assume(hp.set('foo', x)).equals(true);
hp.destroy();
});
});
describe('#remove', function () {
it('returns false when not stored', function () {
var hp = new HotPath();
assume(hp.remove('foo')).is.false();
});
it('decreases the allocated size', function () {
var hp = new HotPath()
, x = new Buffer('foobar');
assume(hp.allocated).equals(0);
hp.set('foo', x);
hp.set('bar', x);
assume(hp.allocated).equals(x.length * 2);
assume(hp.remove('foo')).is.true();
assume(hp.allocated).equals(x.length);
assume(hp.remove('bar')).is.true();
assume(hp.allocated).equals(0);
});
it('includes the keysize when decreasing allocated size', function () {
var hp = new HotPath({ key: true })
, x = new Buffer('foobar');
assume(hp.allocated).equals(0);
hp.set('foo', x);
hp.set('föö', x);
assume(hp.allocated).equals((x.length * 2) + 8 + (hp.prefix.length * 2));
assume(hp.remove('foo')).is.true();
assume(hp.allocated).equals(x.length + 5 + hp.prefix.length);
assume(hp.remove('föö')).is.true();
assume(hp.allocated).equals(0);
});
});
describe('#reset', function () {
it('clears all the things', function () {
var hp = new HotPath();
hp.set('foo', new Buffer('foo'));
hp.set('bar', new Buffer('haai'));
assume(hp.storage).is.length(2);
assume(hp.allocated).to.be.above(0);
hp.reset();
assume(hp.allocated).equals(0);
assume(hp.storage).is.length(0);
});
});
describe('#ram', function () {
it('normalizes the ram to a maximum');
it('it allows more memory when we have more RAM available');
});
});