1/* 2 * Copyright (C) 2021 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 */ 15var endFlag = false; 16class PressureTest { 17 constructor(core) { 18 this.core = core; 19 this.rootSuite = this.core.getDefaultService("suite").rootSuite; 20 this.suites = []; 21 this.its = []; 22 this.times = 5; 23 this.timeout = 10; 24 this.init(); 25 } 26 27 init() { 28 if (this.rootSuite.childSuites.length > 0) { 29 for (var i in this.rootSuite.childSuites) { 30 this.getAllIt(this.rootSuite.childSuites[i]); 31 } 32 } 33 } 34 35 getAllIt(suite) { 36 if (suite.specs.length > 0) { 37 for (var i in suite.specs) { 38 var oneIt = { 39 it: suite.specs[i], 40 suite: suite 41 } 42 this.its.push(oneIt); 43 } 44 } 45 if (suite.childSuites.length > 0) { 46 for (var i in suite.childSuites) { 47 this.getAllIt(suite.childSuites[i]); 48 } 49 } 50 } 51 52 run() { 53 this.core.fireEvents('task', 'taskStart'); 54 let date = new Date(); 55 let start = date.getTime(); 56 let end = start + this.timeout * 1000; 57 if (this.core.getDefaultService('config').isSupportAsync()) { 58 while(this.times > 0 || start < end) { 59 let asyncExecute = async () => { 60 await this.runIts(); 61 } 62 this.times--; 63 date = new Date(); 64 start = date.getTime(); 65 } 66 } else { 67 while(this.times > 0 || start < end) { 68 this.runIts(); 69 this.times--; 70 date = new Date(); 71 start = date.getTime(); 72 } 73 } 74 var res = this.core.getDefaultService("suite").getSummary(); 75 this.core.fireEvents('task', 'taskDone'); 76 } 77 78 runIts() { 79 var itsTmp = []; 80 itsTmp = Array.from(this.its); 81 var lastSuite = null; 82 while (itsTmp.length > 0) { 83 var index = this.random(0, itsTmp.length - 1); 84 var it = itsTmp[index]; 85 this.runIt(it, lastSuite); 86 lastSuite = it.suite; 87 itsTmp.splice(index, 1); 88 } 89 lastSuite.runHookFunc('afterAll'); 90 } 91 92 runIt(it, lastSuite) { 93 var suite = it.suite; 94 if (lastSuite !== suite) { 95 if (lastSuite !== null) { 96 lastSuite.runHookFunc('afterAll'); 97 } 98 suite.runHookFunc('beforeAll'); 99 } 100 suite.runHookFunc('beforeEach'); 101 it.it.run(this.core); 102 suite.runHookFunc('afterEach'); 103 } 104 105 random(min, max) { 106 return parseInt(Math.random()*(max - min) + min); 107 } 108 109 setPressTimes(times) { 110 this.times = times; 111 } 112 113 setTimeout(timeout) { 114 this.timeout = timeout; 115 } 116} 117 118export {PressureTest}