1414 * limitations under the License.
1515 */
1616
17- import { adjustPerfTimeOrigin , getDateForPerfTime , getIsoDateStrForPerfTime , getPerfTimeOrigin , TEST_ONLY } from '../src/common/time-util' ;
17+ import { getDateForPerfTime , getIsoDateStrForPerfTime , getPerfTimeOrigin } from '../src/common/time-util' ;
1818import { mockGetterOrValue , restoreGetterOrValue } from './util' ;
1919
2020describe ( 'time utils' , ( ) => {
@@ -29,112 +29,23 @@ describe('time utils', () => {
2929 describe ( 'getPerfTimeOrigin' , ( ) => {
3030 it ( 'returns `performance.timeOrigin` if set' , ( ) => {
3131 mockGetterOrValue ( performance , 'timeOrigin' , 1548000000000 ) ;
32+
3233 expect ( getPerfTimeOrigin ( ) ) . toBe ( 1548000000000 ) ;
3334 } ) ;
3435
3536 it ( 'calculates via polyfill if `performance.timeOrigin` unset' , ( ) => {
3637 mockGetterOrValue ( performance , 'timeOrigin' , undefined ) ;
3738 spyOn ( Date , 'now' ) . and . returnValue ( 1548000009999 ) ;
3839 spyOn ( performance , 'now' ) . and . returnValue ( 9999 ) ;
39- } ) ;
40- } ) ;
41-
42- describe ( 'adjustPerfTimeOrigin' , ( ) => {
43- function createNavTiming ( { requestStart, responseStart} :
44- { requestStart : number , responseStart : number } ) :
45- PerformanceNavigationTiming {
46- return {
47- // This are used by the time adjustment function below.
48- requestStart, // Client start time in performance clock millis.
49- responseStart, // Client end time in performance clock millis.
50- // These are needed to satisfy the interface.
51- connectEnd : 0 ,
52- connectStart : 0 ,
53- decodedBodySize : 0 ,
54- domComplete : 0 ,
55- domContentLoadedEventEnd : 0 ,
56- domContentLoadedEventStart : 0 ,
57- domInteractive : 0 ,
58- domainLookupEnd : 0 ,
59- domainLookupStart : 0 ,
60- duration : 0 ,
61- encodedBodySize : 0 ,
62- entryType : '' ,
63- fetchStart : 0 ,
64- initiatorType : '' ,
65- loadEventEnd : 0 ,
66- loadEventStart : 0 ,
67- name : '' ,
68- nextHopProtocol : '' ,
69- redirectCount : 0 ,
70- redirectEnd : 0 ,
71- redirectStart : 0 ,
72- responseEnd : 0 ,
73- secureConnectionStart : 0 ,
74- startTime : 0 ,
75- toJSON : ( ) => ( { } ) ,
76- transferSize : 0 ,
77- type : 'navigate' ,
78- unloadEventEnd : 0 ,
79- unloadEventStart : 0 ,
80- workerStart : 0 ,
81- } ;
82- }
83-
84- const CLIENT_TIME_ORIGIN = 1548000000000 ;
85- beforeEach ( ( ) => {
86- mockGetterOrValue ( performance , 'timeOrigin' , CLIENT_TIME_ORIGIN ) ;
87- } ) ;
88- afterEach ( ( ) => {
89- TEST_ONLY . clearAdjustedPerfTime ( ) ;
90- } ) ;
91-
92- it ( 'keeps client time origin if server time longer than client' , ( ) => {
93- // Client nav fetch duration is 5ms
94- const perfNavTiming =
95- createNavTiming ( { requestStart : 10.1 , responseStart : 15.1 } ) ;
96-
97- // Server nav fetch duration is 10ms
98- adjustPerfTimeOrigin (
99- 1548000001000.2 , /* serverNavFetchDuration */ 10 , perfNavTiming ) ;
10040
101- expect ( getPerfTimeOrigin ( ) ) . toBe ( CLIENT_TIME_ORIGIN ) ;
102- } ) ;
103-
104- it ( 'adjusts origin to center server span in client span' , ( ) => {
105- const clientNavFetchStartInPerfTime = 10 ;
106- const clientNavFetchEndInPerfTime = 18 ;
107- const perfNavTiming = createNavTiming ( {
108- requestStart : clientNavFetchStartInPerfTime ,
109- responseStart : clientNavFetchEndInPerfTime ,
110- } ) ;
111- const serverNavFetchStartEpochMillis = 1500000001000 ; // Epoch millis.
112- const serverNavFetchDuration = 6 ; // Duration millis
113-
114- adjustPerfTimeOrigin (
115- serverNavFetchStartEpochMillis , serverNavFetchDuration ,
116- perfNavTiming ) ;
117-
118- // Calculations to make the expectation clearer:
119- const clientNavFetchDuration =
120- clientNavFetchEndInPerfTime - clientNavFetchStartInPerfTime ;
121- expect ( clientNavFetchDuration ) . toBe ( 8 ) ; // Duration millis
122- const networkTime = clientNavFetchDuration - serverNavFetchDuration ;
123- expect ( networkTime ) . toBe ( 2 ) ; // Duration millis
124- const clientNavStartInEpochMillis =
125- serverNavFetchStartEpochMillis - networkTime / 2 ;
126- expect ( clientNavStartInEpochMillis ) . toBe ( 1500000000999 ) ;
127- const perfOriginInEpochMillis =
128- clientNavStartInEpochMillis - clientNavFetchStartInPerfTime ;
129- expect ( perfOriginInEpochMillis ) . toBe ( 1500000000989 ) ;
130-
131- expect ( getPerfTimeOrigin ( ) ) . toBe ( perfOriginInEpochMillis ) ;
41+ expect ( getPerfTimeOrigin ( ) ) . toBe ( 1548000000000 ) ;
13242 } ) ;
13343 } ) ;
13444
13545 describe ( 'getDateForPerfTime' , ( ) => {
13646 it ( 'calculates date for perf time based on time origin' , ( ) => {
13747 mockGetterOrValue ( performance , 'timeOrigin' , 1548000000000 ) ;
48+
13849 expect ( getDateForPerfTime ( 999.6 ) . getTime ( ) ) . toBe ( 1548000000999 ) ;
13950 } ) ;
14051 } ) ;
@@ -148,6 +59,7 @@ describe('time utils', () => {
14859
14960 it ( 'accurately combines milliseconds from origin and perf times' , ( ) => {
15061 mockGetterOrValue ( performance , 'timeOrigin' , 1535683887441.586 ) ;
62+
15163 expect ( getIsoDateStrForPerfTime ( 658867.8000000073 ) )
15264 . toEqual ( '2018-08-31T03:02:26.309385938Z' ) ;
15365 } ) ;
0 commit comments