api-process-spec.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.getProcessMemoryInfo()', async () => {
  36. it('resolves promise successfully with valid data', async () => {
  37. const memoryInfo = await process.getProcessMemoryInfo()
  38. expect(memoryInfo).to.be.an('object')
  39. if (process.platform === 'linux' || process.platform === 'windows') {
  40. expect(memoryInfo.residentSet).to.be.a('number').greaterThan(0)
  41. }
  42. expect(memoryInfo.private).to.be.a('number').greaterThan(0)
  43. // Shared bytes can be zero
  44. expect(memoryInfo.shared).to.be.a('number').greaterThan(-1)
  45. })
  46. })
  47. describe('process.getSystemMemoryInfo()', () => {
  48. it('returns system memory info object', () => {
  49. const systemMemoryInfo = process.getSystemMemoryInfo()
  50. expect(systemMemoryInfo.free).to.be.a('number')
  51. expect(systemMemoryInfo.total).to.be.a('number')
  52. })
  53. })
  54. describe('process.getHeapStatistics()', () => {
  55. it('returns heap statistics object', () => {
  56. const heapStats = process.getHeapStatistics()
  57. expect(heapStats.totalHeapSize).to.be.a('number')
  58. expect(heapStats.totalHeapSizeExecutable).to.be.a('number')
  59. expect(heapStats.totalPhysicalSize).to.be.a('number')
  60. expect(heapStats.totalAvailableSize).to.be.a('number')
  61. expect(heapStats.usedHeapSize).to.be.a('number')
  62. expect(heapStats.heapSizeLimit).to.be.a('number')
  63. expect(heapStats.mallocedMemory).to.be.a('number')
  64. expect(heapStats.peakMallocedMemory).to.be.a('number')
  65. expect(heapStats.doesZapGarbage).to.be.a('boolean')
  66. })
  67. })
  68. describe('process.takeHeapSnapshot()', () => {
  69. it('returns true on success', () => {
  70. const filePath = path.join(remote.app.getPath('temp'), 'test.heapsnapshot')
  71. const cleanup = () => {
  72. try {
  73. fs.unlinkSync(filePath)
  74. } catch (e) {
  75. // ignore error
  76. }
  77. }
  78. try {
  79. const success = process.takeHeapSnapshot(filePath)
  80. expect(success).to.be.true()
  81. const stats = fs.statSync(filePath)
  82. expect(stats.size).not.to.be.equal(0)
  83. } finally {
  84. cleanup()
  85. }
  86. })
  87. it('returns false on failure', () => {
  88. const success = process.takeHeapSnapshot('')
  89. expect(success).to.be.false()
  90. })
  91. })
  92. })