api-process-spec.ts 7.9 KB

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