瀏覽代碼

fix: Fix unit tests for PerformanceObserverStats

dev1
Jaya Allamsetty 4 年之前
父節點
當前提交
ae2f3a7a20
共有 2 個文件被更改,包括 43 次插入23 次删除
  1. 5
    5
      modules/statistics/PerformanceObserverStats.js
  2. 38
    18
      modules/statistics/PerformanceObserverStats.spec.js

+ 5
- 5
modules/statistics/PerformanceObserverStats.js 查看文件

@@ -24,7 +24,7 @@ export class PerformanceObserverStats {
24 24
     constructor(emitter, statsInterval) {
25 25
         this.eventEmitter = emitter;
26 26
         this.longTasks = 0;
27
-        this.maxTaskDuration = 0;
27
+        this.maxDuration = 0;
28 28
         this.performanceStatsInterval = statsInterval;
29 29
         this.stats = new RunningAverage();
30 30
     }
@@ -34,10 +34,10 @@ export class PerformanceObserverStats {
34 34
      * duration of the longest task recorded by the observer.
35 35
      * @returns {Object}
36 36
      */
37
-    getPerformanceStats() {
37
+    getLongTasksStats() {
38 38
         return {
39 39
             average: (this.stats.getAverage() * SECONDS).toFixed(2), // calc rate per min
40
-            maxTaskDuration: this.maxTaskDuration
40
+            maxDuration: this.maxDuration
41 41
         };
42 42
     }
43 43
 
@@ -53,7 +53,7 @@ export class PerformanceObserverStats {
53 53
 
54 54
             for (const task of entries) {
55 55
                 this.longTasks++;
56
-                this.maxTaskDuration = Math.max(this.maxTaskDuration, task.duration).toFixed(3);
56
+                this.maxDuration = Math.max(this.maxDuration, task.duration).toFixed(3);
57 57
             }
58 58
         };
59 59
 
@@ -74,7 +74,7 @@ export class PerformanceObserverStats {
74 74
 
75 75
             this.stats.addNext(rate);
76 76
             this.eventEmitter.emit(
77
-                StatisticsEvents.LONG_TASKS_STATS, this.getPerformanceStats());
77
+                StatisticsEvents.LONG_TASKS_STATS, this.getLongTasksStats());
78 78
 
79 79
             // Reset the counter and start counting events again.
80 80
             this.longTasks = 0;

+ 38
- 18
modules/statistics/PerformanceObserverStats.spec.js 查看文件

@@ -1,47 +1,67 @@
1
-import EventEmitter from 'events';
2 1
 
3
-import JitsiConference from '../../JitsiConference';
4 2
 import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
5 3
 import browser from '../browser';
4
+import Listenable from '../util/Listenable';
6 5
 
7 6
 import Statistics from './statistics';
8 7
 
9 8
 /**
10 9
  * Mock object to be used in place of a real conference.
11 10
  *
12
- * @constructor
13 11
  */
14
-function MockConference() {
15
-    this.eventEmitter = new EventEmitter();
12
+class MockConference extends Listenable {
13
+    /**
14
+     * constructor
15
+     */
16
+    constructor() {
17
+        super();
18
+        this.options = {
19
+            config: {}
20
+        };
21
+    }
16 22
 }
17
-MockConference.prototype = Object.create(JitsiConference.prototype);
18
-MockConference.prototype.constructor = JitsiConference;
19 23
 
20 24
 describe('PerformanceObserverStats', () => {
25
+    let mockConference, statistics;
26
+
21 27
     beforeEach(() => {
22 28
         // works only on chrome.
23 29
         spyOn(browser, 'isChrome').and.returnValue(true);
30
+        mockConference = new MockConference();
31
+        Statistics.init({ longTasksStatsInterval: 1000 });
32
+        statistics = new Statistics();
33
+        jasmine.clock().install();
24 34
     });
25 35
 
26
-    it('Emits performance stats every sec', () => {
27
-        const mockConference = new MockConference();
28
-        const statistics = new Statistics();
29
-
36
+    it('Conference events start/stop observer', () => {
30 37
         statistics.attachLongTasksStats(mockConference);
31
-
32 38
         const startObserverSpy = spyOn(statistics.performanceObserverStats, 'startObserver');
33 39
         const stopObserverSpy = spyOn(statistics.performanceObserverStats, 'stopObserver');
34
-        const addNextSpy = spyOn(statistics.performanceObserverStats.stats, 'addNext');
35 40
 
36 41
         mockConference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
37 42
         expect(startObserverSpy).toHaveBeenCalled();
38
-        expect(statistics.performanceObserverStats.getLongTasksStats()).toBeTruthy();
39
-
40
-        setTimeout(() => {
41
-            expect(addNextSpy).toHaveBeenCalled();
42
-        }, 1000);
43 43
 
44 44
         mockConference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
45 45
         expect(stopObserverSpy).toHaveBeenCalled();
46 46
     });
47
+
48
+    it('Emits long tasks stats every sec', () => {
49
+        statistics.attachLongTasksStats(mockConference);
50
+        statistics.performanceObserverStats.eventEmitter = {
51
+            // eslint-disable-next-line no-empty-function
52
+            emit: () => {}
53
+        };
54
+        statistics.performanceObserverStats.startObserver();
55
+        const eventEmitSpy = spyOn(statistics.performanceObserverStats.eventEmitter, 'emit');
56
+
57
+        expect(statistics.performanceObserverStats.getLongTasksStats()).toBeTruthy();
58
+        expect(eventEmitSpy).not.toHaveBeenCalled();
59
+
60
+        jasmine.clock().tick(1000);
61
+        expect(eventEmitSpy).toHaveBeenCalled();
62
+    });
63
+
64
+    afterEach(() => {
65
+        jasmine.clock().uninstall();
66
+    });
47 67
 });

Loading…
取消
儲存