StateTransitionMachine.loadGraph reports success (#757)

Step one towards #586. This will enable us to chain runPagerank after
loadGraph only if the load went through successfully.

Test plan: Unit tests included.
This commit is contained in:
Dandelion Mané 2018-09-03 14:33:25 -07:00 committed by GitHub
parent b92d6138d3
commit 241905f6d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 5 deletions

View File

@ -79,7 +79,7 @@ export function initialState(): AppState {
export interface StateTransitionMachineInterface { export interface StateTransitionMachineInterface {
+setRepo: (Repo) => void; +setRepo: (Repo) => void;
+setEdgeEvaluator: (EdgeEvaluator) => void; +setEdgeEvaluator: (EdgeEvaluator) => void;
+loadGraph: (Assets) => Promise<void>; +loadGraph: (Assets) => Promise<boolean>;
+runPagerank: (NodeAddressT) => Promise<void>; +runPagerank: (NodeAddressT) => Promise<void>;
} }
/* In production, instantiate via createStateTransitionMachine; the constructor /* In production, instantiate via createStateTransitionMachine; the constructor
@ -167,7 +167,8 @@ export class StateTransitionMachine implements StateTransitionMachineInterface {
} }
} }
async loadGraph(assets: Assets) { /** Loads the graph, reports whether it was successful */
async loadGraph(assets: Assets): Promise<boolean> {
const state = this.getState(); const state = this.getState();
if ( if (
state.type !== "INITIALIZED" || state.type !== "INITIALIZED" ||
@ -182,6 +183,7 @@ export class StateTransitionMachine implements StateTransitionMachineInterface {
}; };
this.setState(loadingState); this.setState(loadingState);
let newState: ?AppState; let newState: ?AppState;
let success = true;
try { try {
const graphWithAdapters = await this.loadGraphWithAdapters(assets, repo); const graphWithAdapters = await this.loadGraphWithAdapters(assets, repo);
newState = { newState = {
@ -195,10 +197,13 @@ export class StateTransitionMachine implements StateTransitionMachineInterface {
} catch (e) { } catch (e) {
console.error(e); console.error(e);
newState = {...state, substate: {...substate, loading: "FAILED"}}; newState = {...state, substate: {...substate, loading: "FAILED"}};
success = false;
} }
if (deepEqual(this.getState(), loadingState)) { if (deepEqual(this.getState(), loadingState)) {
this.setState(newState); this.setState(newState);
return success;
} }
return false;
} }
async runPagerank(totalScoreNodePrefix: NodeAddressT) { async runPagerank(totalScoreNodePrefix: NodeAddressT) {

View File

@ -226,7 +226,8 @@ describe("app/credExplorer/state", () => {
const {getState, stm, loadGraphMock} = example(readyToLoadGraph()); const {getState, stm, loadGraphMock} = example(readyToLoadGraph());
const gwa = graphWithAdapters(); const gwa = graphWithAdapters();
loadGraphMock.mockResolvedValue(gwa); loadGraphMock.mockResolvedValue(gwa);
await stm.loadGraph(new Assets("/my/gateway/")); const succeeded = await stm.loadGraph(new Assets("/my/gateway/"));
expect(succeeded).toBe(true);
const state = getState(); const state = getState();
const substate = getSubstate(state); const substate = getSubstate(state);
expect(loading(state)).toBe("NOT_LOADING"); expect(loading(state)).toBe("NOT_LOADING");
@ -246,7 +247,8 @@ describe("app/credExplorer/state", () => {
resolve(graphWithAdapters()); resolve(graphWithAdapters());
}) })
); );
await stm.loadGraph(new Assets("/my/gateway/")); const succeeded = await stm.loadGraph(new Assets("/my/gateway/"));
expect(succeeded).toBe(false);
const state = getState(); const state = getState();
const substate = getSubstate(state); const substate = getSubstate(state);
expect(loading(state)).toBe("NOT_LOADING"); expect(loading(state)).toBe("NOT_LOADING");
@ -259,7 +261,8 @@ describe("app/credExplorer/state", () => {
// $ExpectFlowError // $ExpectFlowError
console.error = jest.fn(); console.error = jest.fn();
loadGraphMock.mockRejectedValue(error); loadGraphMock.mockRejectedValue(error);
await stm.loadGraph(new Assets("/my/gateway/")); const succeeded = await stm.loadGraph(new Assets("/my/gateway/"));
expect(succeeded).toBe(false);
const state = getState(); const state = getState();
const substate = getSubstate(state); const substate = getSubstate(state);
expect(loading(state)).toBe("FAILED"); expect(loading(state)).toBe("FAILED");