File: FileSystemBlobStore-test.js

package info (click to toggle)
node-v8-compile-cache 2.3.0-3%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 268 kB
  • sloc: javascript: 880; sh: 11; makefile: 2
file content (214 lines) | stat: -rw-r--r-- 7,222 bytes parent folder | download | duplicates (2)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
'use strict';

const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const rimraf = promisify(require('rimraf'));
const tap = require('tap');
const temp = require('temp');

process.env.DISABLE_V8_COMPILE_CACHE = 1;
const FileSystemBlobStore = require('..').__TEST__.FileSystemBlobStore;

temp.track();

let storageDirectory;
let blobStore;

tap.beforeEach(() => {
  storageDirectory = temp.path('filesystemblobstore');
  blobStore = new FileSystemBlobStore(storageDirectory);
});

tap.afterEach(() => {
 return rimraf(storageDirectory);
});

tap.test('is empty when the file doesn\'t exist', t => {
  t.equal(blobStore.isDirty(), false);
  t.type(blobStore.get('foo', 'invalidation-key-1'), 'undefined');
  t.type(blobStore.get('bar', 'invalidation-key-2'), 'undefined');
  t.end();
});

tap.test('allows to read and write buffers from/to memory without persisting them', t => {
  blobStore.set('foo', 'invalidation-key-1', Buffer.from('foo'));
  blobStore.set('bar', 'invalidation-key-2', Buffer.from('bar'));

  t.same(blobStore.get('foo', 'invalidation-key-1'), Buffer.from('foo'));
  t.same(blobStore.get('bar', 'invalidation-key-2'), Buffer.from('bar'));

  t.type(blobStore.get('foo', 'unexisting-key'), 'undefined');
  t.type(blobStore.get('bar', 'unexisting-key'), 'undefined');

  t.end();
});

tap.test('persists buffers when saved and retrieves them on load, giving priority to in-memory ones', t => {
  blobStore.set('foo', 'invalidation-key-1', Buffer.from('foo'));
  blobStore.set('bar', 'invalidation-key-2', Buffer.from('bar'));
  blobStore.save();

  blobStore = new FileSystemBlobStore(storageDirectory);

  t.same(blobStore.get('foo', 'invalidation-key-1'), Buffer.from('foo'));
  t.same(blobStore.get('bar', 'invalidation-key-2'), Buffer.from('bar'));
  t.type(blobStore.get('foo', 'unexisting-key'), 'undefined');
  t.type(blobStore.get('bar', 'unexisting-key'), 'undefined');

  blobStore.set('foo', 'new-key', Buffer.from('changed'));

  t.same(blobStore.get('foo', 'new-key'), Buffer.from('changed'));
  t.type(blobStore.get('foo', 'invalidation-key-1'), 'undefined');

  t.end();
});

tap.test('persists both in-memory and previously stored buffers when saved', t => {
  blobStore.set('foo', 'invalidation-key-1', Buffer.from('foo'));
  blobStore.set('bar', 'invalidation-key-2', Buffer.from('bar'));
  blobStore.save();

  blobStore = new FileSystemBlobStore(storageDirectory);

  blobStore.set('bar', 'invalidation-key-3', Buffer.from('changed'));
  blobStore.set('qux', 'invalidation-key-4', Buffer.from('qux'));
  blobStore.save();

  blobStore = new FileSystemBlobStore(storageDirectory);

  t.same(blobStore.get('foo', 'invalidation-key-1'), Buffer.from('foo'));
  t.same(blobStore.get('bar', 'invalidation-key-3'), Buffer.from('changed'));
  t.same(blobStore.get('qux', 'invalidation-key-4'), Buffer.from('qux'));
  t.type(blobStore.get('foo', 'unexisting-key'), 'undefined');
  t.type(blobStore.get('bar', 'invalidation-key-2'), 'undefined');
  t.type(blobStore.get('qux', 'unexisting-key'), 'undefined');

  t.end();
});

tap.test('allows to delete keys from both memory and stored buffers', t => {
  blobStore.set('a', 'invalidation-key-1', Buffer.from('a'));
  blobStore.set('b', 'invalidation-key-2', Buffer.from('b'));
  blobStore.save();

  blobStore = new FileSystemBlobStore(storageDirectory);

  blobStore.set('b', 'invalidation-key-3', Buffer.from('b'));
  blobStore.set('c', 'invalidation-key-4', Buffer.from('c'));
  blobStore.delete('b');
  blobStore.delete('c');
  blobStore.save();

  blobStore = new FileSystemBlobStore(storageDirectory);

  t.same(blobStore.get('a', 'invalidation-key-1'), Buffer.from('a'));
  t.type(blobStore.get('b', 'invalidation-key-2'), 'undefined');
  t.type(blobStore.get('b', 'invalidation-key-3'), 'undefined');
  t.type(blobStore.get('c', 'invalidation-key-4'), 'undefined');

  t.end();
});

tap.test('ignores errors when loading an invalid blob store', t => {
  blobStore.set('a', 'invalidation-key-1', Buffer.from('a'));
  blobStore.set('b', 'invalidation-key-2', Buffer.from('b'));
  blobStore.save();

  // Simulate corruption
  fs.writeFileSync(path.join(storageDirectory, 'MAP'), Buffer.from([0]));
  fs.writeFileSync(path.join(storageDirectory, 'BLOB'), Buffer.from([0]));

  blobStore = new FileSystemBlobStore(storageDirectory);

  t.type(blobStore.get('a', 'invalidation-key-1'), 'undefined');
  t.type(blobStore.get('b', 'invalidation-key-2'), 'undefined');

  blobStore.set('a', 'invalidation-key-1', Buffer.from('x'));
  blobStore.set('b', 'invalidation-key-2', Buffer.from('y'));
  blobStore.save();

  blobStore = new FileSystemBlobStore(storageDirectory);

  t.same(blobStore.get('a', 'invalidation-key-1'), Buffer.from('x'));
  t.same(blobStore.get('b', 'invalidation-key-2'), Buffer.from('y'));

  t.end();
});

tap.test('object hash collision', t => {
  t.type(blobStore.get('constructor', 'invalidation-key-1'), 'undefined');
  blobStore.delete('constructor');
  t.type(blobStore.get('constructor', 'invalidation-key-1'), 'undefined');

  blobStore.set('constructor', 'invalidation-key-1', Buffer.from('proto'));
  t.same(blobStore.get('constructor', 'invalidation-key-1'), Buffer.from('proto'));
  blobStore.save();

  blobStore = new FileSystemBlobStore(storageDirectory);
  t.same(blobStore.get('constructor', 'invalidation-key-1'), Buffer.from('proto'));
  t.type(blobStore.get('hasOwnProperty', 'invalidation-key-2'), 'undefined');

  t.end();
});

tap.test('dirty state (set)', t => {
  blobStore.set('foo', 'invalidation-key-1', Buffer.from('foo'));
  t.equal(blobStore.isDirty(), true);
  blobStore.save();

  blobStore = new FileSystemBlobStore(storageDirectory);

  t.equal(blobStore.isDirty(), false);
  blobStore.set('foo', 'invalidation-key-2', Buffer.from('bar'));
  t.equal(blobStore.isDirty(), true);

  t.end();
});

tap.test('dirty state (delete memory)', t => {
  blobStore.delete('foo');
  t.equal(blobStore.isDirty(), false);
  blobStore.set('foo', 'invalidation-key-1', Buffer.from('foo'));
  blobStore.delete('foo');
  t.equal(blobStore.isDirty(), true);
  blobStore.save();

  blobStore = new FileSystemBlobStore(storageDirectory);

  t.equal(blobStore.isDirty(), false);
  blobStore.set('foo', 'invalidation-key-2', Buffer.from('bar'));
  t.equal(blobStore.isDirty(), true);

  t.end();
});

tap.test('dirty state (delete stored)', t => {
  blobStore.set('foo', 'invalidation-key-1', Buffer.from('foo'));
  blobStore.save();

  blobStore = new FileSystemBlobStore(storageDirectory);

  blobStore.delete('foo');
  t.equal(blobStore.isDirty(), true);

  t.end();
});

tap.test('prefix', t => {
  blobStore.set('foo', 'invalidation-key-1', Buffer.from('foo'));
  blobStore.save();

  t.ok(fs.existsSync(path.join(storageDirectory, 'MAP')));
  t.ok(fs.existsSync(path.join(storageDirectory, 'BLOB')));

  storageDirectory = temp.path('filesystemblobstore');
  blobStore = new FileSystemBlobStore(storageDirectory, 'prefix');
  blobStore.set('foo', 'invalidation-key-1', Buffer.from('foo'));
  blobStore.save();

  t.ok(fs.existsSync(path.join(storageDirectory, 'prefix.MAP')));
  t.ok(fs.existsSync(path.join(storageDirectory, 'prefix.BLOB')));

  t.end();
});