This repository was archived by the owner on Feb 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathstorage-manager.spec.js
More file actions
153 lines (133 loc) · 4.65 KB
/
storage-manager.spec.js
File metadata and controls
153 lines (133 loc) · 4.65 KB
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
const element = require('../../../index');
const config = require('../json/config.local.json');
const StorageManager = require('./storage-manager');
const anchorFile = {
merkleRoot:
'2fd0f5e87f72d787235ee6c1673500c9929a2559edfcdf3637ba9ab05a827a16',
batchFileHash: 'QmTT6BwuEeDgNs3ixQ2G29izoPawSjAh8wT97uZek5BQVG',
didUniqueSuffixes: ['MRO_nAwc19U1pusMn5PXd_5iY6ATvCyeuFU-bO0XUkI'],
};
const anchorFile2 = {
merkleRoot:
'2fd0f5e87f72d787235ee6c1673500c9929a2559edfcdf3637ba9ab05a827a16',
batchFileHash: 'QmTT6BwuEeDgNs3ixQ2G29izoPawSjAh8wT97uZek5BQVG',
didUniqueSuffixes: ['XRO_nAwc19U1pusMn5PXd_5iY6ATvCyeuFU-bO0XUkI'],
};
jest.setTimeout(20 * 1000);
describe('StorageManager', () => {
let storage;
let db;
beforeAll(() => {
storage = element.storage.ipfs.configure({
multiaddr: config.ipfsApiMultiAddr,
});
db = new element.adapters.database.ElementRXDBAdapter({
name: 'storage-manager',
adapter: 'memory',
});
});
describe('constructor', () => {
it('should create a manager instance', async () => {
const manager = new StorageManager(db, storage, {
autoPersist: false,
retryIntervalSeconds: 4,
});
expect(manager.storage.ipfs).toBeDefined();
expect(manager.db).toBeDefined();
});
});
describe('smart write', () => {
it('should should use db and storage', async () => {
const manager = new StorageManager(db, storage, {
autoPersist: false,
retryIntervalSeconds: 4,
});
const cid = await manager.write(anchorFile);
expect(cid).toBe('QmXRoAeyBTKA3N8D4NLR6wtEes4iwzeMien78cZy2YP3ba');
const record = await manager.db.read(
`element:sidetree:cas-cachable:${cid}`
);
expect(record.id).toBe(`element:sidetree:cas-cachable:${cid}`);
const data = await manager.storage.read(cid);
expect(data).toEqual(anchorFile);
expect(record.persisted).toBe(true);
});
it('can persist from db manually', async () => {
const manager = new StorageManager(db, storage, {
autoPersist: false,
retryIntervalSeconds: 4,
});
let count = 0;
const fakeStorage = {
write: async data => {
if (count === 2) {
return storage.write(data);
}
count += 1;
throw new Error('Fake IPFS is down error');
},
};
const mockedManager = new StorageManager(db, fakeStorage);
const cid = await mockedManager.write(anchorFile2);
expect(cid).toBe('Qma4AeAHhwkFJNHGZ59KpShSmCbnT2JUSCNjffk3h5dm3f');
await mockedManager.retryAllNotPersisted();
const allUnPersisted2 = await mockedManager.db.collection
.find({
type: { $eq: 'element:sidetree:cas-cachable' },
persisted: { $eq: false },
})
.exec()
.then(arrayOfDocs => arrayOfDocs.map(doc => doc.toJSON()));
expect(allUnPersisted2.length).toBe(1);
// can read without persistence
const obj = await manager.read(
'Qma4AeAHhwkFJNHGZ59KpShSmCbnT2JUSCNjffk3h5dm3f'
);
expect(obj).toEqual(anchorFile2);
await mockedManager.retryAllNotPersisted();
const allUnPersisted3 = await mockedManager.db.collection
.find({
type: { $eq: 'element:sidetree:cas-cachable' },
persisted: { $eq: false },
})
.exec()
.then(arrayOfDocs => arrayOfDocs.map(doc => doc.toJSON()));
expect(allUnPersisted3.length).toBe(0);
const record = await manager.db.read(
`element:sidetree:cas-cachable:${cid}`
);
expect(record.id).toBe(`element:sidetree:cas-cachable:${cid}`);
const data = await manager.storage.read(cid);
expect(data).toEqual(anchorFile2);
expect(record.persisted).toBe(true);
});
// need to fix this when we add proper support for remote couchdb
// this hack was added to fix failing IPFS issues
// eslint-disable-next-line
it.skip('can persist from db autmatically', done => {
let count = 0;
const fakeStorage = {
write: async data => {
if (count === 2) {
const res = await storage.write(data);
done();
return res;
}
count += 1;
throw new Error('Fake IPFS is down error');
},
};
const mockedManager = new StorageManager(db, fakeStorage, {
autoPersist: true,
retryIntervalSeconds: 1,
});
mockedManager.write(anchorFile2);
});
it('can read after persisted', async () => {
const obj = await storage.read(
'Qma4AeAHhwkFJNHGZ59KpShSmCbnT2JUSCNjffk3h5dm3f'
);
expect(obj).toEqual(anchorFile2);
});
});
});