Skip to content

Commit

Permalink
fix: issue with first variation having weight of 0 (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
fahad19 committed May 25, 2024
1 parent 28dfe5e commit 2853529
Show file tree
Hide file tree
Showing 4 changed files with 311 additions and 0 deletions.
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
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) {
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],
// },

{
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

0 comments on commit 2853529

Please sign in to comment.