Skip to content

Commit e0fd9f0

Browse files
move helper functions
1 parent 4b50116 commit e0fd9f0

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*!
2+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3+
* Licensed under the MIT License.
4+
*/
5+
6+
import { AttachState } from "@fluidframework/container-definitions";
7+
import type {
8+
IChannel,
9+
IChannelServices,
10+
} from "@fluidframework/datastore-definitions/internal";
11+
import {
12+
MockFluidDataStoreRuntime,
13+
MockStorage,
14+
type MockContainerRuntime,
15+
MockContainerRuntimeFactory,
16+
} from "@fluidframework/test-runtime-utils/internal";
17+
18+
/**
19+
* @internal
20+
*/
21+
export type DDSCreator<T extends IChannel> = (
22+
runtime: MockFluidDataStoreRuntime,
23+
id: string,
24+
) => T;
25+
26+
/**
27+
* @internal
28+
*/
29+
export interface RollbackTestSetup<T extends IChannel> {
30+
dds: T;
31+
dataStoreRuntime: MockFluidDataStoreRuntime;
32+
containerRuntimeFactory: MockContainerRuntimeFactory;
33+
containerRuntime: MockContainerRuntime;
34+
}
35+
36+
/**
37+
* Setup rollback tests
38+
* @internal
39+
*/
40+
export function setupRollbackTest<T extends IChannel>(
41+
id: string,
42+
createDDS: DDSCreator<T>,
43+
opts?: { initialize?: (dds: T) => void },
44+
): RollbackTestSetup<T> {
45+
const containerRuntimeFactory = new MockContainerRuntimeFactory({ flushMode: 1 });
46+
const dataStoreRuntime = new MockFluidDataStoreRuntime({ clientId: "1" });
47+
const containerRuntime = containerRuntimeFactory.createContainerRuntime(dataStoreRuntime);
48+
49+
const dds = createDDS(dataStoreRuntime, id);
50+
51+
dataStoreRuntime.setAttachState(AttachState.Attached);
52+
opts?.initialize?.(dds);
53+
54+
const services: IChannelServices = {
55+
deltaConnection: dataStoreRuntime.createDeltaConnection(),
56+
objectStorage: new MockStorage(),
57+
};
58+
dds.connect(services);
59+
60+
return { dds, dataStoreRuntime, containerRuntimeFactory, containerRuntime };
61+
}
62+
63+
/**
64+
* Create a new client
65+
* @internal
66+
*/
67+
export function createAdditionalClient<T extends IChannel>(
68+
containerRuntimeFactory: MockContainerRuntimeFactory,
69+
id: string,
70+
createDDS: DDSCreator<T>,
71+
opts?: { initialize?: (dds: T) => void },
72+
): {
73+
dds: T;
74+
dataStoreRuntime: MockFluidDataStoreRuntime;
75+
containerRuntime: MockContainerRuntime;
76+
} {
77+
const dataStoreRuntime = new MockFluidDataStoreRuntime({ clientId: id });
78+
const containerRuntime = containerRuntimeFactory.createContainerRuntime(dataStoreRuntime);
79+
80+
const dds = createDDS(dataStoreRuntime, id);
81+
82+
dataStoreRuntime.setAttachState(AttachState.Attached);
83+
opts?.initialize?.(dds);
84+
85+
const services: IChannelServices = {
86+
deltaConnection: dataStoreRuntime.createDeltaConnection(),
87+
objectStorage: new MockStorage(),
88+
};
89+
dds.connect(services);
90+
91+
return { dds, dataStoreRuntime, containerRuntime };
92+
}

packages/dds/test-dds-utils/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@ export {
3434
export type { ISnapshotSuite } from "./ddsSnapshotHarness.js";
3535
export { createSnapshotSuite } from "./ddsSnapshotHarness.js";
3636
export type { Client, FuzzSerializedIdCompressor } from "./clientLoading.js";
37+
export {
38+
setupRollbackTest,
39+
createAdditionalClient,
40+
type RollbackTestSetup,
41+
type DDSCreator,
42+
} from "./ddsTestUtils.js";

0 commit comments

Comments
 (0)