-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-system-blob-store-spec.js
112 lines (84 loc) · 3.88 KB
/
file-system-blob-store-spec.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
const temp = require('temp').track();
const path = require('path');
const fs = require('fs-plus');
const FileSystemBlobStore = require('../src/file-system-blob-store');
describe('FileSystemBlobStore', function() {
let [storageDirectory, blobStore] = [];
beforeEach(function() {
storageDirectory = temp.path('atom-spec-filesystemblobstore');
blobStore = FileSystemBlobStore.load(storageDirectory);
});
afterEach(() => fs.removeSync(storageDirectory));
it("is empty when the file doesn't exist", function() {
expect(blobStore.get('foo')).toBeUndefined();
expect(blobStore.get('bar')).toBeUndefined();
});
it('allows to read and write buffers from/to memory without persisting them', function() {
blobStore.set('foo', Buffer.from('foo'));
blobStore.set('bar', Buffer.from('bar'));
expect(blobStore.get('foo')).toEqual(Buffer.from('foo'));
expect(blobStore.get('bar')).toEqual(Buffer.from('bar'));
expect(blobStore.get('baz')).toBeUndefined();
expect(blobStore.get('qux')).toBeUndefined();
});
it('persists buffers when saved and retrieves them on load, giving priority to in-memory ones', function() {
blobStore.set('foo', Buffer.from('foo'));
blobStore.set('bar', Buffer.from('bar'));
blobStore.save();
blobStore = FileSystemBlobStore.load(storageDirectory);
expect(blobStore.get('foo')).toEqual(Buffer.from('foo'));
expect(blobStore.get('bar')).toEqual(Buffer.from('bar'));
expect(blobStore.get('baz')).toBeUndefined();
expect(blobStore.get('qux')).toBeUndefined();
blobStore.set('foo', Buffer.from('changed'));
expect(blobStore.get('foo')).toEqual(Buffer.from('changed'));
});
it('persists in-memory and previously stored buffers, and deletes unused keys when saved', function() {
blobStore.set('foo', Buffer.from('foo'));
blobStore.set('bar', Buffer.from('bar'));
blobStore.save();
blobStore = FileSystemBlobStore.load(storageDirectory);
blobStore.set('bar', Buffer.from('changed'));
blobStore.set('qux', Buffer.from('qux'));
blobStore.save();
blobStore = FileSystemBlobStore.load(storageDirectory);
expect(blobStore.get('foo')).toBeUndefined();
expect(blobStore.get('bar')).toEqual(Buffer.from('changed'));
expect(blobStore.get('qux')).toEqual(Buffer.from('qux'));
});
it('allows to delete keys from both memory and stored buffers', function() {
blobStore.set('a', Buffer.from('a'));
blobStore.set('b', Buffer.from('b'));
blobStore.save();
blobStore = FileSystemBlobStore.load(storageDirectory);
blobStore.get('a'); // prevent the key from being deleted on save
blobStore.set('b', Buffer.from('b'));
blobStore.set('c', Buffer.from('c'));
blobStore.delete('b');
blobStore.delete('c');
blobStore.save();
blobStore = FileSystemBlobStore.load(storageDirectory);
expect(blobStore.get('a')).toEqual(Buffer.from('a'));
expect(blobStore.get('b')).toBeUndefined();
expect(blobStore.get('b')).toBeUndefined();
expect(blobStore.get('c')).toBeUndefined();
});
it('ignores errors when loading an invalid blob store', function() {
blobStore.set('a', Buffer.from('a'));
blobStore.set('b', Buffer.from('b'));
blobStore.save();
// Simulate corruption
fs.writeFileSync(path.join(storageDirectory, 'MAP'), Buffer.from([0]));
fs.writeFileSync(path.join(storageDirectory, 'INVKEYS'), Buffer.from([0]));
fs.writeFileSync(path.join(storageDirectory, 'BLOB'), Buffer.from([0]));
blobStore = FileSystemBlobStore.load(storageDirectory);
expect(blobStore.get('a')).toBeUndefined();
expect(blobStore.get('b')).toBeUndefined();
blobStore.set('a', Buffer.from('x'));
blobStore.set('b', Buffer.from('y'));
blobStore.save();
blobStore = FileSystemBlobStore.load(storageDirectory);
expect(blobStore.get('a')).toEqual(Buffer.from('x'));
expect(blobStore.get('b')).toEqual(Buffer.from('y'));
});
});