Skip to content

Commit

Permalink
prevent workspace restarting if user intentionally stopped it (eclips…
Browse files Browse the repository at this point in the history
…e-che#14728)

* prevent workspace restarting if user intentionally stopped it

Signed-off-by: Oleksii Kurinnyi <okurinny@redhat.com>

* fixup! prevent workspace restarting if user intentionally stopped it
  • Loading branch information
akurinnoy authored Oct 9, 2019
1 parent 5d902dc commit d7a50ea
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/workspace-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class WorkspaceLoader {
private workspace: che.workspace.Workspace;
private runtimeIsAccessible: Deferred<void>;

// `false` if workspace has been stopped intentionally
// and workspace-loader should not restart it
private allowRestart: boolean = true;

constructor(
private readonly loader: Loader,
private readonly keycloak?: any
Expand Down Expand Up @@ -204,18 +208,25 @@ export class WorkspaceLoader {
return;
}

if (message.status === 'STOPPING') {
if (message.prevStatus === 'STARTING') {
this.allowRestart = false;
}
}

if (message.status === 'STOPPED') {
if (message.prevStatus === 'STARTING') {
this.loader.error('Workspace stopped.');
this.runtimeIsAccessible.reject('Workspace stopped.');
}
if (message.prevStatus === 'STOPPING') {
if (message.prevStatus === 'STOPPING' && this.allowRestart) {
try {
await this.startWorkspace();
} catch (e) {
this.runtimeIsAccessible.reject(e);
}
}
this.allowRestart = true;
}
}

Expand Down
57 changes: 57 additions & 0 deletions test/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,63 @@ describe('If workspace status is STOPPED then workspace-loader', () => {

});

// test intensional stopping of a workspace
describe.only('If workspace status is changed from STARTING to STOPPING then workspace-loader', () => {
let statusChangeCallback: (event: che.workspace.event.WorkspaceStatusEvent) => {};
const statusStoppingEvent: che.workspace.event.WorkspaceStatusEvent = {
status: 'STOPPING',
prevStatus: 'STARTING'
};

beforeEach(done => {
spyOn(workspaceLoader, 'getWorkspaceKey').and.returnValue('foo/bar');
spyOn(workspaceLoader, 'getWorkspace').and.callFake(() =>
new Promise(resolve => {
workspaceConfig.status = 'STARTING';
resolve(workspaceConfig);
}));
spyOn(<any>workspaceLoader, 'subscribeWorkspaceEvents').and.callThrough();
spyOn(workspaceLoader, 'startWorkspace').and.callFake(() => Promise.resolve());
spyOn(<any>workspaceLoader, 'connectMasterApi').and.callFake(() => {
done();
return Promise.resolve({
addListener: () => { },
subscribeEnvironmentOutput: () => { },
subscribeInstallerOutput: () => { },
subscribeWorkspaceStatus: (_workspaceId, callback) => {
statusChangeCallback = callback;
}
});
});
spyOn(workspaceLoader, 'openIDE').and.callFake(() => Promise.resolve());

workspaceLoader.load();
});

beforeEach(() => {
spyOn((<any>workspaceLoader), 'onWorkspaceStatus').and.callThrough();

statusChangeCallback(statusStoppingEvent);
statusChangeCallback(statusStoppedEvent);
});

it('should not open an IDE', () => {
expect(workspaceLoader.openIDE).not.toHaveBeenCalled();
});

it('should handle workspace status change', () => {
expect((<any>workspaceLoader).onWorkspaceStatus).toHaveBeenCalledWith(statusStoppingEvent);
});

const statusStoppedEvent: che.workspace.event.WorkspaceStatusEvent = {
status: 'STOPPED',
prevStatus: 'STOPPING'
};
it.only('should not start the workspace', () => {
expect(workspaceLoader.startWorkspace).not.toHaveBeenCalled();
});
});

describe('If workspace status is changed from STOPPING to STOPPED then workspace-loader', () => {
let statusChangeCallback: (event: che.workspace.event.WorkspaceStatusEvent) => {};
const statusStoppedEvent: che.workspace.event.WorkspaceStatusEvent = {
Expand Down

0 comments on commit d7a50ea

Please sign in to comment.