|
| 1 | +var expect = require('chai').expect |
| 2 | +var EventEmitter = require('events').EventEmitter |
| 3 | + |
| 4 | +var v8profiler = require('../../../optionalDependencies/v8-profiler') |
| 5 | +var CpuProfiler = require('./') |
| 6 | + |
| 7 | +describe('The Cpu Profiler module', function () { |
| 8 | + it('sends profile', function (done) { |
| 9 | + var msgBus = new EventEmitter() |
| 10 | + var profileData = 'some-profile-data' |
| 11 | + var collectorApi = { |
| 12 | + sendCpuProfile: this.sandbox.spy() |
| 13 | + } |
| 14 | + var profile = { |
| 15 | + export: function (cb) { |
| 16 | + cb(null, profileData) |
| 17 | + }, |
| 18 | + delete: this.sandbox.spy() |
| 19 | + } |
| 20 | + |
| 21 | + var startProfilingStub = this.sandbox.stub(v8profiler, 'startProfiling', function () {}) |
| 22 | + var stopProfilingStub = this.sandbox.stub(v8profiler, 'stopProfiling', function () { |
| 23 | + return profile |
| 24 | + }) |
| 25 | + |
| 26 | + var now = 1234 |
| 27 | + |
| 28 | + var profiler = CpuProfiler.create({ |
| 29 | + collectorApi: collectorApi, |
| 30 | + controlBus: msgBus |
| 31 | + }) |
| 32 | + |
| 33 | + profiler.profileWindow = 0 |
| 34 | + |
| 35 | + this.sandbox.stub(Date, 'now', function () { |
| 36 | + return now |
| 37 | + }) |
| 38 | + |
| 39 | + profiler.sendProfile() |
| 40 | + |
| 41 | + setTimeout(function () { |
| 42 | + expect(startProfilingStub).to.be.calledWith('trace-cpu-profile', true) |
| 43 | + expect(stopProfilingStub).to.be.calledWith('trace-cpu-profile') |
| 44 | + expect(collectorApi.sendCpuProfile).to.be.calledWith({ |
| 45 | + cpuProfile: profileData, |
| 46 | + time: now |
| 47 | + }) |
| 48 | + expect(profile.delete).to.be.called |
| 49 | + done() |
| 50 | + }, 1) |
| 51 | + |
| 52 | + }) |
| 53 | +}) |
0 commit comments