Skip to content

Commit 766b23a

Browse files
committed
Support parallel Karma instances on CircleCI
1 parent 9696ff8 commit 766b23a

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ jobs:
133133
environment:
134134
BROWSER_STACK_USERNAME: "angularteam1"
135135
BROWSER_STACK_ACCESS_KEY: "CaXMeMHD9pr5PHg8N7Jq"
136+
parallelism: 2
136137
steps:
137138
- *checkout_code
138139
- *restore_cache

scripts/browserstack/start-tunnel.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ rm ${tunnelFileName}
3131
ARGS=""
3232

3333
if [ ! -z "${CIRCLE_BUILD_NUM}" ]; then
34-
ARGS="${ARGS} --local-identifier ${CIRCLE_BUILD_NUM}"
34+
ARGS="${ARGS} --local-identifier ${CIRCLE_BUILD_NUM}-${CIRCLE_NODE_INDEX}"
3535
fi
3636

3737
echo "Starting Browserstack Local in the background, logging into: ${tunnelLogFile}"

test/karma.conf.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ module.exports = (config) => {
101101
});
102102

103103
if (process.env['CIRCLECI']) {
104-
const tunnelIdentifier = process.env['CIRCLE_BUILD_NUM'];
104+
const instanceIndex = Number(process.env['CIRCLE_NODE_INDEX']);
105+
const maxParallelInstances = Number(process.env['CIRCLE_NODE_TOTAL']);
106+
const tunnelIdentifier = `${process.env['CIRCLE_BUILD_NUM']}-${instanceIndex}`;
105107
const buildIdentifier = `angular-material-${tunnelIdentifier}`;
106108
const testPlatform = process.env['TEST_PLATFORM'];
107109

@@ -110,8 +112,13 @@ module.exports = (config) => {
110112
config.browserStack.tunnelIdentifier = tunnelIdentifier;
111113
}
112114

113-
// Configure Karma launch the browsers that belong to the given test platform.
114-
config.browsers = platformMap[testPlatform];
115+
const platformBrowsers = platformMap[testPlatform];
116+
const browserInstanceChunks = splitBrowsersIntoInstances(
117+
platformBrowsers, maxParallelInstances);
118+
119+
// Configure Karma to launch the browsers that belong to the given test platform
120+
// and instance.
121+
config.browsers = browserInstanceChunks[instanceIndex];
115122
}
116123

117124
if (process.env['TRAVIS']) {
@@ -150,3 +157,21 @@ module.exports = (config) => {
150157
config.browsers = platformMap[platform];
151158
}
152159
};
160+
161+
/**
162+
* Splits the specified browsers into a maximum amount of chunks. The chunk of browsers
163+
* are being created deterministically and therefore we get reproducible tests when executing
164+
* the same CircleCI instance multiple times.
165+
*/
166+
function splitBrowsersIntoInstances(browsers, maxInstances) {
167+
let chunks = [];
168+
let assignedBrowsers = 0;
169+
170+
for (let i = 0; i < maxInstances; i++) {
171+
const chunkSize = Math.floor((browsers.length - assignedBrowsers) / (maxInstances - i));
172+
chunks[i] = browsers.slice(assignedBrowsers, assignedBrowsers + chunkSize);
173+
assignedBrowsers += chunkSize;
174+
}
175+
176+
return chunks;
177+
}

0 commit comments

Comments
 (0)