Skip to content

Dispose realtime docs when no longer in use #3199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .grit/patterns/no_mock_patterns_outside_mocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
level: error
---

# No misused mock patterns outside of mocks

`something.something(anything())` is wrong unless it's inside a `when` or `verify` call.

```grit
`$functionCall()` where $functionCall <: and {
or {
`anyOfClass`,
`anyFunction`,
`anyNumber`,
`anyString`,
`anything`
},
and {
not within `when($_)`,
not within `verify($_)`,
}
}

```

## Positive example

```ts
console.log(anything());
```

```ts
console.log(anything());
```

## Negative example using when()

```ts
when(something.something(anything())).thenReturn(somethingElse);
```

## Negative example using verify()

```ts
verify(something.something(anything())).once();
```
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ async function joinAsChecker(
// Give time for the last answer to be saved
await page.waitForTimeout(500);
} catch (e) {
await page.pause();
console.error('Error running tests for checker ' + userNumber);
console.error(e);
await screenshot(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ServalAdminAuthGuard } from './serval-administration/serval-admin-auth.
import { ServalAdministrationComponent } from './serval-administration/serval-administration.component';
import { ServalProjectComponent } from './serval-administration/serval-project.component';
import { SettingsComponent } from './settings/settings.component';
import { BlankPageComponent } from './shared/blank-page/blank-page.component';
import { PageNotFoundComponent } from './shared/page-not-found/page-not-found.component';
import { SettingsAuthGuard, SyncAuthGuard } from './shared/project-router.guard';
import { SyncComponent } from './sync/sync.component';
Expand All @@ -33,6 +34,7 @@ const routes: Routes = [
{ path: 'serval-administration/:projectId', component: ServalProjectComponent, canActivate: [ServalAdminAuthGuard] },
{ path: 'serval-administration', component: ServalAdministrationComponent, canActivate: [ServalAdminAuthGuard] },
{ path: 'system-administration', component: SystemAdministrationComponent, canActivate: [SystemAdminAuthGuard] },
{ path: 'blank-page', component: BlankPageComponent },
{ path: '**', component: PageNotFoundComponent }
];

Expand Down
46 changes: 35 additions & 11 deletions src/SIL.XForge.Scripture/ClientApp/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { ExternalUrlService } from 'xforge-common/external-url.service';
import { FileService } from 'xforge-common/file.service';
import { I18nService } from 'xforge-common/i18n.service';
import { LocationService } from 'xforge-common/location.service';
import { DocSubscription } from 'xforge-common/models/realtime-doc';
import { UserDoc } from 'xforge-common/models/user-doc';
import { NoticeService } from 'xforge-common/notice.service';
import { OnlineStatusService } from 'xforge-common/online-status.service';
Expand Down Expand Up @@ -678,11 +679,12 @@ class TestEnvironment {
this.addProjectUserConfig('project01', 'user03');
this.addProjectUserConfig('project01', 'user04');

when(mockedSFProjectService.getProfile(anything())).thenCall(projectId =>
this.realtimeService.subscribe(SFProjectProfileDoc.COLLECTION, projectId)
when(mockedSFProjectService.getProfile(anything(), anything())).thenCall((projectId, subscription) =>
this.realtimeService.subscribe(SFProjectProfileDoc.COLLECTION, projectId, subscription)
);
when(mockedSFProjectService.getUserConfig(anything(), anything())).thenCall((projectId, userId) =>
this.realtimeService.subscribe(SFProjectUserConfigDoc.COLLECTION, `${projectId}:${userId}`)
when(mockedSFProjectService.getUserConfig(anything(), anything(), anything())).thenCall(
(projectId, userId, subscriber) =>
this.realtimeService.subscribe(SFProjectUserConfigDoc.COLLECTION, `${projectId}:${userId}`, subscriber)
);
when(mockedLocationService.pathname).thenReturn('/projects/project01/checking');

Expand Down Expand Up @@ -792,12 +794,14 @@ class TestEnvironment {
}

get currentUserDoc(): UserDoc {
return this.realtimeService.get(UserDoc.COLLECTION, 'user01');
return this.realtimeService.get(UserDoc.COLLECTION, 'user01', new DocSubscription('spec'));
}

setCurrentUser(userId: string): void {
when(mockedUserService.currentUserId).thenReturn(userId);
when(mockedUserService.getCurrentUser()).thenCall(() => this.realtimeService.subscribe(UserDoc.COLLECTION, userId));
when(mockedUserService.getCurrentUser()).thenCall(() =>
this.realtimeService.subscribe(UserDoc.COLLECTION, userId, new DocSubscription('spec'))
);
}

triggerLogin(): void {
Expand Down Expand Up @@ -865,33 +869,53 @@ class TestEnvironment {
when(mockedUserService.currentProjectId(anything())).thenReturn(undefined);
}
this.ngZone.run(() => {
const projectDoc = this.realtimeService.get(SFProjectProfileDoc.COLLECTION, projectId);
const projectDoc = this.realtimeService.get(
SFProjectProfileDoc.COLLECTION,
projectId,
new DocSubscription('spec')
);
projectDoc.delete();
});
this.wait();
}

removeUserFromProject(projectId: string): void {
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, projectId);
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
projectId,
new DocSubscription('spec')
);
projectDoc.submitJson0Op(op => op.unset<string>(p => p.userRoles['user01']), false);
this.wait();
}

updatePreTranslate(projectId: string): void {
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, projectId);
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
projectId,
new DocSubscription('spec')
);
projectDoc.submitJson0Op(op => op.set<boolean>(p => p.translateConfig.preTranslate, true), false);
this.wait();
}

addUserToProject(projectId: string): void {
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, projectId);
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
projectId,
new DocSubscription('spec')
);
projectDoc.submitJson0Op(op => op.set<string>(p => p.userRoles['user01'], SFProjectRole.CommunityChecker), false);
this.currentUserDoc.submitJson0Op(op => op.add<string>(u => u.sites['sf'].projects, 'project04'), false);
this.wait();
}

changeUserRole(projectId: string, userId: string, role: SFProjectRole): void {
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, projectId);
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
projectId,
new DocSubscription('spec')
);
projectDoc.submitJson0Op(op => op.set<string>(p => p.userRoles[userId], role), false);
this.wait();
}
Expand Down
4 changes: 3 additions & 1 deletion src/SIL.XForge.Scripture/ClientApp/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { FileService } from 'xforge-common/file.service';
import { I18nService } from 'xforge-common/i18n.service';
import { LocationService } from 'xforge-common/location.service';
import { Breakpoint, MediaBreakpointService } from 'xforge-common/media-breakpoints/media-breakpoint.service';
import { DocSubscription } from 'xforge-common/models/realtime-doc';
import { UserDoc } from 'xforge-common/models/user-doc';
import { NoticeService } from 'xforge-common/notice.service';
import { OnlineStatusService } from 'xforge-common/online-status.service';
Expand Down Expand Up @@ -282,7 +283,8 @@ export class AppComponent extends DataLoadingComponent implements OnInit, OnDest
this.userService.setCurrentProjectId(this.currentUserDoc!, this._selectedProjectDoc.id);
this.projectUserConfigDoc = await this.projectService.getUserConfig(
this._selectedProjectDoc.id,
this.currentUserDoc!.id
this.currentUserDoc!.id,
new DocSubscription('AppComponent', this.destroyRef)
);
if (this.selectedProjectDeleteSub != null) {
this.selectedProjectDeleteSub.unsubscribe();
Expand Down
12 changes: 12 additions & 0 deletions src/SIL.XForge.Scripture/ClientApp/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { ProjectComponent } from './project/project.component';
import { ScriptureChooserDialogComponent } from './scripture-chooser-dialog/scripture-chooser-dialog.component';
import { DeleteProjectDialogComponent } from './settings/delete-project-dialog/delete-project-dialog.component';
import { SettingsComponent } from './settings/settings.component';
import { CacheService } from './shared/cache-service/cache.service';
import { GlobalNoticesComponent } from './shared/global-notices/global-notices.component';
import { SharedModule } from './shared/shared.module';
import { TextNoteDialogComponent } from './shared/text/text-note-dialog/text-note-dialog.component';
Expand All @@ -46,6 +47,11 @@ import { LynxInsightsModule } from './translate/editor/lynx/insights/lynx-insigh
import { TranslateModule } from './translate/translate.module';
import { UsersModule } from './users/users.module';

/** Initialization function for any services that need to be run but are not depended on by any component. */
function initializeGlobalServicesFactory(_cacheService: CacheService): () => Promise<any> {
return () => Promise.resolve();
}

@NgModule({
declarations: [
AppComponent,
Expand Down Expand Up @@ -97,6 +103,12 @@ import { UsersModule } from './users/users.module';
{ provide: ErrorHandler, useClass: ExceptionHandlingService },
{ provide: OverlayContainer, useClass: InAppRootOverlayContainer },
provideHttpClient(withInterceptorsFromDi()),
{
provide: APP_INITIALIZER,
useFactory: initializeGlobalServicesFactory,
deps: [CacheService],
multi: true
},
{
provide: APP_INITIALIZER,
useFactory: preloadEnglishTranslations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { VerseRefData } from 'realtime-server/lib/esm/scriptureforge/models/vers
import { of } from 'rxjs';
import { anything, mock, resetCalls, verify, when } from 'ts-mockito';
import { DialogService } from 'xforge-common/dialog.service';
import { DocSubscription } from 'xforge-common/models/realtime-doc';
import { NoticeService } from 'xforge-common/notice.service';
import { OnlineStatusService } from 'xforge-common/online-status.service';
import { noopDestroyRef } from 'xforge-common/realtime.service';
Expand Down Expand Up @@ -420,7 +421,8 @@ describe('CheckingOverviewComponent', () => {
const env = new TestEnvironment();
const questionDoc: QuestionDoc = env.realtimeService.get(
QuestionDoc.COLLECTION,
getQuestionDocId('project01', 'q7Id')
getQuestionDocId('project01', 'q7Id'),
new DocSubscription('spec')
);
await questionDoc.submitJson0Op(op => {
op.set(d => d.isArchived, false);
Expand Down Expand Up @@ -973,18 +975,26 @@ class TestEnvironment {
when(mockedActivatedRoute.params).thenReturn(of({ projectId: 'project01' }));
when(mockedQuestionDialogService.questionDialog(anything())).thenResolve();
when(mockedDialogService.confirm(anything(), anything())).thenResolve(true);
when(mockedProjectService.getProfile(anything())).thenCall(id =>
this.realtimeService.subscribe(SFProjectProfileDoc.COLLECTION, id)
when(mockedProjectService.getProfile(anything(), anything())).thenCall((id, subscription) =>
this.realtimeService.subscribe(SFProjectProfileDoc.COLLECTION, id, subscription)
);
when(mockedProjectService.getUserConfig(anything(), anything())).thenCall((id, userId) =>
this.realtimeService.subscribe(SFProjectUserConfigDoc.COLLECTION, getSFProjectUserConfigDocId(id, userId))
when(mockedProjectService.getUserConfig(anything(), anything(), anything())).thenCall((id, userId, subscriber) =>
this.realtimeService.subscribe(
SFProjectUserConfigDoc.COLLECTION,
getSFProjectUserConfigDocId(id, userId),
subscriber
)
);
when(mockedQuestionsService.queryQuestions('project01', anything(), anything())).thenCall(() =>
this.realtimeService.subscribeQuery(QuestionDoc.COLLECTION, {}, noopDestroyRef)
);
when(mockedProjectService.onlineDeleteAudioTimingData(anything(), anything(), anything())).thenCall(
(projectId, book, chapter) => {
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, projectId);
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
projectId,
new DocSubscription('spec')
);
const textIndex: number = projectDoc.data!.texts.findIndex(t => t.bookNum === book);
const chapterIndex: number = projectDoc.data!.texts[textIndex].chapters.findIndex(c => c.number === chapter);
projectDoc.submitJson0Op(op => op.set(p => p.texts[textIndex].chapters[chapterIndex].hasAudio, false), false);
Expand Down Expand Up @@ -1154,7 +1164,11 @@ class TestEnvironment {
}

setSeeOtherUserResponses(isEnabled: boolean): void {
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, 'project01');
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
'project01',
new DocSubscription('spec')
);
projectDoc.submitJson0Op(
op => op.set<boolean>(p => p.checkingConfig.usersSeeEachOthersResponses, isEnabled),
false
Expand All @@ -1164,7 +1178,11 @@ class TestEnvironment {

setCheckingEnabled(isEnabled: boolean): void {
this.ngZone.run(() => {
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, 'project01');
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
'project01',
new DocSubscription('spec')
);
projectDoc.submitJson0Op(op => op.set<boolean>(p => p.checkingConfig.checkingEnabled, isEnabled), false);
});
this.waitForProjectDocChanges();
Expand Down Expand Up @@ -1224,7 +1242,11 @@ class TestEnvironment {
],
permissions: {}
};
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(SFProjectProfileDoc.COLLECTION, 'project01');
const projectDoc = this.realtimeService.get<SFProjectProfileDoc>(
SFProjectProfileDoc.COLLECTION,
'project01',
new DocSubscription('spec')
);
const index: number = projectDoc.data!.texts.length - 1;
projectDoc.submitJson0Op(op => op.insert(p => p.texts, index, text), false);
this.addQuestion({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { DataLoadingComponent } from 'xforge-common/data-loading-component';
import { DialogService } from 'xforge-common/dialog.service';
import { I18nService } from 'xforge-common/i18n.service';
import { L10nNumberPipe } from 'xforge-common/l10n-number.pipe';
import { DocSubscription } from 'xforge-common/models/realtime-doc';
import { RealtimeQuery } from 'xforge-common/models/realtime-query';
import { NoticeService } from 'xforge-common/notice.service';
import { OnlineStatusService } from 'xforge-common/online-status.service';
Expand Down Expand Up @@ -181,7 +182,10 @@ export class CheckingOverviewComponent extends DataLoadingComponent implements O
const projectId$ = this.activatedRoute.params.pipe(
tap(params => {
this.loadingStarted();
projectDocPromise = this.projectService.getProfile(params['projectId']);
projectDocPromise = this.projectService.getProfile(
params['projectId'],
new DocSubscription('CheckingOverviewComponent', this.destroyRef)
);
}),
map(params => params['projectId'] as string)
);
Expand All @@ -190,7 +194,11 @@ export class CheckingOverviewComponent extends DataLoadingComponent implements O
this.projectId = projectId;
try {
this.projectDoc = await projectDocPromise;
this.projectUserConfigDoc = await this.projectService.getUserConfig(projectId, this.userService.currentUserId);
this.projectUserConfigDoc = await this.projectService.getUserConfig(
projectId,
this.userService.currentUserId,
new DocSubscription('CheckingOverviewComponent', this.destroyRef)
);
this.questionsQuery?.dispose();
this.questionsQuery = await this.checkingQuestionsService.queryQuestions(
projectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { expect, within } from '@storybook/test';
import { createTestUserProfile } from 'realtime-server/lib/esm/common/models/user-test-data';
import { Comment } from 'realtime-server/lib/esm/scriptureforge/models/comment';
import { createTestProject } from 'realtime-server/lib/esm/scriptureforge/models/sf-project-test-data';
import { instance, mock, when } from 'ts-mockito';
import { anything, instance, mock, when } from 'ts-mockito';
import { DialogService } from 'xforge-common/dialog.service';
import { I18nStoryModule } from 'xforge-common/i18n-story.module';
import { UserProfileDoc } from 'xforge-common/models/user-profile-doc';
Expand All @@ -17,11 +17,11 @@ import { CheckingCommentsComponent } from './checking-comments.component';
const mockedDialogService = mock(DialogService);
const mockedUserService = mock(UserService);
when(mockedUserService.currentUserId).thenReturn('user01');
when(mockedUserService.getProfile('user01')).thenResolve({
when(mockedUserService.getProfile('user01', anything())).thenResolve({
id: 'user01',
data: createTestUserProfile({}, 1)
} as UserProfileDoc);
when(mockedUserService.getProfile('user02')).thenResolve({
when(mockedUserService.getProfile('user02', anything())).thenResolve({
id: 'user02',
data: createTestUserProfile({}, 2)
} as UserProfileDoc);
Expand Down
Loading
Loading