|
| 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 | + makeRandom, |
| 17 | + takeAsync, |
| 18 | +} from "@fluid-private/stochastic-test-utils"; |
| 19 | +import type { |
| 20 | + DDSFuzzModel, |
| 21 | + DDSFuzzSuiteOptions, |
| 22 | + DDSFuzzTestState, |
| 23 | +} from "@fluid-private/test-dds-utils"; |
| 24 | + |
| 25 | +import { ConsensusQueueFactory } from "../consensusOrderedCollectionFactory.js"; |
| 26 | +import type { IConsensusOrderedCollection, IOrderedCollection } from "../interfaces.js"; |
| 27 | +import { ConsensusResult } from "../interfaces.js"; |
| 28 | + |
| 29 | +import { _dirname } from "./dirname.cjs"; |
| 30 | + |
| 31 | +/** |
| 32 | + * Config options for generating ConsensusOrderedCollection operations |
| 33 | + */ |
| 34 | +interface ConsensusOrderedCollectionValueConfig { |
| 35 | + /** |
| 36 | + * Number of values to be generated for the pool |
| 37 | + */ |
| 38 | + valuePoolSize?: number; |
| 39 | + /** |
| 40 | + * Length of value strings |
| 41 | + */ |
| 42 | + valueStringLength?: number; |
| 43 | +} |
| 44 | + |
| 45 | +const valueConfigs: Required<ConsensusOrderedCollectionValueConfig> = { |
| 46 | + valuePoolSize: 3, |
| 47 | + valueStringLength: 5, |
| 48 | +}; |
| 49 | + |
| 50 | +/** |
| 51 | + * Default options for ConsensusOrderedCollection fuzz testing |
| 52 | + */ |
| 53 | +export const defaultOptions: Partial<DDSFuzzSuiteOptions> = { |
| 54 | + validationStrategy: { type: "fixedInterval", interval: 10 }, |
| 55 | + clientJoinOptions: { |
| 56 | + maxNumberOfClients: 6, |
| 57 | + clientAddProbability: 0.05, |
| 58 | + }, |
| 59 | + defaultTestCount: 100, |
| 60 | + saveFailures: { directory: path.join(_dirname, "../../src/test/results") }, |
| 61 | +}; |
| 62 | + |
| 63 | +type FuzzTestState = DDSFuzzTestState<ConsensusQueueFactory>; |
| 64 | + |
| 65 | +export interface AddOperation { |
| 66 | + type: "add"; |
| 67 | + value: string; |
| 68 | +} |
| 69 | + |
| 70 | +export interface AcquireOperation { |
| 71 | + type: "acquire"; |
| 72 | + result: ConsensusResult; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Represents ConsensusOrderedCollection operation types for fuzz testing |
| 77 | + */ |
| 78 | +export type ConsensusOrderedCollectionOperation = AddOperation | AcquireOperation; |
| 79 | + |
| 80 | +function makeOperationGenerator(): Generator< |
| 81 | + ConsensusOrderedCollectionOperation, |
| 82 | + FuzzTestState |
| 83 | +> { |
| 84 | + type OpSelectionState = FuzzTestState & { |
| 85 | + itemValue: string; |
| 86 | + pendingCallbacks: Map<string, string>; |
| 87 | + }; |
| 88 | + |
| 89 | + const valuePoolRandom = makeRandom(0); |
| 90 | + const dedupe = <T>(arr: T[]): T[] => [...new Set(arr)]; |
| 91 | + const valuePool = dedupe( |
| 92 | + Array.from({ length: valueConfigs.valuePoolSize }, () => |
| 93 | + valuePoolRandom.string(valueConfigs.valueStringLength), |
| 94 | + ), |
| 95 | + ); |
| 96 | + |
| 97 | + async function add(state: OpSelectionState): Promise<AddOperation> { |
| 98 | + return { |
| 99 | + type: "add", |
| 100 | + value: state.itemValue, |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + async function acquire(state: OpSelectionState): Promise<AcquireOperation> { |
| 105 | + return { |
| 106 | + type: "acquire", |
| 107 | + result: state.random.pick([ConsensusResult.Complete, ConsensusResult.Release]), |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + const clientBaseOperationGenerator = createWeightedGenerator< |
| 112 | + ConsensusOrderedCollectionOperation, |
| 113 | + OpSelectionState |
| 114 | + >([ |
| 115 | + [add, 1], |
| 116 | + [acquire, 1], |
| 117 | + ]); |
| 118 | + |
| 119 | + return async (state: FuzzTestState) => |
| 120 | + clientBaseOperationGenerator({ |
| 121 | + ...state, |
| 122 | + itemValue: state.random.pick(valuePool), |
| 123 | + pendingCallbacks: new Map<string, string>(), |
| 124 | + }); |
| 125 | +} |
| 126 | + |
| 127 | +// Track async errors that occur during fire-and-forget operations |
| 128 | +let pendingAsyncError: Error | undefined; |
| 129 | + |
| 130 | +function makeReducer(): Reducer<ConsensusOrderedCollectionOperation, FuzzTestState> { |
| 131 | + const reducer = combineReducers<ConsensusOrderedCollectionOperation, FuzzTestState>({ |
| 132 | + add: ({ client }, { value }) => { |
| 133 | + client.channel.add(value).catch((error: Error) => { |
| 134 | + pendingAsyncError = error; |
| 135 | + }); |
| 136 | + }, |
| 137 | + acquire: ({ client }, { result }) => { |
| 138 | + client.channel |
| 139 | + .acquire(async (_value: string): Promise<ConsensusResult> => { |
| 140 | + return result; |
| 141 | + }) |
| 142 | + .catch((error: Error) => { |
| 143 | + pendingAsyncError = error; |
| 144 | + }); |
| 145 | + }, |
| 146 | + }); |
| 147 | + return reducer; |
| 148 | +} |
| 149 | + |
| 150 | +function assertEqualConsensusOrderedCollections( |
| 151 | + a: IConsensusOrderedCollection, |
| 152 | + b: IConsensusOrderedCollection, |
| 153 | +): void { |
| 154 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access |
| 155 | + const aData = (a as any).data as IOrderedCollection<string>; |
| 156 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access |
| 157 | + const bData = (b as any).data as IOrderedCollection<string>; |
| 158 | + assert.equal(aData.size, bData.size, "Data sizes should be equal"); |
| 159 | + assert.deepEqual(aData.asArray(), bData.asArray(), "Data contents should be equal"); |
| 160 | + |
| 161 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access |
| 162 | + const aJobTracking = (a as any).jobTracking as Map< |
| 163 | + string, |
| 164 | + { value: string; clientId: string | undefined } |
| 165 | + >; |
| 166 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access |
| 167 | + const bJobTracking = (b as any).jobTracking as Map< |
| 168 | + string, |
| 169 | + { value: string; clientId: string | undefined } |
| 170 | + >; |
| 171 | + |
| 172 | + assert.equal(aJobTracking.size, bJobTracking.size, "Job tracking sizes should be equal"); |
| 173 | + for (const [key, aJob] of aJobTracking.entries()) { |
| 174 | + const bJob = bJobTracking.get(key); |
| 175 | + assert.deepEqual(aJob, bJob, `Job tracking entry for key ${key} should be equal`); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +/** |
| 180 | + * Base fuzz model for ConsensusOrderedCollection |
| 181 | + */ |
| 182 | +export const baseConsensusOrderedCollectionModel: DDSFuzzModel< |
| 183 | + ConsensusQueueFactory, |
| 184 | + ConsensusOrderedCollectionOperation, |
| 185 | + FuzzTestState |
| 186 | +> = { |
| 187 | + workloadName: "default configuration", |
| 188 | + generatorFactory: () => takeAsync(100, makeOperationGenerator()), |
| 189 | + reducer: makeReducer(), |
| 190 | + validateConsistency: (a, b) => { |
| 191 | + // Check if any async errors occurred during fire-and-forget operations |
| 192 | + if (pendingAsyncError !== undefined) { |
| 193 | + throw pendingAsyncError; |
| 194 | + } |
| 195 | + assertEqualConsensusOrderedCollections(a.channel, b.channel); |
| 196 | + }, |
| 197 | + factory: new ConsensusQueueFactory(), |
| 198 | +}; |
0 commit comments