api-process-spec.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import * as fs from 'node:fs';
  2. import * as path from 'node:path';
  3. import { expect } from 'chai';
  4. import { BrowserWindow } from 'electron';
  5. import { defer } from './lib/spec-helpers';
  6. import { app } from 'electron/main';
  7. import { closeAllWindows } from './lib/window-helpers';
  8. describe('process module', () => {
  9. describe('renderer process', () => {
  10. let w: BrowserWindow;
  11. before(async () => {
  12. w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  13. await w.loadURL('about:blank');
  14. });
  15. after(closeAllWindows);
  16. describe('process.getCreationTime()', () => {
  17. it('returns a creation time', async () => {
  18. const creationTime = await w.webContents.executeJavaScript('process.getCreationTime()');
  19. expect(creationTime).to.be.a('number').and.be.at.least(0);
  20. });
  21. });
  22. describe('process.getCPUUsage()', () => {
  23. it('returns a cpu usage object', async () => {
  24. const cpuUsage = await w.webContents.executeJavaScript('process.getCPUUsage()');
  25. expect(cpuUsage.percentCPUUsage).to.be.a('number');
  26. expect(cpuUsage.cumulativeCPUUsage).to.be.a('number');
  27. expect(cpuUsage.idleWakeupsPerSecond).to.be.a('number');
  28. });
  29. });
  30. describe('process.getBlinkMemoryInfo()', () => {
  31. it('returns blink memory information object', async () => {
  32. const heapStats = await w.webContents.executeJavaScript('process.getBlinkMemoryInfo()');
  33. expect(heapStats.allocated).to.be.a('number');
  34. expect(heapStats.total).to.be.a('number');
  35. });
  36. });
  37. describe('process.getProcessMemoryInfo()', () => {
  38. it('resolves promise successfully with valid data', async () => {
  39. const memoryInfo = await w.webContents.executeJavaScript('process.getProcessMemoryInfo()');
  40. expect(memoryInfo).to.be.an('object');
  41. if (process.platform === 'linux' || process.platform === 'win32') {
  42. expect(memoryInfo.residentSet).to.be.a('number').greaterThan(0);
  43. }
  44. expect(memoryInfo.private).to.be.a('number').greaterThan(0);
  45. // Shared bytes can be zero
  46. expect(memoryInfo.shared).to.be.a('number').greaterThan(-1);
  47. });
  48. });
  49. describe('process.getSystemMemoryInfo()', () => {
  50. it('returns system memory info object', async () => {
  51. const systemMemoryInfo = await w.webContents.executeJavaScript('process.getSystemMemoryInfo()');
  52. expect(systemMemoryInfo.free).to.be.a('number');
  53. expect(systemMemoryInfo.total).to.be.a('number');
  54. });
  55. });
  56. describe('process.getSystemVersion()', () => {
  57. it('returns a string', async () => {
  58. const systemVersion = await w.webContents.executeJavaScript('process.getSystemVersion()');
  59. expect(systemVersion).to.be.a('string');
  60. });
  61. });
  62. describe('process.getHeapStatistics()', () => {
  63. it('returns heap statistics object', async () => {
  64. const heapStats = await w.webContents.executeJavaScript('process.getHeapStatistics()');
  65. expect(heapStats.totalHeapSize).to.be.a('number');
  66. expect(heapStats.totalHeapSizeExecutable).to.be.a('number');
  67. expect(heapStats.totalPhysicalSize).to.be.a('number');
  68. expect(heapStats.totalAvailableSize).to.be.a('number');
  69. expect(heapStats.usedHeapSize).to.be.a('number');
  70. expect(heapStats.heapSizeLimit).to.be.a('number');
  71. expect(heapStats.mallocedMemory).to.be.a('number');
  72. expect(heapStats.peakMallocedMemory).to.be.a('number');
  73. expect(heapStats.doesZapGarbage).to.be.a('boolean');
  74. });
  75. });
  76. describe('process.takeHeapSnapshot()', () => {
  77. it('returns true on success', async () => {
  78. const filePath = path.join(app.getPath('temp'), 'test.heapsnapshot');
  79. defer(() => {
  80. try {
  81. fs.unlinkSync(filePath);
  82. } catch {
  83. // ignore error
  84. }
  85. });
  86. const success = await w.webContents.executeJavaScript(`process.takeHeapSnapshot(${JSON.stringify(filePath)})`);
  87. expect(success).to.be.true();
  88. const stats = fs.statSync(filePath);
  89. expect(stats.size).not.to.be.equal(0);
  90. });
  91. it('returns false on failure', async () => {
  92. const success = await w.webContents.executeJavaScript('process.takeHeapSnapshot("")');
  93. expect(success).to.be.false();
  94. });
  95. });
  96. describe('process.contextId', () => {
  97. it('is a string', async () => {
  98. const contextId = await w.webContents.executeJavaScript('process.contextId');
  99. expect(contextId).to.be.a('string');
  100. });
  101. });
  102. });
  103. describe('main process', () => {
  104. describe('process.getCreationTime()', () => {
  105. it('returns a creation time', () => {
  106. const creationTime = process.getCreationTime();
  107. expect(creationTime).to.be.a('number').and.be.at.least(0);
  108. });
  109. });
  110. describe('process.getCPUUsage()', () => {
  111. it('returns a cpu usage object', () => {
  112. const cpuUsage = process.getCPUUsage();
  113. expect(cpuUsage.percentCPUUsage).to.be.a('number');
  114. expect(cpuUsage.cumulativeCPUUsage).to.be.a('number');
  115. expect(cpuUsage.idleWakeupsPerSecond).to.be.a('number');
  116. });
  117. });
  118. describe('process.getBlinkMemoryInfo()', () => {
  119. it('returns blink memory information object', () => {
  120. const heapStats = process.getBlinkMemoryInfo();
  121. expect(heapStats.allocated).to.be.a('number');
  122. expect(heapStats.total).to.be.a('number');
  123. });
  124. });
  125. describe('process.getProcessMemoryInfo()', () => {
  126. it('resolves promise successfully with valid data', async () => {
  127. const memoryInfo = await process.getProcessMemoryInfo();
  128. expect(memoryInfo).to.be.an('object');
  129. if (process.platform === 'linux' || process.platform === 'win32') {
  130. expect(memoryInfo.residentSet).to.be.a('number').greaterThan(0);
  131. }
  132. expect(memoryInfo.private).to.be.a('number').greaterThan(0);
  133. // Shared bytes can be zero
  134. expect(memoryInfo.shared).to.be.a('number').greaterThan(-1);
  135. });
  136. });
  137. describe('process.getSystemMemoryInfo()', () => {
  138. it('returns system memory info object', () => {
  139. const systemMemoryInfo = process.getSystemMemoryInfo();
  140. expect(systemMemoryInfo.free).to.be.a('number');
  141. expect(systemMemoryInfo.total).to.be.a('number');
  142. });
  143. });
  144. describe('process.getSystemVersion()', () => {
  145. it('returns a string', () => {
  146. const systemVersion = process.getSystemVersion();
  147. expect(systemVersion).to.be.a('string');
  148. });
  149. });
  150. describe('process.getHeapStatistics()', () => {
  151. it('returns heap statistics object', async () => {
  152. const heapStats = process.getHeapStatistics();
  153. expect(heapStats.totalHeapSize).to.be.a('number');
  154. expect(heapStats.totalHeapSizeExecutable).to.be.a('number');
  155. expect(heapStats.totalPhysicalSize).to.be.a('number');
  156. expect(heapStats.totalAvailableSize).to.be.a('number');
  157. expect(heapStats.usedHeapSize).to.be.a('number');
  158. expect(heapStats.heapSizeLimit).to.be.a('number');
  159. expect(heapStats.mallocedMemory).to.be.a('number');
  160. expect(heapStats.peakMallocedMemory).to.be.a('number');
  161. expect(heapStats.doesZapGarbage).to.be.a('boolean');
  162. });
  163. });
  164. describe('process.takeHeapSnapshot()', () => {
  165. // DISABLED-FIXME(nornagon): this seems to take a really long time when run in the
  166. // main process, for unknown reasons.
  167. it('returns true on success', () => {
  168. const filePath = path.join(app.getPath('temp'), 'test.heapsnapshot');
  169. defer(() => {
  170. try {
  171. fs.unlinkSync(filePath);
  172. } catch {
  173. // ignore error
  174. }
  175. });
  176. const success = process.takeHeapSnapshot(filePath);
  177. expect(success).to.be.true();
  178. const stats = fs.statSync(filePath);
  179. expect(stats.size).not.to.be.equal(0);
  180. });
  181. it('returns false on failure', async () => {
  182. const success = process.takeHeapSnapshot('');
  183. expect(success).to.be.false();
  184. });
  185. });
  186. });
  187. });