api-process-spec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. const { ipcRenderer } = require('electron');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const { expect } = require('chai');
  5. describe('process module', () => {
  6. describe('process.getCreationTime()', () => {
  7. it('returns a creation time', () => {
  8. const creationTime = process.getCreationTime();
  9. expect(creationTime).to.be.a('number').and.be.at.least(0);
  10. });
  11. });
  12. describe('process.getCPUUsage()', () => {
  13. it('returns a cpu usage object', () => {
  14. const cpuUsage = process.getCPUUsage();
  15. expect(cpuUsage.percentCPUUsage).to.be.a('number');
  16. expect(cpuUsage.idleWakeupsPerSecond).to.be.a('number');
  17. });
  18. });
  19. describe('process.getIOCounters()', () => {
  20. before(function () {
  21. if (process.platform === 'darwin') {
  22. this.skip();
  23. }
  24. });
  25. it('returns an io counters object', () => {
  26. const ioCounters = process.getIOCounters();
  27. expect(ioCounters.readOperationCount).to.be.a('number');
  28. expect(ioCounters.writeOperationCount).to.be.a('number');
  29. expect(ioCounters.otherOperationCount).to.be.a('number');
  30. expect(ioCounters.readTransferCount).to.be.a('number');
  31. expect(ioCounters.writeTransferCount).to.be.a('number');
  32. expect(ioCounters.otherTransferCount).to.be.a('number');
  33. });
  34. });
  35. describe('process.getBlinkMemoryInfo()', () => {
  36. it('returns blink memory information object', () => {
  37. const heapStats = process.getBlinkMemoryInfo();
  38. expect(heapStats.allocated).to.be.a('number');
  39. expect(heapStats.total).to.be.a('number');
  40. });
  41. });
  42. describe('process.getProcessMemoryInfo()', async () => {
  43. it('resolves promise successfully with valid data', async () => {
  44. const memoryInfo = await process.getProcessMemoryInfo();
  45. expect(memoryInfo).to.be.an('object');
  46. if (process.platform === 'linux' || process.platform === 'windows') {
  47. expect(memoryInfo.residentSet).to.be.a('number').greaterThan(0);
  48. }
  49. expect(memoryInfo.private).to.be.a('number').greaterThan(0);
  50. // Shared bytes can be zero
  51. expect(memoryInfo.shared).to.be.a('number').greaterThan(-1);
  52. });
  53. });
  54. describe('process.getSystemMemoryInfo()', () => {
  55. it('returns system memory info object', () => {
  56. const systemMemoryInfo = process.getSystemMemoryInfo();
  57. expect(systemMemoryInfo.free).to.be.a('number');
  58. expect(systemMemoryInfo.total).to.be.a('number');
  59. });
  60. });
  61. describe('process.getSystemVersion()', () => {
  62. it('returns a string', () => {
  63. expect(process.getSystemVersion()).to.be.a('string');
  64. if (process.platform === 'darwin') {
  65. expect(process.getSystemVersion()).to.not.equal('10.16'); // #26419
  66. }
  67. });
  68. });
  69. describe('process.getHeapStatistics()', () => {
  70. it('returns heap statistics object', () => {
  71. const heapStats = process.getHeapStatistics();
  72. expect(heapStats.totalHeapSize).to.be.a('number');
  73. expect(heapStats.totalHeapSizeExecutable).to.be.a('number');
  74. expect(heapStats.totalPhysicalSize).to.be.a('number');
  75. expect(heapStats.totalAvailableSize).to.be.a('number');
  76. expect(heapStats.usedHeapSize).to.be.a('number');
  77. expect(heapStats.heapSizeLimit).to.be.a('number');
  78. expect(heapStats.mallocedMemory).to.be.a('number');
  79. expect(heapStats.peakMallocedMemory).to.be.a('number');
  80. expect(heapStats.doesZapGarbage).to.be.a('boolean');
  81. });
  82. });
  83. describe('process.takeHeapSnapshot()', () => {
  84. it('returns true on success', async () => {
  85. const filePath = path.join(await ipcRenderer.invoke('get-temp-dir'), 'test.heapsnapshot');
  86. const cleanup = () => {
  87. try {
  88. fs.unlinkSync(filePath);
  89. } catch (e) {
  90. // ignore error
  91. }
  92. };
  93. try {
  94. const success = process.takeHeapSnapshot(filePath);
  95. expect(success).to.be.true();
  96. const stats = fs.statSync(filePath);
  97. expect(stats.size).not.to.be.equal(0);
  98. } finally {
  99. cleanup();
  100. }
  101. });
  102. it('returns false on failure', () => {
  103. const success = process.takeHeapSnapshot('');
  104. expect(success).to.be.false();
  105. });
  106. });
  107. });