Skip to content

Commit

Permalink
Add test for slow initial fetch of filebrowser model.
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-r-rose committed Aug 28, 2018
1 parent df3156e commit 33b129d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/filebrowser/src/model.ts
Expand Up @@ -248,7 +248,7 @@ export class FileBrowserModel implements IDisposable {
} else {
newValue = this._pendingPath || this._model.path;
}
if (this._pendingPath && this._pending) {
if (this._pending) {
// Collapse requests to the same directory.
if (newValue === this._pendingPath) {
return this._pending;
Expand Down
56 changes: 55 additions & 1 deletion tests/test-filebrowser/src/model.spec.ts
Expand Up @@ -11,7 +11,11 @@ import { DocumentManager, IDocumentManager } from '@jupyterlab/docmanager';

import { DocumentRegistry, TextModelFactory } from '@jupyterlab/docregistry';

import { ServiceManager } from '@jupyterlab/services';
import {
Contents,
ContentsManager,
ServiceManager
} from '@jupyterlab/services';

import {
FileBrowserModel,
Expand All @@ -26,6 +30,29 @@ import {
} from '@jupyterlab/testutils';
import { toArray } from '@phosphor/algorithm';

/**
* A contents manager that delays requests by less each time it is called
* in order to simulate out-of-order responses from the server.
*/
class DelayedContentsManager extends ContentsManager {
get(
path: string,
options?: Contents.IFetchOptions
): Promise<Contents.IModel> {
return new Promise<Contents.IModel>(resolve => {
const delay = this._delay;
this._delay -= 500;
super.get(path, options).then(contents => {
setTimeout(() => {
resolve(contents);
}, Math.max(delay, 0));
});
});
}

private _delay = 1000;
}

describe('filebrowser/model', () => {
let manager: IDocumentManager;
let serviceManager: ServiceManager.IManager;
Expand Down Expand Up @@ -223,6 +250,33 @@ describe('filebrowser/model', () => {
await model.cd('..');
expect(model.path).to.equal('');
});

it('should be resilient to a slow initial fetch', async () => {
let delayedServiceManager = new ServiceManager();
(delayedServiceManager as any).contents = new DelayedContentsManager();
let manager = new DocumentManager({
registry,
opener,
manager: delayedServiceManager
});
model = new FileBrowserModel({ manager, state });

const paths: string[] = [];
// An initial refresh is called in the constructor.
// If it is too slow, it can come in after the directory change,
// causing a directory set by, e.g., the tree handler to be wrong.
// This checks to make sure we are handling that case correctly.
const refresh = model.refresh().then(() => paths.push(model.path));
const cd = model.cd('src').then(() => paths.push(model.path));
await Promise.all([refresh, cd]);
expect(model.path).to.equal('src');
expect(paths).to.eql(['', 'src']);

manager.dispose();
delayedServiceManager.contents.dispose();
delayedServiceManager.dispose();
model.dispose();
});
});

describe('#restore()', () => {
Expand Down

0 comments on commit 33b129d

Please sign in to comment.