api-process-spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. const { remote } = 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. });
  65. });
  66. describe('process.getHeapStatistics()', () => {
  67. it('returns heap statistics object', () => {
  68. const heapStats = process.getHeapStatistics();
  69. expect(heapStats.totalHeapSize).to.be.a('number');
  70. expect(heapStats.totalHeapSizeExecutable).to.be.a('number');
  71. expect(heapStats.totalPhysicalSize).to.be.a('number');
  72. expect(heapStats.totalAvailableSize).to.be.a('number');
  73. expect(heapStats.usedHeapSize).to.be.a('number');
  74. expect(heapStats.heapSizeLimit).to.be.a('number');
  75. expect(heapStats.mallocedMemory).to.be.a('number');
  76. expect(heapStats.peakMallocedMemory).to.be.a('number');
  77. expect(heapStats.doesZapGarbage).to.be.a('boolean');
  78. });
  79. });
  80. describe('process.takeHeapSnapshot()', () => {
  81. it('returns true on success', () => {
  82. const filePath = path.join(remote.app.getPath('temp'), 'test.heapsnapshot');
  83. const cleanup = () => {
  84. try {
  85. fs.unlinkSync(filePath);
  86. } catch (e) {
  87. // ignore error
  88. }
  89. };
  90. try {
  91. const success = process.takeHeapSnapshot(filePath);
  92. expect(success).to.be.true();
  93. const stats = fs.statSync(filePath);
  94. expect(stats.size).not.to.be.equal(0);
  95. } finally {
  96. cleanup();
  97. }
  98. });
  99. it('returns false on failure', () => {
  100. const success = process.takeHeapSnapshot('');
  101. expect(success).to.be.false();
  102. });
  103. });
  104. });