-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
74 lines (64 loc) · 1.9 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import test from "ava";
import decider from ".";
import { times, countBy, mapValues } from "lodash";
import server from "./server"
import cookie from "cookie"
require("isomorphic-fetch");
test.before(async t => {
await server.on('listened', () => Promise.resolve())
});
const generateAB = (percentage) => {
const total = 10000;
const testExp = {
a: {
url: "http://",
weight: percentage
},
b: {
url: "http://",
weight: 100 - percentage
}
};
let hitsCounter = 0;
for (let counter = 0; counter < total; counter++) {
if (decider(testExp).name == "a") hitsCounter++;
}
return hitsCounter;
}
const generateHitsTest = t => (percentage) => {
const expectedHits = percentage * 100;
const exactHits = generateAB(percentage);
t.truthy(
exactHits > expectedHits * 0.90 &&
exactHits < expectedHits * 1.1
);
}
test("weights work properly in the decider process", t => {
t.plan(4);
generateHitsTest(t)(80)
generateHitsTest(t)(20)
generateHitsTest(t)(5)
generateHitsTest(t)(3)
});
const generateNHits = (t) => (hits) => times(hits, async () => {
const res = await fetch(server.hostname)
const setCookieHeader = res.headers.get('set-cookie')
const variantCookie = decodeURIComponent(cookie.parse(setCookieHeader).variant)
const responseExperimentName = variantCookie.split("@")[0]
const responseExperimentHash = variantCookie.split("@")[1]
return responseExperimentName
})
test("decider server balance weight correctly", async t => {
const HITS_PER_PAGE = 100;
const PAGES = 20;
const TOTAL_HITS = HITS_PER_PAGE * PAGES;
let responses = []
for (let t = 0; t < PAGES; t++) {
const newReponses = await Promise.all(generateNHits(t)(HITS_PER_PAGE))
responses = responses.concat(newReponses)
}
const totals = countBy(responses)
const percentages = mapValues(totals, t => t * 100 / TOTAL_HITS)
t.log(percentages)
t.truthy(true)
});