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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { strictEqual } from 'assert';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { Schemas } from 'vs/base/common/network';
import { joinPath } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { Storage } from 'vs/base/parts/storage/common/storage';
import { flakySuite } from 'vs/base/test/common/testUtils';
import { runWithFakedTimers } from 'vs/base/test/common/timeTravelScheduler';
import { FileService } from 'vs/platform/files/common/fileService';
import { InMemoryFileSystemProvider } from 'vs/platform/files/common/inMemoryFilesystemProvider';
import { NullLogService } from 'vs/platform/log/common/log';
import { StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { createSuite } from 'vs/platform/storage/test/common/storageService.test';
import { UriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentityService';
import { IUserDataProfile, UserDataProfilesService } from 'vs/platform/userDataProfile/common/userDataProfile';
import { BrowserStorageService, IndexedDBStorageDatabase } from 'vs/workbench/services/storage/browser/storageService';
import { UserDataProfileService } from 'vs/workbench/services/userDataProfile/common/userDataProfileService';
import { TestEnvironmentService } from 'vs/workbench/test/browser/workbenchTestServices';
async function createStorageService(): Promise<[DisposableStore, BrowserStorageService]> {
const disposables = new DisposableStore();
const logService = new NullLogService();
const fileService = disposables.add(new FileService(logService));
const userDataProvider = disposables.add(new InMemoryFileSystemProvider());
disposables.add(fileService.registerProvider(Schemas.vscodeUserData, userDataProvider));
const profilesRoot = URI.file('/profiles').with({ scheme: Schemas.inMemory });
const inMemoryExtraProfileRoot = joinPath(profilesRoot, 'extra');
const inMemoryExtraProfile: IUserDataProfile = {
id: 'id',
name: 'inMemory',
isDefault: false,
location: inMemoryExtraProfileRoot,
globalStorageHome: joinPath(inMemoryExtraProfileRoot, 'globalStorageHome'),
settingsResource: joinPath(inMemoryExtraProfileRoot, 'settingsResource'),
keybindingsResource: joinPath(inMemoryExtraProfileRoot, 'keybindingsResource'),
tasksResource: joinPath(inMemoryExtraProfileRoot, 'tasksResource'),
snippetsHome: joinPath(inMemoryExtraProfileRoot, 'snippetsHome'),
extensionsResource: joinPath(inMemoryExtraProfileRoot, 'extensionsResource')
};
const storageService = disposables.add(new BrowserStorageService({ id: 'workspace-storage-test' }, new UserDataProfileService(inMemoryExtraProfile, new UserDataProfilesService(TestEnvironmentService, fileService, new UriIdentityService(fileService), logService)), logService));
await storageService.initialize();
return [disposables, storageService];
}
flakySuite('StorageService (browser)', function () {
const disposables = new DisposableStore();
let storageService: BrowserStorageService;
createSuite<BrowserStorageService>({
setup: async () => {
const res = await createStorageService();
disposables.add(res[0]);
storageService = res[1];
return storageService;
},
teardown: async () => {
await storageService.clear();
disposables.clear();
}
});
});
flakySuite('StorageService (browser specific)', () => {
const disposables = new DisposableStore();
let storageService: BrowserStorageService;
setup(async () => {
const res = await createStorageService();
disposables.add(res[0]);
storageService = res[1];
});
teardown(async () => {
await storageService.clear();
disposables.clear();
});
test.skip('clear', () => { // slow test and also only ever being used as a developer action
return runWithFakedTimers({ useFakeTimers: true }, async () => {
storageService.store('bar', 'foo', StorageScope.APPLICATION, StorageTarget.MACHINE);
storageService.store('bar', 3, StorageScope.APPLICATION, StorageTarget.USER);
storageService.store('bar', 'foo', StorageScope.PROFILE, StorageTarget.MACHINE);
storageService.store('bar', 3, StorageScope.PROFILE, StorageTarget.USER);
storageService.store('bar', 'foo', StorageScope.WORKSPACE, StorageTarget.MACHINE);
storageService.store('bar', 3, StorageScope.WORKSPACE, StorageTarget.USER);
await storageService.clear();
for (const scope of [StorageScope.APPLICATION, StorageScope.PROFILE, StorageScope.WORKSPACE]) {
for (const target of [StorageTarget.USER, StorageTarget.MACHINE]) {
strictEqual(storageService.get('bar', scope), undefined);
strictEqual(storageService.keys(scope, target).length, 0);
}
}
});
});
});
flakySuite('IndexDBStorageDatabase (browser)', () => {
const id = 'workspace-storage-db-test';
const logService = new NullLogService();
teardown(async () => {
const storage = await IndexedDBStorageDatabase.create({ id }, logService);
await storage.clear();
});
test('Basics', async () => {
let storage = new Storage(await IndexedDBStorageDatabase.create({ id }, logService));
await storage.init();
// Insert initial data
storage.set('bar', 'foo');
storage.set('barNumber', 55);
storage.set('barBoolean', true);
storage.set('barUndefined', undefined);
storage.set('barNull', null);
strictEqual(storage.get('bar'), 'foo');
strictEqual(storage.get('barNumber'), '55');
strictEqual(storage.get('barBoolean'), 'true');
strictEqual(storage.get('barUndefined'), undefined);
strictEqual(storage.get('barNull'), undefined);
strictEqual(storage.size, 3);
strictEqual(storage.items.size, 3);
await storage.close();
storage = new Storage(await IndexedDBStorageDatabase.create({ id }, logService));
await storage.init();
// Check initial data still there
strictEqual(storage.get('bar'), 'foo');
strictEqual(storage.get('barNumber'), '55');
strictEqual(storage.get('barBoolean'), 'true');
strictEqual(storage.get('barUndefined'), undefined);
strictEqual(storage.get('barNull'), undefined);
strictEqual(storage.size, 3);
strictEqual(storage.items.size, 3);
// Update data
storage.set('bar', 'foo2');
storage.set('barNumber', 552);
strictEqual(storage.get('bar'), 'foo2');
strictEqual(storage.get('barNumber'), '552');
await storage.close();
storage = new Storage(await IndexedDBStorageDatabase.create({ id }, logService));
await storage.init();
// Check initial data still there
strictEqual(storage.get('bar'), 'foo2');
strictEqual(storage.get('barNumber'), '552');
strictEqual(storage.get('barBoolean'), 'true');
strictEqual(storage.get('barUndefined'), undefined);
strictEqual(storage.get('barNull'), undefined);
strictEqual(storage.size, 3);
strictEqual(storage.items.size, 3);
// Delete data
storage.delete('bar');
storage.delete('barNumber');
storage.delete('barBoolean');
strictEqual(storage.get('bar', 'undefined'), 'undefined');
strictEqual(storage.get('barNumber', 'undefinedNumber'), 'undefinedNumber');
strictEqual(storage.get('barBoolean', 'undefinedBoolean'), 'undefinedBoolean');
strictEqual(storage.size, 0);
strictEqual(storage.items.size, 0);
await storage.close();
storage = new Storage(await IndexedDBStorageDatabase.create({ id }, logService));
await storage.init();
strictEqual(storage.get('bar', 'undefined'), 'undefined');
strictEqual(storage.get('barNumber', 'undefinedNumber'), 'undefinedNumber');
strictEqual(storage.get('barBoolean', 'undefinedBoolean'), 'undefinedBoolean');
strictEqual(storage.size, 0);
strictEqual(storage.items.size, 0);
});
test('Clear', async () => {
let storage = new Storage(await IndexedDBStorageDatabase.create({ id }, logService));
await storage.init();
storage.set('bar', 'foo');
storage.set('barNumber', 55);
storage.set('barBoolean', true);
await storage.close();
const db = await IndexedDBStorageDatabase.create({ id }, logService);
storage = new Storage(db);
await storage.init();
await db.clear();
storage = new Storage(await IndexedDBStorageDatabase.create({ id }, logService));
await storage.init();
strictEqual(storage.get('bar'), undefined);
strictEqual(storage.get('barNumber'), undefined);
strictEqual(storage.get('barBoolean'), undefined);
strictEqual(storage.size, 0);
strictEqual(storage.items.size, 0);
});
test('Inserts and Deletes at the same time', async () => {
let storage = new Storage(await IndexedDBStorageDatabase.create({ id }, logService));
await storage.init();
storage.set('bar', 'foo');
storage.set('barNumber', 55);
storage.set('barBoolean', true);
await storage.close();
storage = new Storage(await IndexedDBStorageDatabase.create({ id }, logService));
await storage.init();
storage.set('bar', 'foobar');
const largeItem = JSON.stringify({ largeItem: 'Hello World'.repeat(1000) });
storage.set('largeItem', largeItem);
storage.delete('barNumber');
storage.delete('barBoolean');
await storage.close();
storage = new Storage(await IndexedDBStorageDatabase.create({ id }, logService));
await storage.init();
strictEqual(storage.get('bar'), 'foobar');
strictEqual(storage.get('largeItem'), largeItem);
strictEqual(storage.get('barNumber'), undefined);
strictEqual(storage.get('barBoolean'), undefined);
});
});
|