Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue with first variation having weight of 0 #273

Merged
merged 6 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions examples/example-1/features/pricing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
description: Testing two variations with first one having weight of 0
tags:
- checkout

bucketBy: userId

variations:
- value: control
weight: 0

- value: treatment
weight: 100

environments:
staging:
rules:
- key: "1"
segments: "*"
percentage: 100
production:
rules:
- key: "1"
segments: germany
percentage: 100
- key: "2"
segments: "*"
percentage: 0
21 changes: 21 additions & 0 deletions examples/example-1/tests/pricing.spec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
feature: pricing
assertions:
- at: 5
environment: staging
context:
country: nl
expectedToBeEnabled: true
expectedVariation: treatment

- at: 10
environment: production
context:
country: nl
expectedToBeEnabled: false

- at: 20
environment: production
context:
country: de
expectedToBeEnabled: true
expectedVariation: treatment
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is failing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now fixed.

4 changes: 4 additions & 0 deletions packages/core/src/builder/allocator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export function getUpdatedAvailableRangesAfterFilling(
availableRanges: Range[],
fill: Percentage,
): Range[] {
if (fill === 0) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was the fix. this. this little if condition.

return availableRanges;
}

const result: Range[] = [];

let remaining = fill;
Expand Down
259 changes: 259 additions & 0 deletions packages/core/src/builder/traffic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,265 @@ describe("core: Traffic", function () {
]);
});

it("should allocate traffic for 0-100 weight on two variations", function () {
const result = getTraffic(
// parsed variations from YAML
[
{
value: "control",
weight: 0,
},
{
value: "treatment",
weight: 100,
},
],

// parsed rollouts from YAML
[
{
key: "1",
segments: "*",
percentage: 80,
},
],

// existing feature from previous release
undefined,
);

expect(result).toEqual([
{
key: "1",
segments: "*",
percentage: 80000,
allocation: [
// should be filtered out automatically
// {
// variation: "control",
// range: [0, 0],
// },

{
Copy link
Member Author

@fahad19 fahad19 Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is failing. the allocation array is mistakenly generated as an empty array.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now fixed.

variation: "treatment",
range: [0, 80000],
},
],
},
]);
});

it("should allocate traffic for 0-100 weight on two variations, with rule percentage going from 80% to 100%", function () {
const result = getTraffic(
// parsed variations from YAML
[
{
value: "control",
weight: 0,
},
{
value: "treatment",
weight: 100,
},
],

// parsed rollouts from YAML
[
{
key: "1",
segments: "*",
percentage: 100,
},
],

// existing feature from previous release
{
variations: [
{
value: "control",
weight: 0,
},
{
value: "treatment",
weight: 80,
},
],
traffic: [
{
key: "1",
percentage: 80000,
allocation: [
{
variation: "treatment",
range: [0, 80000],
},
],
},
],
},
);

expect(result).toEqual([
{
key: "1",
segments: "*",
percentage: 100000,
allocation: [
// should be filtered out automatically
// {
// variation: "control",
// range: [0, 0],
// },

{
variation: "treatment",
range: [0, 100000],
},
],
},
]);
});

it("should allocate traffic for 0-100 weight on two variations, with rule percentage going from 100% to 80%", function () {
const result = getTraffic(
// parsed variations from YAML
[
{
value: "control",
weight: 0,
},
{
value: "treatment",
weight: 100,
},
],

// parsed rollouts from YAML
[
{
key: "1",
segments: "*",
percentage: 80,
},
],

// existing feature from previous release
{
variations: [
{
value: "control",
weight: 0,
},
{
value: "treatment",
weight: 80,
},
],
traffic: [
{
key: "1",
percentage: 100000,
allocation: [
{
variation: "treatment",
range: [0, 100000],
},
],
},
],
},
);

expect(result).toEqual([
{
key: "1",
segments: "*",
percentage: 80000,
allocation: [
// should be filtered out automatically
// {
// variation: "control",
// range: [0, 0],
// },

{
variation: "treatment",
range: [0, 80000],
},
],
},
]);
});

it("should allocate traffic for existing variations state of 0-100 weights", function () {
const result = getTraffic(
// parsed variations from YAML
[
{
value: "control",
weight: 50,
},
{
value: "treatment",
weight: 50,
},
],

// parsed rollouts from YAML
[
{
key: "1",
segments: "*",
percentage: 80,
},
],

// existing feature from previous release
{
variations: [
{
value: "control",
weight: 0,
},
{
value: "treatment",
weight: 80,
},
],
traffic: [
{
key: "1",
percentage: 80000,
allocation: [
{
variation: "treatment",
range: [0, 80000],
},
],
},
],
},
);

expect(result).toEqual([
{
key: "1",
segments: "*",
percentage: 80000,
allocation: [
{
variation: "control",
range: [0, 40000],
},
{
variation: "treatment",
range: [40000, 80000],
},
],
},
]);
});

it("should allocate traffic for 50-50 weight on two variations", function () {
const result = getTraffic(
// parsed variations from YAML
Expand Down