1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { TraficEnum } from '../../../../../src/trace/database/data-trafic/utils/QueryEnum';
17import {
18  chartHiperfProcessData10MSProtoSql, chartHiperfProcessDataProtoSql, hiperfProcessDataReceiver
19} from '../../../../../src/trace/database/data-trafic/hiperf/HiperfProcessDataReceiver';
20
21describe('HiperfProcess Test', () => {
22  let data1;
23  let data2;
24  let proc1;
25  let proc2;
26
27  beforeEach(() => {
28    data1 = {
29      params: {
30        trafic: TraficEnum.ProtoBuffer,
31        sharedArrayBuffers: {},
32        drawType: -2,
33        endNS: 49171193732,
34        intervalPerf: 1,
35        maxCpuCount: -1,
36        pid: 11,
37        recordEndNS: 30418971157414,
38        recordStartNS: 30369799963682,
39        scale: 2000000000,
40        startNS: 0,
41        width: 1407,
42      },
43    };
44    data2 = {
45      params: {
46        trafic: TraficEnum.ProtoBuffer,
47        sharedArrayBuffers: {},
48        drawType: -2,
49        endNS: 10360520695.855345,
50        intervalPerf: 1,
51        maxCpuCount: -1,
52        pid: 11,
53        recordEndNS: 30418971157414,
54        recordStartNS: 30369799963682,
55        scale: 20000000,
56        startNS: 9901354882.564587,
57        width: 888,
58      },
59    };
60    proc1 = jest.fn((sql) => [
61      { hiperfData: { callchainId: 262, eventCount: 14, eventTypeId: 1, sampleCount: 1, startNs: 130000000 } },
62      { hiperfData: { callchainId: 422, eventCount: 24, eventTypeId: 2, sampleCount: 1, startNs: 170000000 } },
63    ]);
64    proc2 = jest.fn((sql) => [
65      { hiperfData: { callchainId: 12515, eventCount: 191077, eventTypeId: 1, sampleCount: 1, startNs: 10017921673 } },
66      { hiperfData: { callchainId: 94, eventCount: 140815, eventTypeId: 1, sampleCount: 1, startNs: 10022069465 } },
67    ]);
68  });
69  it('HiperfProcessReceiverTest01', () => {
70    expect(chartHiperfProcessData10MSProtoSql(data1.params)).toBeTruthy();
71    expect(chartHiperfProcessDataProtoSql(data2.params)).toBeTruthy();
72  });
73  it('HiperfProcessReceiverTest02', () => {
74    let mockPostMessage = jest.fn();
75    global.postMessage = mockPostMessage;
76    hiperfProcessDataReceiver(data1, proc1);
77    expect(mockPostMessage).toHaveBeenCalledTimes(1);
78  });
79  it('HiperfProcessReceiverTest03', () => {
80    let mockPostMessage = jest.fn();
81    global.postMessage = mockPostMessage;
82    hiperfProcessDataReceiver(data2, proc2);
83    expect(mockPostMessage).toHaveBeenCalledTimes(1);
84  });
85});
86