@fluidframework/test-utils 2.0.0-internal.3.0.2 → 2.0.0-internal.3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/.eslintrc.js +8 -10
  2. package/README.md +41 -11
  3. package/api-extractor.json +2 -2
  4. package/dist/DriverWrappers.d.ts.map +1 -1
  5. package/dist/DriverWrappers.js.map +1 -1
  6. package/dist/TestConfigs.d.ts.map +1 -1
  7. package/dist/TestConfigs.js +3 -2
  8. package/dist/TestConfigs.js.map +1 -1
  9. package/dist/TestSummaryUtils.d.ts +15 -4
  10. package/dist/TestSummaryUtils.d.ts.map +1 -1
  11. package/dist/TestSummaryUtils.js +23 -17
  12. package/dist/TestSummaryUtils.js.map +1 -1
  13. package/dist/containerUtils.d.ts +1 -2
  14. package/dist/containerUtils.d.ts.map +1 -1
  15. package/dist/containerUtils.js +4 -2
  16. package/dist/containerUtils.js.map +1 -1
  17. package/dist/index.d.ts +4 -4
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +1 -2
  20. package/dist/index.js.map +1 -1
  21. package/dist/interfaces.d.ts.map +1 -1
  22. package/dist/interfaces.js.map +1 -1
  23. package/dist/loaderContainerTracker.d.ts.map +1 -1
  24. package/dist/loaderContainerTracker.js +37 -27
  25. package/dist/loaderContainerTracker.js.map +1 -1
  26. package/dist/localCodeLoader.d.ts.map +1 -1
  27. package/dist/localCodeLoader.js.map +1 -1
  28. package/dist/localLoader.d.ts.map +1 -1
  29. package/dist/localLoader.js.map +1 -1
  30. package/dist/packageVersion.d.ts +1 -1
  31. package/dist/packageVersion.js +1 -1
  32. package/dist/packageVersion.js.map +1 -1
  33. package/dist/retry.d.ts.map +1 -1
  34. package/dist/retry.js.map +1 -1
  35. package/dist/testContainerRuntimeFactory.d.ts.map +1 -1
  36. package/dist/testContainerRuntimeFactory.js +2 -1
  37. package/dist/testContainerRuntimeFactory.js.map +1 -1
  38. package/dist/testFluidObject.d.ts.map +1 -1
  39. package/dist/testFluidObject.js +7 -3
  40. package/dist/testFluidObject.js.map +1 -1
  41. package/dist/testObjectProvider.d.ts +4 -1
  42. package/dist/testObjectProvider.d.ts.map +1 -1
  43. package/dist/testObjectProvider.js +28 -11
  44. package/dist/testObjectProvider.js.map +1 -1
  45. package/dist/timeoutUtils.d.ts.map +1 -1
  46. package/dist/timeoutUtils.js +4 -3
  47. package/dist/timeoutUtils.js.map +1 -1
  48. package/package.json +66 -55
  49. package/prettier.config.cjs +1 -1
  50. package/src/DriverWrappers.ts +40 -37
  51. package/src/TestConfigs.ts +9 -7
  52. package/src/TestSummaryUtils.ts +113 -119
  53. package/src/containerUtils.ts +20 -18
  54. package/src/index.ts +24 -25
  55. package/src/interfaces.ts +10 -7
  56. package/src/loaderContainerTracker.ts +627 -565
  57. package/src/localCodeLoader.ts +85 -77
  58. package/src/localLoader.ts +24 -24
  59. package/src/packageVersion.ts +1 -1
  60. package/src/retry.ts +31 -25
  61. package/src/testContainerRuntimeFactory.ts +59 -56
  62. package/src/testFluidObject.ts +168 -152
  63. package/src/testObjectProvider.ts +445 -384
  64. package/src/timeoutUtils.ts +174 -154
  65. package/tsconfig.json +9 -16
@@ -4,15 +4,23 @@
4
4
  */
5
5
 
6
6
  import { IContainer, IHostLoader, IFluidCodeDetails } from "@fluidframework/container-definitions";
7
- import { ITelemetryGenericEvent, ITelemetryBaseLogger, ITelemetryBaseEvent } from "@fluidframework/common-definitions";
8
7
  import {
9
- ILoaderProps,
10
- Loader,
11
- waitContainerToCatchUp as waitContainerToCatchUp_original,
8
+ ITelemetryGenericEvent,
9
+ ITelemetryBaseLogger,
10
+ ITelemetryBaseEvent,
11
+ } from "@fluidframework/common-definitions";
12
+ import {
13
+ ILoaderProps,
14
+ Loader,
15
+ waitContainerToCatchUp as waitContainerToCatchUp_original,
12
16
  } from "@fluidframework/container-loader";
13
17
  import { IContainerRuntimeOptions } from "@fluidframework/container-runtime";
14
18
  import { IRequestHeader } from "@fluidframework/core-interfaces";
15
- import { IDocumentServiceFactory, IResolvedUrl, IUrlResolver } from "@fluidframework/driver-definitions";
19
+ import {
20
+ IDocumentServiceFactory,
21
+ IResolvedUrl,
22
+ IUrlResolver,
23
+ } from "@fluidframework/driver-definitions";
16
24
  import { ensureFluidResolvedUrl } from "@fluidframework/driver-utils";
17
25
  import { ITestDriver, TestDriverTypes } from "@fluidframework/test-driver-definitions";
18
26
  import { v4 as uuid } from "uuid";
@@ -23,84 +31,90 @@ import { createAndAttachContainer } from "./localLoader";
23
31
  import { ChannelFactoryRegistry } from "./testFluidObject";
24
32
 
25
33
  const defaultCodeDetails: IFluidCodeDetails = {
26
- package: "defaultTestPackage",
27
- config: {},
34
+ package: "defaultTestPackage",
35
+ config: {},
28
36
  };
29
37
 
30
38
  export interface IOpProcessingController {
31
- processIncoming(...containers: IContainer[]): Promise<void>;
32
- processOutgoing(...containers: IContainer[]): Promise<void>;
33
- pauseProcessing(...containers: IContainer[]): Promise<void>;
34
- resumeProcessing(...containers: IContainer[]): void;
39
+ processIncoming(...containers: IContainer[]): Promise<void>;
40
+ processOutgoing(...containers: IContainer[]): Promise<void>;
41
+ pauseProcessing(...containers: IContainer[]): Promise<void>;
42
+ resumeProcessing(...containers: IContainer[]): void;
35
43
  }
36
44
 
37
45
  export interface ITestObjectProvider {
38
- createFluidEntryPoint: (testContainerConfig?: ITestContainerConfig) => fluidEntryPoint;
39
- createLoader(
40
- packageEntries: Iterable<[IFluidCodeDetails, fluidEntryPoint]>,
41
- loaderProps?: Partial<ILoaderProps>,
42
- ): IHostLoader;
43
- createContainer(entryPoint: fluidEntryPoint, loaderProps?: Partial<ILoaderProps>): Promise<IContainer>;
44
- loadContainer(
45
- entryPoint: fluidEntryPoint,
46
- loaderProps?: Partial<ILoaderProps>,
47
- requestHeader?: IRequestHeader,
48
- ): Promise<IContainer>;
49
-
50
- /**
51
- * Used to create a test Container. The Loader/ContainerRuntime/DataRuntime might be different versioned.
52
- * In generateLocalCompatTest(), this Container and its runtime will be arbitrarily-versioned.
53
- */
54
- makeTestLoader(testContainerConfig?: ITestContainerConfig): IHostLoader;
55
- makeTestContainer(testContainerConfig?: ITestContainerConfig): Promise<IContainer>;
56
- loadTestContainer(testContainerConfig?: ITestContainerConfig, requestHeader?: IRequestHeader): Promise<IContainer>;
57
- /**
58
- *
59
- * @param url - Resolved container URL
60
- */
61
- updateDocumentId(url: IResolvedUrl | undefined): void;
62
-
63
- logger: ITelemetryBaseLogger;
64
- documentServiceFactory: IDocumentServiceFactory;
65
- urlResolver: IUrlResolver;
66
- defaultCodeDetails: IFluidCodeDetails;
67
- opProcessingController: IOpProcessingController;
68
-
69
- ensureSynchronized(timeoutDuration?: number): Promise<void>;
70
- reset(): void;
71
-
72
- documentId: string;
73
- driver: ITestDriver;
46
+ createFluidEntryPoint: (testContainerConfig?: ITestContainerConfig) => fluidEntryPoint;
47
+ createLoader(
48
+ packageEntries: Iterable<[IFluidCodeDetails, fluidEntryPoint]>,
49
+ loaderProps?: Partial<ILoaderProps>,
50
+ ): IHostLoader;
51
+ createContainer(
52
+ entryPoint: fluidEntryPoint,
53
+ loaderProps?: Partial<ILoaderProps>,
54
+ ): Promise<IContainer>;
55
+ loadContainer(
56
+ entryPoint: fluidEntryPoint,
57
+ loaderProps?: Partial<ILoaderProps>,
58
+ requestHeader?: IRequestHeader,
59
+ ): Promise<IContainer>;
60
+
61
+ /**
62
+ * Used to create a test Container. The Loader/ContainerRuntime/DataRuntime might be different versioned.
63
+ * In generateLocalCompatTest(), this Container and its runtime will be arbitrarily-versioned.
64
+ */
65
+ makeTestLoader(testContainerConfig?: ITestContainerConfig): IHostLoader;
66
+ makeTestContainer(testContainerConfig?: ITestContainerConfig): Promise<IContainer>;
67
+ loadTestContainer(
68
+ testContainerConfig?: ITestContainerConfig,
69
+ requestHeader?: IRequestHeader,
70
+ ): Promise<IContainer>;
71
+ /**
72
+ *
73
+ * @param url - Resolved container URL
74
+ */
75
+ updateDocumentId(url: IResolvedUrl | undefined): void;
76
+
77
+ logger: ITelemetryBaseLogger;
78
+ documentServiceFactory: IDocumentServiceFactory;
79
+ urlResolver: IUrlResolver;
80
+ defaultCodeDetails: IFluidCodeDetails;
81
+ opProcessingController: IOpProcessingController;
82
+
83
+ ensureSynchronized(timeoutDuration?: number): Promise<void>;
84
+ reset(): void;
85
+
86
+ documentId: string;
87
+ driver: ITestDriver;
74
88
  }
75
89
 
76
90
  export enum DataObjectFactoryType {
77
- Primed, // default
78
- Test,
91
+ Primed, // default
92
+ Test,
79
93
  }
80
94
 
81
95
  export interface ITestContainerConfig {
82
- /** TestFluidDataObject instead of PrimedDataStore */
83
- fluidDataObjectType?: DataObjectFactoryType;
96
+ /** TestFluidDataObject instead of PrimedDataStore */
97
+ fluidDataObjectType?: DataObjectFactoryType;
84
98
 
85
- /** An array of channel name and DDS factory pair to create on container creation time */
86
- registry?: ChannelFactoryRegistry;
99
+ /** An array of channel name and DDS factory pair to create on container creation time */
100
+ registry?: ChannelFactoryRegistry;
87
101
 
88
- /** Container runtime options for the container instance */
89
- runtimeOptions?: IContainerRuntimeOptions;
102
+ /** Container runtime options for the container instance */
103
+ runtimeOptions?: IContainerRuntimeOptions;
90
104
 
91
- /** Whether this runtime should be instantiated using a mixed-in attributor class */
92
- enableAttribution?: boolean;
105
+ /** Whether this runtime should be instantiated using a mixed-in attributor class */
106
+ enableAttribution?: boolean;
93
107
 
94
- /** Loader options for the loader used to create containers */
95
- loaderProps?: Partial<ILoaderProps>;
108
+ /** Loader options for the loader used to create containers */
109
+ loaderProps?: Partial<ILoaderProps>;
96
110
  }
97
111
 
98
112
  export const createDocumentId = (): string => uuid();
99
113
 
100
114
  interface IDocumentIdStrategy {
101
- get(): string;
102
- update(resolvedUrl?: IResolvedUrl): void;
103
- reset(): void;
115
+ get(): string;
116
+ update(resolvedUrl?: IResolvedUrl): void;
117
+ reset(): void;
104
118
  }
105
119
 
106
120
  /**
@@ -108,25 +122,29 @@ interface IDocumentIdStrategy {
108
122
  * a new container ID and accessing the container in multi-instance test cases.
109
123
  */
110
124
  function getDocumentIdStrategy(type?: TestDriverTypes): IDocumentIdStrategy {
111
- let documentId = createDocumentId();
112
- switch (type) {
113
- case "odsp":
114
- return {
115
- get: () => documentId,
116
- update: () => { }, // do not update the document ID in odsp test cases
117
- reset: () => { documentId = createDocumentId(); },
118
- };
119
- default:
120
- return {
121
- get: () => documentId,
122
- update: (resolvedUrl?: IResolvedUrl) => {
123
- // Extract the document ID from the resolved container's URL and reset the ID property
124
- ensureFluidResolvedUrl(resolvedUrl);
125
- documentId = resolvedUrl.id ?? documentId;
126
- },
127
- reset: () => { documentId = createDocumentId(); },
128
- };
129
- }
125
+ let documentId = createDocumentId();
126
+ switch (type) {
127
+ case "odsp":
128
+ return {
129
+ get: () => documentId,
130
+ update: () => {}, // do not update the document ID in odsp test cases
131
+ reset: () => {
132
+ documentId = createDocumentId();
133
+ },
134
+ };
135
+ default:
136
+ return {
137
+ get: () => documentId,
138
+ update: (resolvedUrl?: IResolvedUrl) => {
139
+ // Extract the document ID from the resolved container's URL and reset the ID property
140
+ ensureFluidResolvedUrl(resolvedUrl);
141
+ documentId = resolvedUrl.id ?? documentId;
142
+ },
143
+ reset: () => {
144
+ documentId = createDocumentId();
145
+ },
146
+ };
147
+ }
130
148
  }
131
149
 
132
150
  /**
@@ -136,315 +154,358 @@ function getDocumentIdStrategy(type?: TestDriverTypes): IDocumentIdStrategy {
136
154
  * any expected events that have not occurred.
137
155
  */
138
156
  export class EventAndErrorTrackingLogger extends TelemetryLogger {
139
- /** Even if these error events are logged, tests should still be allowed to pass */
140
- private readonly allowedErrors: string[] = [
141
- // This log was removed in current version as unnecessary, but it's still present in previous versions
142
- "fluid:telemetry:Container:NoRealStorageInDetachedContainer",
143
- ];
144
-
145
- constructor(private readonly baseLogger: ITelemetryBaseLogger) {
146
- super();
147
- }
148
-
149
- private readonly expectedEvents: ({ index: number; event: ITelemetryGenericEvent | undefined; } | undefined)[] = [];
150
- private readonly unexpectedErrors: ITelemetryBaseEvent[] = [];
151
-
152
- public registerExpectedEvent(...orderedExpectedEvents: ITelemetryGenericEvent[]) {
153
- if (this.expectedEvents.length !== 0) {
154
- // we don't have to error here. just no reason not to. given the events must be
155
- // ordered it could be tricky to figure out problems around multiple registrations.
156
- throw new Error(
157
- "Expected events already registered.\n"
158
- + "Call reportAndClearTrackedEvents to clear them before registering more");
159
- }
160
- this.expectedEvents.push(...orderedExpectedEvents.map((event, index) => ({ index, event })));
161
- }
162
-
163
- send(event: ITelemetryBaseEvent): void {
164
- const ee = this.expectedEvents[0]?.event;
165
- if (ee?.eventName === event.eventName) {
166
- let matches = true;
167
- for (const key of Object.keys(ee)) {
168
- if (ee[key] !== event[key]) {
169
- matches = false;
170
- break;
171
- }
172
- }
173
- if (matches) {
174
- // we found an expected event
175
- // so remove it from the list of expected events
176
- // and if it is an error, change it to generic
177
- // this helps keep our telemetry clear of
178
- // expected errors.
179
- this.expectedEvents.shift();
180
- if (event.category === "error") {
181
- event.category = "generic";
182
- }
183
- }
184
- }
185
- if (event.category === "error") {
186
- if (this.allowedErrors.includes(event.eventName)) {
187
- event.category = "generic";
188
- } else {
189
- this.unexpectedErrors.push(event);
190
- }
191
- }
192
-
193
- this.baseLogger.send(event);
194
- }
195
-
196
- public reportAndClearTrackedEvents() {
197
- const expectedNotFound = this.expectedEvents.splice(0, this.expectedEvents.length);
198
- const unexpectedErrors = this.unexpectedErrors.splice(0, this.unexpectedErrors.length);
199
- return {
200
- expectedNotFound,
201
- unexpectedErrors,
202
- };
203
- }
157
+ /**
158
+ * Even if these error events are logged, tests should still be allowed to pass
159
+ * Additionally, if downgrade is true, then log as generic (e.g. to avoid polluting the e2e test logs)
160
+ */
161
+ private readonly allowedErrors: { eventName: string; downgrade?: true }[] = [
162
+ // This log was removed in current version as unnecessary, but it's still present in previous versions
163
+ {
164
+ eventName: "fluid:telemetry:Container:NoRealStorageInDetachedContainer",
165
+ downgrade: true,
166
+ },
167
+ // This log's category changes depending on the op latency. test results shouldn't be affected but if we see lots we'd like an alert from the logs.
168
+ { eventName: "fluid:telemetry:OpPerf:OpRoundtripTime" },
169
+ ];
170
+
171
+ constructor(private readonly baseLogger: ITelemetryBaseLogger) {
172
+ super();
173
+ }
174
+
175
+ private readonly expectedEvents: (
176
+ | { index: number; event: ITelemetryGenericEvent | undefined }
177
+ | undefined
178
+ )[] = [];
179
+ private readonly unexpectedErrors: ITelemetryBaseEvent[] = [];
180
+
181
+ public registerExpectedEvent(...orderedExpectedEvents: ITelemetryGenericEvent[]) {
182
+ if (this.expectedEvents.length !== 0) {
183
+ // we don't have to error here. just no reason not to. given the events must be
184
+ // ordered it could be tricky to figure out problems around multiple registrations.
185
+ throw new Error(
186
+ "Expected events already registered.\n" +
187
+ "Call reportAndClearTrackedEvents to clear them before registering more",
188
+ );
189
+ }
190
+ this.expectedEvents.push(
191
+ ...orderedExpectedEvents.map((event, index) => ({ index, event })),
192
+ );
193
+ }
194
+
195
+ send(event: ITelemetryBaseEvent): void {
196
+ const ee = this.expectedEvents[0]?.event;
197
+ if (ee?.eventName === event.eventName) {
198
+ let matches = true;
199
+ for (const key of Object.keys(ee)) {
200
+ if (ee[key] !== event[key]) {
201
+ matches = false;
202
+ break;
203
+ }
204
+ }
205
+ if (matches) {
206
+ // we found an expected event
207
+ // so remove it from the list of expected events
208
+ // and if it is an error, change it to generic
209
+ // this helps keep our telemetry clear of
210
+ // expected errors.
211
+ this.expectedEvents.shift();
212
+ if (event.category === "error") {
213
+ event.category = "generic";
214
+ }
215
+ }
216
+ }
217
+ if (event.category === "error") {
218
+ // Check to see if this error is allowed and if its category should be downgraded
219
+ const allowedError = this.allowedErrors.find(
220
+ ({ eventName }) => eventName === event.eventName,
221
+ );
222
+
223
+ if (allowedError === undefined) {
224
+ this.unexpectedErrors.push(event);
225
+ } else if (allowedError.downgrade) {
226
+ event.category = "generic";
227
+ }
228
+ }
229
+
230
+ this.baseLogger.send(event);
231
+ }
232
+
233
+ public reportAndClearTrackedEvents() {
234
+ const expectedNotFound = this.expectedEvents.splice(0, this.expectedEvents.length);
235
+ const unexpectedErrors = this.unexpectedErrors.splice(0, this.unexpectedErrors.length);
236
+ return {
237
+ expectedNotFound,
238
+ unexpectedErrors,
239
+ };
240
+ }
204
241
  }
205
242
 
206
243
  /**
207
244
  * Shared base class for test object provider. Contain code for loader and container creation and loading
208
245
  */
209
246
  export class TestObjectProvider implements ITestObjectProvider {
210
- private _loaderContainerTracker = new LoaderContainerTracker();
211
- private _documentServiceFactory: IDocumentServiceFactory | undefined;
212
- private _urlResolver: IUrlResolver | undefined;
213
- private _logger: EventAndErrorTrackingLogger | undefined;
214
- private readonly _documentIdStrategy: IDocumentIdStrategy;
215
- // Since documentId doesn't change we can only create/make one container. Call the load functions instead.
216
- private _documentCreated = false;
217
-
218
- /**
219
- * Manage objects for loading and creating container, including the driver, loader, and OpProcessingController
220
- * @param createFluidEntryPoint - callback to create a fluidEntryPoint, with an optional set of channel name
221
- * and factory for TestFluidObject
222
- */
223
- constructor(
224
- public readonly LoaderConstructor: typeof Loader,
225
- public readonly driver: ITestDriver,
226
- public readonly createFluidEntryPoint: (testContainerConfig?: ITestContainerConfig) => fluidEntryPoint,
227
- ) {
228
- this._documentIdStrategy = getDocumentIdStrategy(driver.type);
229
- }
230
-
231
- get logger(): EventAndErrorTrackingLogger {
232
- if (this._logger === undefined) {
233
- this._logger = new EventAndErrorTrackingLogger(
234
- ChildLogger.create(getTestLogger?.(), undefined,
235
- {
236
- all: {
237
- driverType: this.driver.type,
238
- driverEndpointName: this.driver.endpointName,
239
- driverTenantName: this.driver.tenantName,
240
- driverUserIndex: this.driver.userIndex,
241
- },
242
- }));
243
- }
244
- return this._logger;
245
- }
246
-
247
- set logger(logger: EventAndErrorTrackingLogger) {
248
- this._logger = logger;
249
- }
250
-
251
- get documentServiceFactory() {
252
- if (!this._documentServiceFactory) {
253
- this._documentServiceFactory = this.driver.createDocumentServiceFactory();
254
- }
255
- return this._documentServiceFactory;
256
- }
257
-
258
- get urlResolver() {
259
- if (!this._urlResolver) {
260
- this._urlResolver = this.driver.createUrlResolver();
261
- }
262
- return this._urlResolver;
263
- }
264
-
265
- get documentId() {
266
- return this._documentIdStrategy.get();
267
- }
268
-
269
- get defaultCodeDetails() {
270
- return defaultCodeDetails;
271
- }
272
-
273
- get opProcessingController(): IOpProcessingController {
274
- return this._loaderContainerTracker;
275
- }
276
-
277
- /**
278
- * Create a loader. Containers created/loaded through this loader will be added to the OpProcessingController.
279
- *
280
- * Only the version of the loader will vary based on compat config. The version of
281
- * containerRuntime/dataRuntime used in fluidEntryPoint will be used as is from what is passed in.
282
- *
283
- * @param packageEntries - list of code details and fluidEntryPoint pairs.
284
- */
285
- public createLoader(
286
- packageEntries: Iterable<[IFluidCodeDetails, fluidEntryPoint]>,
287
- loaderProps?: Partial<ILoaderProps>,
288
- ) {
289
- const multiSinkLogger = new MultiSinkLogger();
290
- multiSinkLogger.addLogger(this.logger);
291
- if (loaderProps?.logger !== undefined) {
292
- multiSinkLogger.addLogger(loaderProps.logger);
293
- }
294
-
295
- const loader = new this.LoaderConstructor({
296
- ...loaderProps,
297
- logger: multiSinkLogger,
298
- codeLoader: loaderProps?.codeLoader ?? new LocalCodeLoader(packageEntries),
299
- urlResolver: loaderProps?.urlResolver ?? this.urlResolver,
300
- documentServiceFactory: loaderProps?.documentServiceFactory ?? this.documentServiceFactory,
301
- });
302
- this._loaderContainerTracker.add(loader);
303
- return loader;
304
- }
305
-
306
- /**
307
- * Create a container using a default document id and code details.
308
- * Container created is automatically added to the OpProcessingController to manage op flow
309
- *
310
- * Only the version of the loader will vary based on compat config. The version of
311
- * containerRuntime/dataRuntime used in fluidEntryPoint will be used as is from what is passed in.
312
- *
313
- * @param packageEntries - list of code details and fluidEntryPoint pairs.
314
- */
315
- public async createContainer(entryPoint: fluidEntryPoint, loaderProps?: Partial<ILoaderProps>) {
316
- if (this._documentCreated) {
317
- throw new Error(
318
- "Only one container/document can be created. To load the container/document use loadContainer");
319
- }
320
- const loader = this.createLoader([[defaultCodeDetails, entryPoint]], loaderProps);
321
- const container = await createAndAttachContainer(
322
- defaultCodeDetails,
323
- loader,
324
- this.driver.createCreateNewRequest(this.documentId),
325
- );
326
- this._documentCreated = true;
327
- // r11s driver will generate a new ID for the new container.
328
- // update the document ID with the actual ID of the attached container.
329
- this._documentIdStrategy.update(container.resolvedUrl);
330
- return container;
331
- }
332
-
333
- public async loadContainer(
334
- entryPoint: fluidEntryPoint,
335
- loaderProps?: Partial<ILoaderProps>,
336
- requestHeader?: IRequestHeader): Promise<IContainer> {
337
- const loader = this.createLoader([[defaultCodeDetails, entryPoint]], loaderProps);
338
- return loader.resolve({ url: await this.driver.createContainerUrl(this.documentId), headers: requestHeader });
339
- }
340
-
341
- /**
342
- * Make a test loader. Containers created/loaded through this loader will be added to the OpProcessingController.
343
- * The version of the loader/containerRuntime/dataRuntime may vary based on compat config of the current run
344
- * @param testContainerConfig - optional configuring the test Container
345
- */
346
- public makeTestLoader(testContainerConfig?: ITestContainerConfig) {
347
- return this.createLoader(
348
- [[defaultCodeDetails, this.createFluidEntryPoint(testContainerConfig)]],
349
- testContainerConfig?.loaderProps,
350
- );
351
- }
352
-
353
- /**
354
- * Make a container using a default document id and code details
355
- * Container loaded is automatically added to the OpProcessingController to manage op flow
356
- * @param testContainerConfig - optional configuring the test Container
357
- */
358
- public async makeTestContainer(testContainerConfig?: ITestContainerConfig): Promise<IContainer> {
359
- if (this._documentCreated) {
360
- throw new Error(
361
- "Only one container/document can be created. To load the container/document use loadTestContainer");
362
- }
363
- const loader = this.makeTestLoader(testContainerConfig);
364
- const container =
365
- await createAndAttachContainer(
366
- defaultCodeDetails,
367
- loader,
368
- this.driver.createCreateNewRequest(this.documentId));
369
- this._documentCreated = true;
370
- // r11s driver will generate a new ID for the new container.
371
- // update the document ID with the actual ID of the attached container.
372
- this._documentIdStrategy.update(container.resolvedUrl);
373
- return container;
374
- }
375
-
376
- /**
377
- * Load a container using a default document id and code details.
378
- * IContainer loaded is automatically added to the OpProcessingController to manage op flow
379
- * @param testContainerConfig - optional configuring the test Container
380
- * @param requestHeader - optional headers to be supplied to the loader
381
- */
382
- public async loadTestContainer(
383
- testContainerConfig?: ITestContainerConfig,
384
- requestHeader?: IRequestHeader,
385
- ): Promise<IContainer> {
386
- const loader = this.makeTestLoader(testContainerConfig);
387
- const container = await loader.resolve({
388
- url: await this.driver.createContainerUrl(this.documentId),
389
- headers: requestHeader,
390
- });
391
- await this.waitContainerToCatchUp(container);
392
-
393
- return container;
394
- }
395
-
396
- public reset() {
397
- this._loaderContainerTracker.reset();
398
- this._documentServiceFactory = undefined;
399
- this._urlResolver = undefined;
400
- this._documentIdStrategy.reset();
401
- const logError = getUnexpectedLogErrorException(this._logger);
402
- if (logError) {
403
- throw logError;
404
- }
405
- this._logger = undefined;
406
- this._documentCreated = false;
407
- }
408
-
409
- public async ensureSynchronized(timeoutDuration?: number): Promise<void> {
410
- return this._loaderContainerTracker.ensureSynchronizedWithTimeout
411
- ? this._loaderContainerTracker.ensureSynchronizedWithTimeout(timeoutDuration)
412
- : this._loaderContainerTracker.ensureSynchronized();
413
- }
414
-
415
- public async waitContainerToCatchUp(container: IContainer) {
416
- // The original waitContainerToCatchUp() from container loader uses either Container.resume()
417
- // or Container.connect() as part of its implementation. However, resume() was deprecated
418
- // and eventually replaced with connect(). To avoid issues during LTS compatibility testing
419
- // with older container versions issues, we use resume() when connect() is unavailable.
420
- if ((container as any).connect === undefined) {
421
- (container as any).connect = (container as any).resume;
422
- }
423
-
424
- return waitContainerToCatchUp_original(container);
425
- }
426
-
427
- updateDocumentId(resolvedUrl: IResolvedUrl | undefined) {
428
- this._documentIdStrategy.update(resolvedUrl);
429
- }
430
-
431
- public resetLoaderContainerTracker(syncSummarizerClients: boolean = false) {
432
- this._loaderContainerTracker.reset();
433
- this._loaderContainerTracker = new LoaderContainerTracker(syncSummarizerClients);
434
- }
247
+ private _loaderContainerTracker = new LoaderContainerTracker();
248
+ private _documentServiceFactory: IDocumentServiceFactory | undefined;
249
+ private _urlResolver: IUrlResolver | undefined;
250
+ private _logger: EventAndErrorTrackingLogger | undefined;
251
+ private readonly _documentIdStrategy: IDocumentIdStrategy;
252
+ // Since documentId doesn't change we can only create/make one container. Call the load functions instead.
253
+ private _documentCreated = false;
254
+
255
+ /**
256
+ * Manage objects for loading and creating container, including the driver, loader, and OpProcessingController
257
+ * @param createFluidEntryPoint - callback to create a fluidEntryPoint, with an optional set of channel name
258
+ * and factory for TestFluidObject
259
+ */
260
+ constructor(
261
+ public readonly LoaderConstructor: typeof Loader,
262
+ public readonly driver: ITestDriver,
263
+ public readonly createFluidEntryPoint: (
264
+ testContainerConfig?: ITestContainerConfig,
265
+ ) => fluidEntryPoint,
266
+ ) {
267
+ this._documentIdStrategy = getDocumentIdStrategy(driver.type);
268
+ }
269
+
270
+ get logger(): EventAndErrorTrackingLogger {
271
+ if (this._logger === undefined) {
272
+ this._logger = new EventAndErrorTrackingLogger(
273
+ ChildLogger.create(getTestLogger?.(), undefined, {
274
+ all: {
275
+ driverType: this.driver.type,
276
+ driverEndpointName: this.driver.endpointName,
277
+ driverTenantName: this.driver.tenantName,
278
+ driverUserIndex: this.driver.userIndex,
279
+ },
280
+ }),
281
+ );
282
+ }
283
+ return this._logger;
284
+ }
285
+
286
+ set logger(logger: EventAndErrorTrackingLogger) {
287
+ this._logger = logger;
288
+ }
289
+
290
+ get documentServiceFactory() {
291
+ if (!this._documentServiceFactory) {
292
+ this._documentServiceFactory = this.driver.createDocumentServiceFactory();
293
+ }
294
+ return this._documentServiceFactory;
295
+ }
296
+
297
+ get urlResolver() {
298
+ if (!this._urlResolver) {
299
+ this._urlResolver = this.driver.createUrlResolver();
300
+ }
301
+ return this._urlResolver;
302
+ }
303
+
304
+ get documentId() {
305
+ return this._documentIdStrategy.get();
306
+ }
307
+
308
+ get defaultCodeDetails() {
309
+ return defaultCodeDetails;
310
+ }
311
+
312
+ get opProcessingController(): IOpProcessingController {
313
+ return this._loaderContainerTracker;
314
+ }
315
+
316
+ /**
317
+ * Create a loader. Containers created/loaded through this loader will be added to the OpProcessingController.
318
+ *
319
+ * Only the version of the loader will vary based on compat config. The version of
320
+ * containerRuntime/dataRuntime used in fluidEntryPoint will be used as is from what is passed in.
321
+ *
322
+ * @param packageEntries - list of code details and fluidEntryPoint pairs.
323
+ */
324
+ public createLoader(
325
+ packageEntries: Iterable<[IFluidCodeDetails, fluidEntryPoint]>,
326
+ loaderProps?: Partial<ILoaderProps>,
327
+ ) {
328
+ const multiSinkLogger = new MultiSinkLogger();
329
+ multiSinkLogger.addLogger(this.logger);
330
+ if (loaderProps?.logger !== undefined) {
331
+ multiSinkLogger.addLogger(loaderProps.logger);
332
+ }
333
+
334
+ const loader = new this.LoaderConstructor({
335
+ ...loaderProps,
336
+ logger: multiSinkLogger,
337
+ codeLoader: loaderProps?.codeLoader ?? new LocalCodeLoader(packageEntries),
338
+ urlResolver: loaderProps?.urlResolver ?? this.urlResolver,
339
+ documentServiceFactory:
340
+ loaderProps?.documentServiceFactory ?? this.documentServiceFactory,
341
+ });
342
+ this._loaderContainerTracker.add(loader);
343
+ return loader;
344
+ }
345
+
346
+ /**
347
+ * Create a container using a default document id and code details.
348
+ * Container created is automatically added to the OpProcessingController to manage op flow
349
+ *
350
+ * Only the version of the loader will vary based on compat config. The version of
351
+ * containerRuntime/dataRuntime used in fluidEntryPoint will be used as is from what is passed in.
352
+ *
353
+ * @param packageEntries - list of code details and fluidEntryPoint pairs.
354
+ */
355
+ public async createContainer(entryPoint: fluidEntryPoint, loaderProps?: Partial<ILoaderProps>) {
356
+ if (this._documentCreated) {
357
+ throw new Error(
358
+ "Only one container/document can be created. To load the container/document use loadContainer",
359
+ );
360
+ }
361
+ const loader = this.createLoader([[defaultCodeDetails, entryPoint]], loaderProps);
362
+ const container = await createAndAttachContainer(
363
+ defaultCodeDetails,
364
+ loader,
365
+ this.driver.createCreateNewRequest(this.documentId),
366
+ );
367
+ this._documentCreated = true;
368
+ // r11s driver will generate a new ID for the new container.
369
+ // update the document ID with the actual ID of the attached container.
370
+ this._documentIdStrategy.update(container.resolvedUrl);
371
+ return container;
372
+ }
373
+
374
+ public async loadContainer(
375
+ entryPoint: fluidEntryPoint,
376
+ loaderProps?: Partial<ILoaderProps>,
377
+ requestHeader?: IRequestHeader,
378
+ ): Promise<IContainer> {
379
+ const loader = this.createLoader([[defaultCodeDetails, entryPoint]], loaderProps);
380
+ return loader.resolve({
381
+ url: await this.driver.createContainerUrl(this.documentId),
382
+ headers: requestHeader,
383
+ });
384
+ }
385
+
386
+ /**
387
+ * Make a test loader. Containers created/loaded through this loader will be added to the OpProcessingController.
388
+ * The version of the loader/containerRuntime/dataRuntime may vary based on compat config of the current run
389
+ * @param testContainerConfig - optional configuring the test Container
390
+ */
391
+ public makeTestLoader(testContainerConfig?: ITestContainerConfig) {
392
+ return this.createLoader(
393
+ [[defaultCodeDetails, this.createFluidEntryPoint(testContainerConfig)]],
394
+ testContainerConfig?.loaderProps,
395
+ );
396
+ }
397
+
398
+ /**
399
+ * Make a container using a default document id and code details
400
+ * Container loaded is automatically added to the OpProcessingController to manage op flow
401
+ * @param testContainerConfig - optional configuring the test Container
402
+ */
403
+ public async makeTestContainer(
404
+ testContainerConfig?: ITestContainerConfig,
405
+ ): Promise<IContainer> {
406
+ if (this._documentCreated) {
407
+ throw new Error(
408
+ "Only one container/document can be created. To load the container/document use loadTestContainer",
409
+ );
410
+ }
411
+ const loader = this.makeTestLoader(testContainerConfig);
412
+ const container = await createAndAttachContainer(
413
+ defaultCodeDetails,
414
+ loader,
415
+ this.driver.createCreateNewRequest(this.documentId),
416
+ );
417
+ this._documentCreated = true;
418
+ // r11s driver will generate a new ID for the new container.
419
+ // update the document ID with the actual ID of the attached container.
420
+ this._documentIdStrategy.update(container.resolvedUrl);
421
+ return container;
422
+ }
423
+
424
+ /**
425
+ * Load a container using a default document id and code details.
426
+ * IContainer loaded is automatically added to the OpProcessingController to manage op flow
427
+ * @param testContainerConfig - optional configuring the test Container
428
+ * @param requestHeader - optional headers to be supplied to the loader
429
+ */
430
+ public async loadTestContainer(
431
+ testContainerConfig?: ITestContainerConfig,
432
+ requestHeader?: IRequestHeader,
433
+ ): Promise<IContainer> {
434
+ const loader = this.makeTestLoader(testContainerConfig);
435
+ const container = await loader.resolve({
436
+ url: await this.driver.createContainerUrl(this.documentId),
437
+ headers: requestHeader,
438
+ });
439
+ await this.waitContainerToCatchUp(container);
440
+
441
+ return container;
442
+ }
443
+
444
+ public reset() {
445
+ this._loaderContainerTracker.reset();
446
+ this._documentServiceFactory = undefined;
447
+ this._urlResolver = undefined;
448
+ this._documentIdStrategy.reset();
449
+ const logError = getUnexpectedLogErrorException(this._logger);
450
+ if (logError) {
451
+ throw logError;
452
+ }
453
+ this._logger = undefined;
454
+ this._documentCreated = false;
455
+ }
456
+
457
+ public async ensureSynchronized(timeoutDuration?: number): Promise<void> {
458
+ return this._loaderContainerTracker.ensureSynchronizedWithTimeout
459
+ ? this._loaderContainerTracker.ensureSynchronizedWithTimeout(timeoutDuration)
460
+ : this._loaderContainerTracker.ensureSynchronized();
461
+ }
462
+
463
+ public async waitContainerToCatchUp(container: IContainer) {
464
+ // The original waitContainerToCatchUp() from container loader uses either Container.resume()
465
+ // or Container.connect() as part of its implementation. However, resume() was deprecated
466
+ // and eventually replaced with connect(). To avoid issues during LTS compatibility testing
467
+ // with older container versions issues, we use resume() when connect() is unavailable.
468
+ if ((container as any).connect === undefined) {
469
+ (container as any).connect = (container as any).resume;
470
+ }
471
+
472
+ return waitContainerToCatchUp_original(container);
473
+ }
474
+
475
+ updateDocumentId(resolvedUrl: IResolvedUrl | undefined) {
476
+ this._documentIdStrategy.update(resolvedUrl);
477
+ }
478
+
479
+ public resetLoaderContainerTracker(syncSummarizerClients: boolean = false) {
480
+ this._loaderContainerTracker.reset();
481
+ this._loaderContainerTracker = new LoaderContainerTracker(syncSummarizerClients);
482
+ }
435
483
  }
436
484
 
437
- export function getUnexpectedLogErrorException(logger: EventAndErrorTrackingLogger | undefined, prefix?: string) {
438
- if (logger === undefined) {
439
- return;
440
- }
441
- const results = logger.reportAndClearTrackedEvents();
442
- if (results.unexpectedErrors.length > 0) {
443
- return new Error(
444
- `${prefix ?? ""}Unexpected Errors in Logs:\n${JSON.stringify(results.unexpectedErrors, undefined, 2)}`);
445
- }
446
- if (results.expectedNotFound.length > 0) {
447
- return new Error(
448
- `${prefix ?? ""}Expected Events not found:\n${JSON.stringify(results.expectedNotFound, undefined, 2)}`);
449
- }
485
+ export function getUnexpectedLogErrorException(
486
+ logger: EventAndErrorTrackingLogger | undefined,
487
+ prefix?: string,
488
+ ) {
489
+ if (logger === undefined) {
490
+ return;
491
+ }
492
+ const results = logger.reportAndClearTrackedEvents();
493
+ if (results.unexpectedErrors.length > 0) {
494
+ return new Error(
495
+ `${prefix ?? ""}Unexpected Errors in Logs:\n${JSON.stringify(
496
+ results.unexpectedErrors,
497
+ undefined,
498
+ 2,
499
+ )}`,
500
+ );
501
+ }
502
+ if (results.expectedNotFound.length > 0) {
503
+ return new Error(
504
+ `${prefix ?? ""}Expected Events not found:\n${JSON.stringify(
505
+ results.expectedNotFound,
506
+ undefined,
507
+ 2,
508
+ )}`,
509
+ );
510
+ }
450
511
  }