api-process-spec.ts 9.1 KB

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