|
15 | 15 | */ |
16 | 16 |
|
17 | 17 | import {adjustPerfTimeOrigin, getDateForPerfTime, getIsoDateStrForPerfTime, getPerfTimeOrigin, TEST_ONLY} from '../src/common/time-util'; |
| 18 | +import {mockGetterOrValue, restoreGetterOrValue} from './util'; |
18 | 19 |
|
19 | | -describe('getPerfTimeOrigin', () => { |
20 | | - it('returns `performance.timeOrigin` if set', () => { |
21 | | - spyOnProperty(performance, 'timeOrigin').and.returnValue(1548000000000); |
22 | | - |
23 | | - expect(getPerfTimeOrigin()).toBe(1548000000000); |
24 | | - }); |
25 | | - |
26 | | - it('calculates via polyfill if `performance.timeOrigin` unset', () => { |
27 | | - spyOnProperty(performance, 'timeOrigin').and.returnValue(undefined); |
28 | | - spyOn(Date, 'now').and.returnValue(1548000009999); |
29 | | - spyOn(performance, 'now').and.returnValue(9999); |
30 | | - |
31 | | - expect(getPerfTimeOrigin()).toBe(1548000000000); |
32 | | - }); |
33 | | -}); |
34 | | - |
35 | | -describe('adjustPerfTimeOrigin', () => { |
36 | | - const CLIENT_TIME_ORIGIN = 1548000000000; |
| 20 | +describe('time utils', () => { |
| 21 | + let realTimeOrigin: number; |
37 | 22 | beforeEach(() => { |
38 | | - spyOnProperty(performance, 'timeOrigin') |
39 | | - .and.returnValue(CLIENT_TIME_ORIGIN); |
| 23 | + realTimeOrigin = performance.timeOrigin; |
40 | 24 | }); |
41 | 25 | afterEach(() => { |
42 | | - TEST_ONLY.clearAdjustedPerfTime(); |
| 26 | + restoreGetterOrValue(performance, 'timeOrigin', realTimeOrigin); |
43 | 27 | }); |
44 | 28 |
|
45 | | - it('keeps client time origin if no performance timing missing', () => { |
46 | | - spyOnProperty(performance, 'timing').and.returnValue(undefined); |
47 | | - adjustPerfTimeOrigin(1548000001000.2, 5.1); |
48 | | - expect(getPerfTimeOrigin()).toBe(CLIENT_TIME_ORIGIN); |
| 29 | + describe('getPerfTimeOrigin', () => { |
| 30 | + it('returns `performance.timeOrigin` if set', () => { |
| 31 | + mockGetterOrValue(performance, 'timeOrigin', 1548000000000); |
| 32 | + expect(getPerfTimeOrigin()).toBe(1548000000000); |
| 33 | + }); |
| 34 | + |
| 35 | + it('calculates via polyfill if `performance.timeOrigin` unset', () => { |
| 36 | + mockGetterOrValue(performance, 'timeOrigin', undefined); |
| 37 | + spyOn(Date, 'now').and.returnValue(1548000009999); |
| 38 | + spyOn(performance, 'now').and.returnValue(9999); |
| 39 | + }); |
49 | 40 | }); |
50 | 41 |
|
51 | | - it('keeps client time origin if server time longer than client', () => { |
52 | | - // Client nav fetch duration is 5ms |
53 | | - spyOnProperty(performance.timing, 'requestStart').and.returnValue(10.1); |
54 | | - spyOnProperty(performance.timing, 'responseStart').and.returnValue(15.1); |
55 | | - |
56 | | - // Server nav fetch duration is 10ms |
57 | | - adjustPerfTimeOrigin(1548000001000.2, /* serverNavFetchDuration */ 10); |
58 | | - |
59 | | - expect(getPerfTimeOrigin()).toBe(CLIENT_TIME_ORIGIN); |
| 42 | + describe('adjustPerfTimeOrigin', () => { |
| 43 | + const CLIENT_TIME_ORIGIN = 1548000000000; |
| 44 | + beforeEach(() => { |
| 45 | + mockGetterOrValue(performance, 'timeOrigin', CLIENT_TIME_ORIGIN); |
| 46 | + }); |
| 47 | + afterEach(() => { |
| 48 | + TEST_ONLY.clearAdjustedPerfTime(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('keeps client time origin if performance timing missing', () => { |
| 52 | + spyOnProperty(performance, 'timing').and.returnValue(undefined); |
| 53 | + adjustPerfTimeOrigin(1548000001000.2, 5.1); |
| 54 | + expect(getPerfTimeOrigin()).toBe(CLIENT_TIME_ORIGIN); |
| 55 | + }); |
| 56 | + |
| 57 | + it('keeps client time origin if server time longer than client', () => { |
| 58 | + // Client nav fetch duration is 5ms |
| 59 | + spyOnProperty(performance.timing, 'requestStart').and.returnValue(10.1); |
| 60 | + spyOnProperty(performance.timing, 'responseStart').and.returnValue(15.1); |
| 61 | + |
| 62 | + // Server nav fetch duration is 10ms |
| 63 | + adjustPerfTimeOrigin(1548000001000.2, /* serverNavFetchDuration */ 10); |
| 64 | + |
| 65 | + expect(getPerfTimeOrigin()).toBe(CLIENT_TIME_ORIGIN); |
| 66 | + }); |
| 67 | + |
| 68 | + it('adjusts origin to center server span in client span', () => { |
| 69 | + const clientNavFetchStartInPerfTime = 10; // Performance clock millis. |
| 70 | + spyOnProperty(performance.timing, 'requestStart') |
| 71 | + .and.returnValue(clientNavFetchStartInPerfTime); |
| 72 | + const clientNavFetchEndInPerfTime = 18; // Performance clock millis. |
| 73 | + spyOnProperty(performance.timing, 'responseStart') |
| 74 | + .and.returnValue(clientNavFetchEndInPerfTime); |
| 75 | + |
| 76 | + const serverNavFetchStartEpochMillis = 1500000001000; // Epoch millis. |
| 77 | + const serverNavFetchDuration = 6; // Duration millis |
| 78 | + adjustPerfTimeOrigin( |
| 79 | + serverNavFetchStartEpochMillis, serverNavFetchDuration); |
| 80 | + |
| 81 | + // Calculations to make the expectation clearer: |
| 82 | + const clientNavFetchDuration = |
| 83 | + clientNavFetchEndInPerfTime - clientNavFetchStartInPerfTime; |
| 84 | + expect(clientNavFetchDuration).toBe(8); // Duration millis |
| 85 | + const networkTime = clientNavFetchDuration - serverNavFetchDuration; |
| 86 | + expect(networkTime).toBe(2); // Duration millis |
| 87 | + const clientNavStartInEpochMillis = |
| 88 | + serverNavFetchStartEpochMillis - networkTime / 2; |
| 89 | + expect(clientNavStartInEpochMillis).toBe(1500000000999); |
| 90 | + const perfOriginInEpochMillis = |
| 91 | + clientNavStartInEpochMillis - clientNavFetchStartInPerfTime; |
| 92 | + expect(perfOriginInEpochMillis).toBe(1500000000989); |
| 93 | + |
| 94 | + expect(getPerfTimeOrigin()).toBe(perfOriginInEpochMillis); |
| 95 | + }); |
60 | 96 | }); |
61 | 97 |
|
62 | | - it('adjusts origin to center server span in client span', () => { |
63 | | - const clientNavFetchStartInPerfTime = 10; // Performance clock millis. |
64 | | - spyOnProperty(performance.timing, 'requestStart') |
65 | | - .and.returnValue(clientNavFetchStartInPerfTime); |
66 | | - const clientNavFetchEndInPerfTime = 18; // Performance clock millis. |
67 | | - spyOnProperty(performance.timing, 'responseStart') |
68 | | - .and.returnValue(clientNavFetchEndInPerfTime); |
69 | | - |
70 | | - const serverNavFetchStartEpochMillis = 1500000001000; // Epoch millis. |
71 | | - const serverNavFetchDuration = 6; // Duration millis |
72 | | - adjustPerfTimeOrigin( |
73 | | - serverNavFetchStartEpochMillis, serverNavFetchDuration); |
74 | | - |
75 | | - // Calculations to make the expectation clearer: |
76 | | - const clientNavFetchDuration = |
77 | | - clientNavFetchEndInPerfTime - clientNavFetchStartInPerfTime; |
78 | | - expect(clientNavFetchDuration).toBe(8); // Duration millis |
79 | | - const networkTime = clientNavFetchDuration - serverNavFetchDuration; |
80 | | - expect(networkTime).toBe(2); // Duration millis |
81 | | - const clientNavStartInEpochMillis = |
82 | | - serverNavFetchStartEpochMillis - networkTime / 2; |
83 | | - expect(clientNavStartInEpochMillis).toBe(1500000000999); |
84 | | - const perfOriginInEpochMillis = |
85 | | - clientNavStartInEpochMillis - clientNavFetchStartInPerfTime; |
86 | | - expect(perfOriginInEpochMillis).toBe(1500000000989); |
87 | | - |
88 | | - expect(getPerfTimeOrigin()).toBe(perfOriginInEpochMillis); |
| 98 | + describe('getDateForPerfTime', () => { |
| 99 | + it('calculates date for perf time based on time origin', () => { |
| 100 | + mockGetterOrValue(performance, 'timeOrigin', 1548000000000); |
| 101 | + expect(getDateForPerfTime(999.6).getTime()).toBe(1548000000999); |
| 102 | + }); |
89 | 103 | }); |
90 | | -}); |
91 | | - |
92 | | -describe('getDateForPerfTime', () => { |
93 | | - it('calculates date for perf time based on time origin', () => { |
94 | | - spyOnProperty(performance, 'timeOrigin').and.returnValue(1548000000000); |
95 | | - |
96 | | - expect(getDateForPerfTime(999.6).getTime()).toBe(1548000000999); |
97 | | - }); |
98 | | -}); |
99 | | - |
100 | | -describe('getIsoDateStrForPerfTime', () => { |
101 | | - it('converts perf time to nanosecond-precise ISO date string', () => { |
102 | | - spyOnProperty(performance, 'timeOrigin').and.returnValue(1535683887001); |
103 | | - expect(getIsoDateStrForPerfTime(0.000001)) |
104 | | - .toEqual('2018-08-31T02:51:27.001000001Z'); |
105 | | - }); |
106 | | - |
107 | | - it('accurately combines milliseconds from origin and perf times', () => { |
108 | | - spyOnProperty(performance, 'timeOrigin').and.returnValue(1535683887441.586); |
109 | 104 |
|
110 | | - expect(getIsoDateStrForPerfTime(658867.8000000073)) |
111 | | - .toEqual('2018-08-31T03:02:26.309385938Z'); |
| 105 | + describe('getIsoDateStrForPerfTime', () => { |
| 106 | + it('converts perf time to nanosecond-precise ISO date string', () => { |
| 107 | + mockGetterOrValue(performance, 'timeOrigin', 1535683887001); |
| 108 | + expect(getIsoDateStrForPerfTime(0.000001)) |
| 109 | + .toEqual('2018-08-31T02:51:27.001000001Z'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('accurately combines milliseconds from origin and perf times', () => { |
| 113 | + mockGetterOrValue(performance, 'timeOrigin', 1535683887441.586); |
| 114 | + expect(getIsoDateStrForPerfTime(658867.8000000073)) |
| 115 | + .toEqual('2018-08-31T03:02:26.309385938Z'); |
| 116 | + }); |
112 | 117 | }); |
113 | 118 | }); |
0 commit comments