Skip to content

Instantly share code, notes, and snippets.

@gastonfournier
Created April 23, 2024 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gastonfournier/c20a1b57df3af69c7590c37c0dd2a037 to your computer and use it in GitHub Desktop.
Save gastonfournier/c20a1b57df3af69c7590c37c0dd2a037 to your computer and use it in GitHub Desktop.
Unleash roll up&down consistency test
// you can run unleash locally with https://docs.getunleash.io/quickstart#i-want-to-run-unleash-locally
// create a toggle named flag-1 and add the rollout strategy sticky on userId
// then play around rolling up and down.
// Note: to run this you'll need to have an npm project (npm init) and add unleash dependency (npm i unleash-client)
import { initialize } from 'unleash-client';
const unleash = initialize({
url: 'http://localhost:4242',
appName: 'my-node-name',
refreshInterval: 200,
environment: 'development',
customHeaders: { Authorization: 'default:development.xxx' },
instanceId: 'my-unique-instance-id',
});
let done = false;
function checkFlags() {
const flag = 'flag-1';
const flagResults = {};
for (let i = 0; i < 10; i++) {
const userId = `user-${i}`;
const enabled = unleash.isEnabled(flag, {
userId: `213${i}`,
currentTime: new Date().getTime(),
}, false);
flagResults[userId] = enabled;
}
let log = "";
for (const userId in flagResults) {
log += `${userId}\t`;
}
log += `\n`;
for (const userId in flagResults) {
log += `${flagResults[userId]}\t`;
}
console.log(log);
// set a timeout to execute this function again
if (!done) {
setTimeout(checkFlags, 1000);
}
}
unleash.on('ready', async () => {
checkFlags();
});
process.on('SIGINT', () => {
done = true;
unleash.destroy();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment