|
| 1 | +/*! |
| 2 | + * Copyright (c) Microsoft Corporation and contributors. All rights reserved. |
| 3 | + * Licensed under the MIT License. |
| 4 | + */ |
| 5 | + |
| 6 | +import { strict as assert } from "node:assert"; |
| 7 | +import * as path from "node:path"; |
| 8 | + |
| 9 | +import type { |
| 10 | + AsyncGenerator as Generator, |
| 11 | + Reducer, |
| 12 | +} from "@fluid-private/stochastic-test-utils"; |
| 13 | +import { |
| 14 | + combineReducers, |
| 15 | + createWeightedAsyncGenerator as createWeightedGenerator, |
| 16 | + takeAsync as take, |
| 17 | +} from "@fluid-private/stochastic-test-utils"; |
| 18 | +import type { |
| 19 | + DDSFuzzModel, |
| 20 | + DDSFuzzSuiteOptions, |
| 21 | + DDSFuzzTestState, |
| 22 | +} from "@fluid-private/test-dds-utils"; |
| 23 | + |
| 24 | +import type { IIncrementOperation } from "../counter.js"; |
| 25 | +import { CounterFactory } from "../counterFactory.js"; |
| 26 | +import type { ISharedCounter } from "../interfaces.js"; |
| 27 | + |
| 28 | +import { _dirname } from "./dirname.cjs"; |
| 29 | + |
| 30 | +/** |
| 31 | + * Default options for Counter fuzz testing |
| 32 | + */ |
| 33 | +export const defaultOptions: Partial<DDSFuzzSuiteOptions> = { |
| 34 | + validationStrategy: { type: "fixedInterval", interval: 10 }, |
| 35 | + clientJoinOptions: { |
| 36 | + maxNumberOfClients: 6, |
| 37 | + clientAddProbability: 0.05, |
| 38 | + stashableClientProbability: 0.2, |
| 39 | + }, |
| 40 | + defaultTestCount: 100, |
| 41 | + saveFailures: { directory: path.join(_dirname, "../../src/test/results") }, |
| 42 | +}; |
| 43 | + |
| 44 | +type FuzzTestState = DDSFuzzTestState<CounterFactory>; |
| 45 | + |
| 46 | +/** |
| 47 | + * Represents Counter operation types for fuzz testing |
| 48 | + */ |
| 49 | +export type CounterOperation = IIncrementOperation; |
| 50 | + |
| 51 | +function makeOperationGenerator(): Generator<CounterOperation, FuzzTestState> { |
| 52 | + async function increment(state: FuzzTestState): Promise<CounterOperation> { |
| 53 | + return { |
| 54 | + type: "increment", |
| 55 | + incrementAmount: state.random.integer(-10, 10), |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + const clientBaseOperationGenerator = createWeightedGenerator< |
| 60 | + CounterOperation, |
| 61 | + FuzzTestState |
| 62 | + >([[increment, 1]]); |
| 63 | + |
| 64 | + return async (state: FuzzTestState) => |
| 65 | + clientBaseOperationGenerator({ |
| 66 | + ...state, |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +function makeReducer(): Reducer<CounterOperation, FuzzTestState> { |
| 71 | + const reducer = combineReducers<CounterOperation, FuzzTestState>({ |
| 72 | + increment: ({ client }, { incrementAmount }) => { |
| 73 | + client.channel.increment(incrementAmount); |
| 74 | + }, |
| 75 | + }); |
| 76 | + return reducer; |
| 77 | +} |
| 78 | + |
| 79 | +function assertEqualCounters(a: ISharedCounter, b: ISharedCounter): void { |
| 80 | + assert.equal(a.value, b.value, `Counter values do not match: ${a.value} !== ${b.value}`); |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Base fuzz model for Counter |
| 85 | + */ |
| 86 | +export const baseCounterModel: DDSFuzzModel<CounterFactory, CounterOperation, FuzzTestState> = |
| 87 | + { |
| 88 | + workloadName: "default configuration", |
| 89 | + generatorFactory: () => take(100, makeOperationGenerator()), |
| 90 | + reducer: makeReducer(), |
| 91 | + validateConsistency: (a, b) => assertEqualCounters(a.channel, b.channel), |
| 92 | + factory: new CounterFactory(), |
| 93 | + }; |
0 commit comments