forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-process-spec.ts
209 lines (187 loc) · 8.06 KB
/
api-process-spec.ts
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
import * as fs from 'node:fs';
import * as path from 'node:path';
import { expect } from 'chai';
import { BrowserWindow } from 'electron';
import { defer } from './lib/spec-helpers';
import { app } from 'electron/main';
import { closeAllWindows } from './lib/window-helpers';
describe('process module', () => {
describe('renderer process', () => {
let w: BrowserWindow;
before(async () => {
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
await w.loadURL('about:blank');
});
after(closeAllWindows);
describe('process.getCreationTime()', () => {
it('returns a creation time', async () => {
const creationTime = await w.webContents.executeJavaScript('process.getCreationTime()');
expect(creationTime).to.be.a('number').and.be.at.least(0);
});
});
describe('process.getCPUUsage()', () => {
it('returns a cpu usage object', async () => {
const cpuUsage = await w.webContents.executeJavaScript('process.getCPUUsage()');
expect(cpuUsage.percentCPUUsage).to.be.a('number');
expect(cpuUsage.cumulativeCPUUsage).to.be.a('number');
expect(cpuUsage.idleWakeupsPerSecond).to.be.a('number');
});
});
describe('process.getBlinkMemoryInfo()', () => {
it('returns blink memory information object', async () => {
const heapStats = await w.webContents.executeJavaScript('process.getBlinkMemoryInfo()');
expect(heapStats.allocated).to.be.a('number');
expect(heapStats.total).to.be.a('number');
});
});
describe('process.getProcessMemoryInfo()', () => {
it('resolves promise successfully with valid data', async () => {
const memoryInfo = await w.webContents.executeJavaScript('process.getProcessMemoryInfo()');
expect(memoryInfo).to.be.an('object');
if (process.platform === 'linux' || process.platform === 'win32') {
expect(memoryInfo.residentSet).to.be.a('number').greaterThan(0);
}
expect(memoryInfo.private).to.be.a('number').greaterThan(0);
// Shared bytes can be zero
expect(memoryInfo.shared).to.be.a('number').greaterThan(-1);
});
});
describe('process.getSystemMemoryInfo()', () => {
it('returns system memory info object', async () => {
const systemMemoryInfo = await w.webContents.executeJavaScript('process.getSystemMemoryInfo()');
expect(systemMemoryInfo.free).to.be.a('number');
expect(systemMemoryInfo.total).to.be.a('number');
});
});
describe('process.getSystemVersion()', () => {
it('returns a string', async () => {
const systemVersion = await w.webContents.executeJavaScript('process.getSystemVersion()');
expect(systemVersion).to.be.a('string');
});
});
describe('process.getHeapStatistics()', () => {
it('returns heap statistics object', async () => {
const heapStats = await w.webContents.executeJavaScript('process.getHeapStatistics()');
expect(heapStats.totalHeapSize).to.be.a('number');
expect(heapStats.totalHeapSizeExecutable).to.be.a('number');
expect(heapStats.totalPhysicalSize).to.be.a('number');
expect(heapStats.totalAvailableSize).to.be.a('number');
expect(heapStats.usedHeapSize).to.be.a('number');
expect(heapStats.heapSizeLimit).to.be.a('number');
expect(heapStats.mallocedMemory).to.be.a('number');
expect(heapStats.peakMallocedMemory).to.be.a('number');
expect(heapStats.doesZapGarbage).to.be.a('boolean');
});
});
describe('process.takeHeapSnapshot()', () => {
it('returns true on success', async () => {
const filePath = path.join(app.getPath('temp'), 'test.heapsnapshot');
defer(() => {
try {
fs.unlinkSync(filePath);
} catch {
// ignore error
}
});
const success = await w.webContents.executeJavaScript(`process.takeHeapSnapshot(${JSON.stringify(filePath)})`);
expect(success).to.be.true();
const stats = fs.statSync(filePath);
expect(stats.size).not.to.be.equal(0);
});
it('returns false on failure', async () => {
const success = await w.webContents.executeJavaScript('process.takeHeapSnapshot("")');
expect(success).to.be.false();
});
});
describe('process.contextId', () => {
it('is a string', async () => {
const contextId = await w.webContents.executeJavaScript('process.contextId');
expect(contextId).to.be.a('string');
});
});
});
describe('main process', () => {
describe('process.getCreationTime()', () => {
it('returns a creation time', () => {
const creationTime = process.getCreationTime();
expect(creationTime).to.be.a('number').and.be.at.least(0);
});
});
describe('process.getCPUUsage()', () => {
it('returns a cpu usage object', () => {
const cpuUsage = process.getCPUUsage();
expect(cpuUsage.percentCPUUsage).to.be.a('number');
expect(cpuUsage.cumulativeCPUUsage).to.be.a('number');
expect(cpuUsage.idleWakeupsPerSecond).to.be.a('number');
});
});
describe('process.getBlinkMemoryInfo()', () => {
it('returns blink memory information object', () => {
const heapStats = process.getBlinkMemoryInfo();
expect(heapStats.allocated).to.be.a('number');
expect(heapStats.total).to.be.a('number');
});
});
describe('process.getProcessMemoryInfo()', () => {
it('resolves promise successfully with valid data', async () => {
const memoryInfo = await process.getProcessMemoryInfo();
expect(memoryInfo).to.be.an('object');
if (process.platform === 'linux' || process.platform === 'win32') {
expect(memoryInfo.residentSet).to.be.a('number').greaterThan(0);
}
expect(memoryInfo.private).to.be.a('number').greaterThan(0);
// Shared bytes can be zero
expect(memoryInfo.shared).to.be.a('number').greaterThan(-1);
});
});
describe('process.getSystemMemoryInfo()', () => {
it('returns system memory info object', () => {
const systemMemoryInfo = process.getSystemMemoryInfo();
expect(systemMemoryInfo.free).to.be.a('number');
expect(systemMemoryInfo.total).to.be.a('number');
});
});
describe('process.getSystemVersion()', () => {
it('returns a string', () => {
const systemVersion = process.getSystemVersion();
expect(systemVersion).to.be.a('string');
});
});
describe('process.getHeapStatistics()', () => {
it('returns heap statistics object', async () => {
const heapStats = process.getHeapStatistics();
expect(heapStats.totalHeapSize).to.be.a('number');
expect(heapStats.totalHeapSizeExecutable).to.be.a('number');
expect(heapStats.totalPhysicalSize).to.be.a('number');
expect(heapStats.totalAvailableSize).to.be.a('number');
expect(heapStats.usedHeapSize).to.be.a('number');
expect(heapStats.heapSizeLimit).to.be.a('number');
expect(heapStats.mallocedMemory).to.be.a('number');
expect(heapStats.peakMallocedMemory).to.be.a('number');
expect(heapStats.doesZapGarbage).to.be.a('boolean');
});
});
describe('process.takeHeapSnapshot()', () => {
// DISABLED-FIXME(nornagon): this seems to take a really long time when run in the
// main process, for unknown reasons.
it('returns true on success', () => {
const filePath = path.join(app.getPath('temp'), 'test.heapsnapshot');
defer(() => {
try {
fs.unlinkSync(filePath);
} catch {
// ignore error
}
});
const success = process.takeHeapSnapshot(filePath);
expect(success).to.be.true();
const stats = fs.statSync(filePath);
expect(stats.size).not.to.be.equal(0);
});
it('returns false on failure', async () => {
const success = process.takeHeapSnapshot('');
expect(success).to.be.false();
});
});
});
});