Skip to content
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

fix for flaky realm info tests #841

Merged
merged 2 commits into from
Nov 21, 2023
Merged
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
9 changes: 8 additions & 1 deletion packages/host/app/components/realm-dropdown.gts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ interface Signature {

export default class RealmDropdown extends Component<Signature> {
<template>
<BoxelDropdown @contentClass={{@contentClass}}>
<BoxelDropdown
@contentClass={{@contentClass}}
data-test-load-realms-loaded={{this.loaded}}
>
<:trigger as |bindings|>
<Button
class='realm-dropdown-trigger'
Expand Down Expand Up @@ -94,6 +97,10 @@ export default class RealmDropdown extends Component<Signature> {
this.realmInfoService.fetchAllKnownRealmInfos.perform();
}

get loaded() {
return this.realmInfoService.fetchAllKnownRealmInfos.isIdle;
}

get realms(): RealmDropdownItem[] {
let items: RealmDropdownItem[] | [] = [];
for (let [
Expand Down
59 changes: 37 additions & 22 deletions packages/host/app/services/realm-info-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Service, { service } from '@ember/service';

import { buildWaiter } from '@ember/test-waiters';

import { restartableTask } from 'ember-concurrency';
import { TrackedMap } from 'tracked-built-ins';

import {
RealmInfo,
Expand All @@ -14,11 +17,12 @@ import ENV from '@cardstack/host/config/environment';
import LoaderService from '@cardstack/host/services/loader-service';

const { ownRealmURL, otherRealmURLs } = ENV;
const waiter = buildWaiter('realm-info-service:waiter');

export default class RealmInfoService extends Service {
@service declare loaderService: LoaderService;
cachedRealmURLsForFileURL: Map<string, string> = new Map(); // Has the file url already been resolved to a realm url?
cachedRealmInfos: Map<string, RealmInfo> = new Map(); // Has the realm url already been resolved to a realm info?
cachedRealmURLsForFileURL: TrackedMap<string, string> = new TrackedMap(); // Has the file url already been resolved to a realm url?
cachedRealmInfos: TrackedMap<string, RealmInfo> = new TrackedMap(); // Has the realm url already been resolved to a realm info?

async fetchRealmURL(fileURL: string): Promise<string> {
if (this.cachedRealmURLsForFileURL.has(fileURL)) {
Expand Down Expand Up @@ -50,21 +54,26 @@ export default class RealmInfoService extends Service {
throw new Error("Must provide either 'realmUrl' or 'fileUrl'");
}

let realmURLString = realmURL
? realmURL.href
: await this.fetchRealmURL(fileURL!);

if (this.cachedRealmInfos.has(realmURLString)) {
return this.cachedRealmInfos.get(realmURLString)!;
} else {
let realmInfoResponse = await this.loaderService.loader.fetch(
`${realmURLString}_info`,
{ headers: { Accept: SupportedMimeType.RealmInfo } },
);

let realmInfo = (await realmInfoResponse.json())?.data?.attributes;
this.cachedRealmInfos.set(realmURLString, realmInfo);
return realmInfo;
let token = waiter.beginAsync();
try {
let realmURLString = realmURL
? realmURL.href
: await this.fetchRealmURL(fileURL!);

if (this.cachedRealmInfos.has(realmURLString)) {
return this.cachedRealmInfos.get(realmURLString)!;
} else {
let realmInfoResponse = await this.loaderService.loader.fetch(
`${realmURLString}_info`,
{ headers: { Accept: SupportedMimeType.RealmInfo } },
);

let realmInfo = (await realmInfoResponse.json())?.data?.attributes;
this.cachedRealmInfos.set(realmURLString, realmInfo);
return realmInfo;
}
} finally {
waiter.endAsync(token);
}
}

Expand All @@ -73,10 +82,16 @@ export default class RealmInfoService extends Service {
...new Set([ownRealmURL, baseRealm.url, ...otherRealmURLs]),
].map((path) => new RealmPaths(path).url);

await Promise.all(
paths.map(
async (path) => await this.fetchRealmInfo({ realmURL: new URL(path) }),
),
);
let token = waiter.beginAsync();
try {
await Promise.all(
paths.map(
async (path) =>
await this.fetchRealmInfo({ realmURL: new URL(path) }),
),
);
} finally {
waiter.endAsync(token);
}
});
}
4 changes: 0 additions & 4 deletions packages/host/tests/acceptance/code-submode/editor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,6 @@ module('Acceptance | code submode | editor tests', function (hooks) {
setMonacoContent('Hello Mars');

await waitFor('[data-test-save-idle]');

await percySnapshot(assert);
});

test<TestContextWithSave>('unsaved changes made in monaco editor are saved when switching out of code submode', async function (assert) {
Expand Down Expand Up @@ -767,8 +765,6 @@ module('Acceptance | code submode | editor tests', function (hooks) {
},
});

await percySnapshot(assert);

let fileRef = await adapter.openFile('pet.gts');
if (!fileRef) {
throw new Error('file not found');
Expand Down