Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"jest-localstorage-mock": "^2.4.3",
"js-yaml": "^3.14.0",
"lodash": "^4.17.21",
"node-fetch": "^2.6.1",
"node-fetch": "^2.6.7",
"redis-server": "1.2.2",
"rimraf": "^3.0.2",
"ts-jest": "^27.0.5",
Expand Down
5 changes: 0 additions & 5 deletions src/evaluator/condition/__tests__/engineUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ test('ENGINE / should always evaluate to "off"', () => {

expect(engineUtils.getTreatment(loggerMock, bucketingKey, seed, treatmentsMock) === 'off').toBe(true); // treatment should be 'off'


let endTime = Date.now();

console.log(`Evaluation takes ${(endTime - startTime) / 1000} seconds`);
Expand All @@ -38,28 +37,24 @@ test('ENGINE / should always evaluate to "on"', () => {
});

test('ENGINE / shouldApplyRollout - trafficAllocation 100', () => {

const shouldApplyRollout = engineUtils.shouldApplyRollout(100, 'asd', 14);

expect(shouldApplyRollout).toBe(true); // Should return true as traffic allocation is 100.
});

test('ENGINE / shouldApplyRollout - algo murmur | trafficAllocation 53 | bucket 51', () => {

const shouldApplyRollout = engineUtils.shouldApplyRollout(53, 'a', 29);

expect(shouldApplyRollout).toBe(true); // Should return true as traffic allocation is 100.
});

test('ENGINE / shouldApplyRollout - algo murmur | trafficAllocation 53 | bucket 56', () => {

const shouldApplyRollout = engineUtils.shouldApplyRollout(53, 'a', 31);

expect(shouldApplyRollout).toBe(false); // Should return false as bucket is higher than trafficAllocation.
});

test('ENGINE / shouldApplyRollout - algo murmur | trafficAllocation 1 | bucket 1', () => {

const shouldApplyRollout = engineUtils.shouldApplyRollout(1, 'aaaaaaklmnbv', -1667452163);

expect(shouldApplyRollout).toBe(true); // Should return true as bucket is higher or equal than trafficAllocation.
Expand Down
10 changes: 5 additions & 5 deletions src/sync/streaming/__tests__/mySegmentsV2utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ test('parseKeyList', () => {
addedUserKeys.forEach(userKey => {
const hash = hash64(userKey);
expect(added.has(hash.dec)).toBe(true); // key hash belongs to added list
expect(removed.has(hash.dec)).toBe(false); // t belong to removed list
expect(removed.has(hash.dec)).toBe(false); // key hash doesn\'t belong to removed list
});

removedUserKeys.forEach(userKey => {
const hash = hash64(userKey);
expect(added.has(hash.dec)).toBe(false); // t belong to added list
expect(added.has(hash.dec)).toBe(false); // key hash doesn\'t belong to added list
expect(removed.has(hash.dec)).toBe(true); // key hash belongs to removed list
});

otherUserKeys.forEach(userKey => {
const hash = hash64(userKey);
expect(added.has(hash.dec)).toBe(false); // t belong to added list
expect(removed.has(hash.dec)).toBe(false); // t belong to removed list
expect(added.has(hash.dec)).toBe(false); // key hash doesn\'t belong to added list
expect(removed.has(hash.dec)).toBe(false); // key hash doesn\'t belong to removed list
});
});
});
Expand All @@ -47,7 +47,7 @@ test('parseBitmap & isInBitmap', () => {

falseUserKeys.forEach(userKey => {
const hash = hash64(userKey);
expect(isInBitmap(actualBitmap, hash.hex)).toBe(false); // t belong to Bitmap
expect(isInBitmap(actualBitmap, hash.hex)).toBe(false); // key hash doesn\'t belong to Bitmap
});
});
});
44 changes: 44 additions & 0 deletions src/utils/__tests__/Backoff.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Backoff } from '../Backoff';

test('Backoff', (done) => {

let start = Date.now();
let backoff: Backoff;

let alreadyReset = false;
const callback = () => {
const delta = Date.now() - start;
start += delta;
const expectedMillis = Math.min(backoff.baseMillis * Math.pow(2, backoff.attempts - 1), backoff.maxMillis);

expect(delta > expectedMillis - 20 && delta < expectedMillis + 20).toBe(true); // executes callback at expected time
if (backoff.attempts <= 3) {
backoff.scheduleCall();
} else {
backoff.reset();
expect(backoff.attempts).toBe(0); // restarts attempts when `reset` called
expect(backoff.timeoutID).toBe(undefined); // restarts timeoutId when `reset` called

// init the schedule cycle or finish the test
if (alreadyReset) {
done();
} else {
alreadyReset = true;
backoff.scheduleCall();
}
}
};

backoff = new Backoff(callback);
expect(backoff.cb).toBe(callback); // contains given callback
expect(backoff.baseMillis).toBe(Backoff.DEFAULT_BASE_MILLIS); // contains default baseMillis
expect(backoff.maxMillis).toBe(Backoff.DEFAULT_MAX_MILLIS); // contains default maxMillis

const CUSTOM_BASE = 200;
const CUSTOM_MAX = 700;
backoff = new Backoff(callback, CUSTOM_BASE, CUSTOM_MAX);
expect(backoff.baseMillis).toBe(CUSTOM_BASE); // contains given baseMillis
expect(backoff.maxMillis).toBe(CUSTOM_MAX); // contains given maxMillis

expect(backoff.scheduleCall()).toBe(backoff.baseMillis); // scheduleCall returns the scheduled delay time
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { SplitIO } from '../../../types';
import { keyParser, getMatching, getBucketing } from '../index';

test('keyParser / if a string is passed as param, it should return a object', () => {
test('keyParser / if a string is passed as a param, it should return a object', () => {

const key = 'some key';
const keyParsed = keyParser(key);
Expand All @@ -24,7 +23,7 @@ test('keyParser / should return the keys configurations', () => {
expect(keyParsed.bucketingKey).toBe(bucketingKey); // matching key should be equal to bucketingKey
});

test('getMatching / if a string is passed as a param it should return a string', () => {
test('getMatching / if a string is passed as a param, it should return a string', () => {

const key = 'some key';
const keyParsed = getMatching(key);
Expand All @@ -33,7 +32,7 @@ test('getMatching / if a string is passed as a param it should return a string',
expect(keyParsed).toBe(key); // key parsed should be equal to key
});

test('getMatching / if a object is passed as a param it should return a string', () => {
test('getMatching / if a object is passed as a param, it should return a string', () => {

const key = {
matchingKey: 'some key',
Expand All @@ -46,7 +45,7 @@ test('getMatching / if a object is passed as a param it should return a string',
expect(keyParsed).toBe(key.matchingKey); // key parsed should be equal to key
});

test('getBucketing / should return undefined if a string is passed as a param and return undefined is set', () => {
test('getBucketing / if a string is passed as a param, it should return undefined', () => {

const key = 'simple key';

Expand All @@ -55,11 +54,11 @@ test('getBucketing / should return undefined if a string is passed as a param an
expect(keyParsed).toBe(undefined); // key parsed should return undefined
});

test('getBucketing / should return undefined if a string is passed as a param and return undefined is set', () => {
test('getBucketing / if a object is passed as a param, it should return the bucketingKey value', () => {

const key = { bucketingKey: 'test_buck_key' };
const key: any = { bucketingKey: 'test_buck_key' };

const keyParsed = getBucketing(key as SplitIO.SplitKeyObject);
const keyParsed = getBucketing(key);

expect(keyParsed).toBe('test_buck_key'); // key parsed should return undefined
expect(keyParsed).toBe('test_buck_key'); // key parsed should return the bucketingKey
});
132 changes: 132 additions & 0 deletions src/utils/lang/__tests__/binarySearch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
Copyright 2022 Split Software

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**/
import { binarySearch } from '../binarySearch';

test('BINARY SEARCH / given [1,3,5,7,10] as dataset look for several elements', () => {
let searchFor = binarySearch.bind(null, [1, 3, 5, 7, 10]);
let index = undefined;
let value = -1;

index = searchFor(value);
expect(index).toBe(0); // expected value 0

value++; // 0
index = searchFor(value);
expect(index).toBe(0); // expected value 0

value++; // 1
index = searchFor(value);
expect(index).toBe(0); // expected value 0

value++; // 2
index = searchFor(value);
expect(index).toBe(0); // expected value 0

value++; // 3
index = searchFor(value);
expect(index).toBe(1); // expected value 1

value++; // 4
index = searchFor(value);
expect(index).toBe(1); // expected value 1

value++; // 5
index = searchFor(value);
expect(index).toBe(2); // expected value 2

value++; // 6
index = searchFor(value);
expect(index).toBe(2); // expected value 2

value++; // 7
index = searchFor(value);
expect(index).toBe(3); // expected value 3

value++; // 8
index = searchFor(value);
expect(index).toBe(3); // expected value 3

value++; // 9
index = searchFor(value);
expect(index).toBe(3); // expected value 3

value++; // 10
index = searchFor(value);
expect(index).toBe(4); // expected value 4

value++; // 11
index = searchFor(value);
expect(index).toBe(4); // expected value 4

value++; // 12
index = searchFor(value);
expect(index).toBe(4); // expected value 4
});

test('BINARY SEARCH / run test using integer keys', () => {
const KEYS = [
1000, 1500, 2250, 3375, 5063,
7594, 11391, 17086, 25629, 38443,
57665, 86498, 129746, 194620, 291929,
437894, 656841, 985261, 1477892, 2216838,
3325257, 4987885, 7481828
];

let searchFor = binarySearch.bind(null, KEYS);

let index = searchFor(10);
expect(index).toBe(0); // expected value 0

index = searchFor(1001);
expect(index).toBe(0); // expected value 0

index = searchFor(1499);
expect(index).toBe(0); // expected value 0

index = searchFor(1500);
expect(index).toBe(1); // expected value 1

index = searchFor(2200);
expect(index).toBe(1); // expected value 1

index = searchFor(2251);
expect(index).toBe(2); // expected value 2
});

test('BINARY SEARCH / run test using float keys', () => {
const KEYS = [
1, 1.5, 2.25, 3.38, 5.06, 7.59, 11.39, 17.09, 25.63, 38.44,
57.67, 86.5, 129.75, 194.62, 291.93, 437.89, 656.84, 985.26, 1477.89,
2216.84, 3325.26, 4987.89, 7481.83
];

let searchFor = binarySearch.bind(null, KEYS);

let index = searchFor(3.38);
expect(index).toBe(3); // expected value 3

index = searchFor(6);
expect(index).toBe(4); // expected value 4

index = searchFor(500.55);
expect(index).toBe(15); // expected value 15

index = searchFor(7481.83);
expect(index).toBe(22); // expected value 22

index = searchFor(80000);
expect(index).toBe(22); // expected value 22
});
21 changes: 21 additions & 0 deletions src/utils/murmur3/__tests__/legacy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @ts-nocheck
import * as utils from '../legacy';
import csv from 'csv-streamify';
import fs from 'fs';

test('ENGINE / validate hashing behavior using sample data', (done) => {
let parser = csv({ objectMode: false });

parser.on('data', (line) => {
let [seed, key, hash, bucket] = JSON.parse(line.toString('utf8').trim());

seed = parseInt(seed, 10);
hash = parseInt(hash, 10);
bucket = parseInt(bucket, 10);

expect(utils.hash(key, seed)).toBe(hash); // matching using int32 hash value
expect(utils.bucket(key, seed)).toBe(bucket); // matching using int32 bucket value
}).on('end', done);

fs.createReadStream(require.resolve('./mocks/small-data.csv')).pipe(parser);
});
Loading