Skip to content

Commit

Permalink
Fix generator randomBoolean() to ensure it works with seeded random…
Browse files Browse the repository at this point in the history
… numbers
  • Loading branch information
paul-tavares committed May 27, 2021
1 parent 67e4838 commit a34cc45
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const DAY_OFFSETS = Array.from({ length: 14 }, (_, i) => 8.64e7 * (i + 1));
* public method named `generate()` which should be implemented by sub-classes.
*/
export class BaseDataGenerator<GeneratedDoc extends {} = {}> {
/** A javascript seeded random number (float between 0 and 1). Don't use `Math.random()` */
protected random: seedrandom.prng;

constructor(seed: string | seedrandom.prng = Math.random().toString()) {
Expand Down Expand Up @@ -49,7 +50,7 @@ export class BaseDataGenerator<GeneratedDoc extends {} = {}> {

/** Generate either `true` or `false` */
protected randomBoolean(): boolean {
return Math.random() < 0.5;
return this.random() < 0.5;
}

/** generate random OS family value */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ describe('data generator', () => {
expect(event2.event?.sequence).toBe((firstNonNullValue(event1.event?.sequence) ?? 0) + 1);
});

it('creates the same documents with same random seed', () => {
// Lets run this one multiple times just to ensure that the randomness
// is truly predicable based on the seed passed
it.each([1, 2, 3, 4, 5])('[%#] creates the same documents with same random seed', () => {
const generator1 = new EndpointDocGenerator('seed');
const generator2 = new EndpointDocGenerator('seed');
const timestamp = new Date().getTime();
Expand Down

0 comments on commit a34cc45

Please sign in to comment.