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 {
17  chartCpuDataProtoSql,
18  chartCpuDataProtoSqlMem, cpuDataReceiver
19} from '../../../../src/trace/database/data-trafic/CpuDataReceiver';
20
21describe('CpuDataReceiver Test', () => {
22  let data;
23  let proc;
24  let data2;
25  beforeEach(() => {
26    data = {
27      id: 'b1ba1ace-8f2b-4ce4-b27e-7a3bf2ff8499',
28      name: 0,
29      action: 'exec-proto',
30      params: {
31        cpu: 0,
32        startNS: 0,
33        endNS: 74946716780,
34        recordStartNS: 1395573006744,
35        recordEndNS: 1470519723524,
36        width: 491,
37        t: 1703729410566,
38        trafic: 0,
39        sharedArrayBuffers: {
40          processId: {},
41          id: {},
42          tid: {},
43          cpu: {},
44          dur: {},
45          startTime: {},
46          argSetId: {}
47        }
48      }
49    };
50     data2 = {
51      id: 'b1ba1ace-8f2b-4ce4-b27e-7a3bf2ff8499',
52      name: 0,
53      action: 'exec-proto',
54      params: {
55        cpu: 0,
56        startNS: 0,
57        endNS: 74946716780,
58        recordStartNS: 1395573006744,
59        recordEndNS: 1470519723524,
60        width: 491,
61        t: 1703729410566,
62        trafic: 1,
63        sharedArrayBuffers: {
64          processId: {},
65          id: {},
66          tid: {},
67          cpu: {},
68          dur: {},
69          startTime: {},
70          argSetId: {}
71        }
72      }
73    };
74    proc = jest.fn((sql) => [
75      {CpuData: {id: 4, startTime: 4.4, processId: 40, tid: 400, cpu: 0, argSetId: 1, dur: 40000}},
76      {CpuData: {id: 5, startTime: 5.5, processId: 50, tid: 500, cpu: 0, argSetId: 2, dur: 50000}},
77    ]);
78  });
79  it('CpuDataReceiverTest01 ', function () {
80    const args = {
81      recordStartNS: 1000,
82      endNS: 3000,
83      startNS: 2000,
84      width: 10,
85      cpu:0,
86    };
87    expect(chartCpuDataProtoSql(args)).toBeTruthy();
88    expect(chartCpuDataProtoSqlMem(args)).toBeTruthy();
89  });
90  it('CpuDataReceiverTest02 ', function () {
91    const mockPostMessage = jest.fn();
92    global.postMessage = mockPostMessage;
93    cpuDataReceiver(data, proc);
94    cpuDataReceiver(data2,proc)
95    expect(mockPostMessage).toHaveBeenCalledTimes(2);
96  });
97});