Skip to content

Commit 90e5d0d

Browse files
authored
Improve iterationCount and worstCaseCount params parsing (#215)
- Add _parseOneOf helper - Add unittest for iteration count and worst-case count parsing - Use _parseOneOf for testList as well
1 parent 3db87be commit 90e5d0d

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

params.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,36 @@ class Params {
8282
this.startDelay = 100;
8383
}
8484

85-
for (const paramKey of ["tag", "tags", "test", "tests"]) {
86-
this.testList = this._parseTestListParam(sourceParams, paramKey);
87-
}
88-
89-
this.testIterationCount = this._parseIntParam(sourceParams, "iterationCount", 1);
90-
this.testWorstCaseCount = this._parseIntParam(sourceParams, "worstCaseCount", 1);
85+
this.testList = this._parseOneOf(sourceParams, ["testList", "tag", "tags", "test", "tests"], this._parseTestListParam);
86+
this.testIterationCount = this._parseOneOf(sourceParams, ["testIterationCount", "iterationCount", "iterations" ], this._parseIntParam, 1);
87+
this.testWorstCaseCount = this._parseOneOf(sourceParams, ["testWorstCaseCount", "worstCaseCount", "worst"], this._parseIntParam, 1);
9188

9289
const unused = Array.from(sourceParams.keys());
9390
if (unused.length > 0)
9491
console.error("Got unused source params", unused);
9592
}
9693

94+
_parseOneOf(sourceParams, paramKeys, parseFunction, ...args) {
95+
const defaultParamKey = paramKeys[0]
96+
let result = undefined;
97+
let parsedParamKey = undefined;
98+
for (const paramKey of paramKeys) {
99+
if (!sourceParams.has(paramKey)) {
100+
continue;
101+
}
102+
const parseResult = parseFunction.call(this, sourceParams, paramKey, ...args);
103+
if (parsedParamKey) {
104+
throw new Error(`Cannot parse ${paramKey}, overriding previous "${parsedParamKey}" value ${JSON.stringify(result)} with ${JSON.stringify(parseResult)}`)
105+
}
106+
parsedParamKey = paramKey;
107+
result = parseResult;
108+
}
109+
if (!parsedParamKey) {
110+
return DefaultJetStreamParams[defaultParamKey];
111+
}
112+
return result;
113+
}
114+
97115
_parseTestListParam(sourceParams, key) {
98116
if (!sourceParams.has(key))
99117
return this.testList;
@@ -108,9 +126,6 @@ class Params {
108126
}
109127
testList = testList.map(each => each.trim());
110128
sourceParams.delete(key);
111-
if (this.testList.length > 0 && testList.length > 0) {
112-
throw new Error(`Overriding previous testList='${this.testList.join()}' with ${key} url-parameter.`);
113-
}
114129
return testList;
115130
}
116131

tests/unit-tests.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function assertThrows(message, func) {
2828
} catch (e) {
2929
didThrow = true;
3030
}
31-
assertTrue(didThrow, message);
31+
assertTrue(didThrow, `Test did not throw: ${message}`);
3232
}
3333

3434
(function testTagsAreLowerCaseStrings() {
@@ -276,3 +276,20 @@ async function testStartupBenchmarkInnerTests() {
276276
}
277277
);
278278
})();
279+
280+
281+
(function testParseIterationCount() {
282+
assertThrows("Cannot parse negative iterationCounts",
283+
() => {
284+
const sourceParams = new Map(Object.entries({ iterationCount: -123, }));
285+
new Params(sourceParams);
286+
});
287+
assertThrows("Cannot parse multiple iterationCounts",
288+
() => {
289+
const sourceParams = new Map(Object.entries({ iterationCount: 123, testIterationCount: 10 }));
290+
new Params(sourceParams);
291+
});
292+
let sourceParams = new Map(Object.entries({ iterationCount: 123 }));
293+
let params = new Params(sourceParams);
294+
assertEquals(params.testIterationCount, 123);
295+
})();

0 commit comments

Comments
 (0)