Tests for dispatchQosStart()
parent
c516d7fc9f
commit
bdef67704f
|
@ -17,13 +17,10 @@ import I from "i18next";
|
||||||
|
|
||||||
describe("entry file", () => {
|
describe("entry file", () => {
|
||||||
it("Calls the expected callbacks", async () => {
|
it("Calls the expected callbacks", async () => {
|
||||||
console.log = jest.fn();
|
|
||||||
|
|
||||||
await import("../entry");
|
await import("../entry");
|
||||||
|
|
||||||
expect(stopIE).toHaveBeenCalled();
|
expect(stopIE).toHaveBeenCalled();
|
||||||
expect(detectLanguage).toHaveBeenCalled();
|
expect(detectLanguage).toHaveBeenCalled();
|
||||||
expect(I.init).toHaveBeenCalled();
|
expect(I.init).toHaveBeenCalled();
|
||||||
expect(console.log).toHaveBeenCalledWith("ABCD");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,17 +1,44 @@
|
||||||
const mockRedux = { store: { dispatch: jest.fn() } };
|
jest.mock("../../redux/store", () => {
|
||||||
jest.mock("../../redux/store", () => mockRedux);
|
return {
|
||||||
|
store: {
|
||||||
|
dispatch: jest.fn(),
|
||||||
|
getState: jest.fn((): DeepPartial<Everything> => ({
|
||||||
|
bot: {
|
||||||
|
connectivity: {
|
||||||
|
pings: {
|
||||||
|
"already_complete": {
|
||||||
|
kind: "complete",
|
||||||
|
start: 1,
|
||||||
|
end: 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
jest.mock("../auto_sync_handle_inbound", () => ({ handleInbound: jest.fn() }));
|
jest.mock("../auto_sync_handle_inbound", () => ({ handleInbound: jest.fn() }));
|
||||||
|
|
||||||
import { dispatchNetworkUp, dispatchNetworkDown } from "../index";
|
import { dispatchNetworkUp, dispatchNetworkDown, dispatchQosStart, networkUptimeThrottleStats } from "../index";
|
||||||
import { networkUp, networkDown } from "../actions";
|
import { networkUp, networkDown } from "../actions";
|
||||||
import { GetState } from "../../redux/interfaces";
|
import { GetState } from "../../redux/interfaces";
|
||||||
import { autoSync, routeMqttData } from "../auto_sync";
|
import { autoSync, routeMqttData } from "../auto_sync";
|
||||||
import { handleInbound } from "../auto_sync_handle_inbound";
|
import { handleInbound } from "../auto_sync_handle_inbound";
|
||||||
|
import { store } from "../../redux/store";
|
||||||
|
import { DeepPartial } from "redux";
|
||||||
|
import { Everything } from "../../interfaces";
|
||||||
|
import { now } from "../../devices/connectivity/qos";
|
||||||
|
|
||||||
const NOW = new Date();
|
const NOW = new Date();
|
||||||
const SHORT_TIME_LATER = new Date(NOW.getTime() + 500).getTime();
|
const SHORT_TIME_LATER = new Date(NOW.getTime() + 500).getTime();
|
||||||
const LONGER_TIME_LATER = new Date(NOW.getTime() + 5000).getTime();
|
const LONGER_TIME_LATER = new Date(NOW.getTime() + 5000).getTime();
|
||||||
|
const resetStats = () => {
|
||||||
|
networkUptimeThrottleStats["user.api"] = 0;
|
||||||
|
networkUptimeThrottleStats["user.mqtt"] = 0;
|
||||||
|
networkUptimeThrottleStats["bot.mqtt"] = 0;
|
||||||
|
};
|
||||||
|
|
||||||
describe("dispatchNetworkUp", () => {
|
describe("dispatchNetworkUp", () => {
|
||||||
const NOW_UP = networkUp("bot.mqtt", NOW.getTime());
|
const NOW_UP = networkUp("bot.mqtt", NOW.getTime());
|
||||||
|
@ -19,25 +46,36 @@ describe("dispatchNetworkUp", () => {
|
||||||
|
|
||||||
it("calls redux directly", () => {
|
it("calls redux directly", () => {
|
||||||
dispatchNetworkUp("bot.mqtt", NOW.getTime());
|
dispatchNetworkUp("bot.mqtt", NOW.getTime());
|
||||||
expect(mockRedux.store.dispatch).toHaveBeenLastCalledWith(NOW_UP);
|
expect(store.dispatch).toHaveBeenLastCalledWith(NOW_UP);
|
||||||
dispatchNetworkUp("bot.mqtt", SHORT_TIME_LATER);
|
dispatchNetworkUp("bot.mqtt", SHORT_TIME_LATER);
|
||||||
expect(mockRedux.store.dispatch).toHaveBeenLastCalledWith(NOW_UP);
|
expect(store.dispatch).toHaveBeenLastCalledWith(NOW_UP);
|
||||||
dispatchNetworkUp("bot.mqtt", LONGER_TIME_LATER);
|
dispatchNetworkUp("bot.mqtt", LONGER_TIME_LATER);
|
||||||
expect(mockRedux.store.dispatch).toHaveBeenLastCalledWith(LATER_UP);
|
expect(store.dispatch).toHaveBeenLastCalledWith(LATER_UP);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("dispatchNetworkDown", () => {
|
describe("dispatchNetworkDown", () => {
|
||||||
const NOW_DOWN = networkDown("user.api", NOW.getTime());
|
const NOW_DOWN = networkDown("user.api", NOW.getTime());
|
||||||
const LATER_DOWN = networkDown("user.api", LONGER_TIME_LATER);
|
const LATER_DOWN = networkDown("user.api", LONGER_TIME_LATER);
|
||||||
|
beforeEach(resetStats);
|
||||||
|
|
||||||
it("calls redux directly", () => {
|
it("calls redux directly", () => {
|
||||||
dispatchNetworkDown("user.api", NOW.getTime());
|
dispatchNetworkDown("user.api", NOW.getTime());
|
||||||
expect(mockRedux.store.dispatch).toHaveBeenLastCalledWith(NOW_DOWN);
|
expect(store.dispatch).toHaveBeenLastCalledWith(NOW_DOWN);
|
||||||
dispatchNetworkDown("user.api", SHORT_TIME_LATER);
|
dispatchNetworkDown("user.api", SHORT_TIME_LATER);
|
||||||
expect(mockRedux.store.dispatch).toHaveBeenLastCalledWith(NOW_DOWN);
|
expect(store.dispatch).toHaveBeenLastCalledWith(NOW_DOWN);
|
||||||
dispatchNetworkDown("user.api", LONGER_TIME_LATER);
|
dispatchNetworkDown("user.api", LONGER_TIME_LATER);
|
||||||
expect(mockRedux.store.dispatch).toHaveBeenLastCalledWith(LATER_DOWN);
|
expect(store.dispatch).toHaveBeenLastCalledWith(LATER_DOWN);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not falsely mark network of being down", () => {
|
||||||
|
// This test uses mocked state.
|
||||||
|
// Please see `jest.mock` calls above.
|
||||||
|
dispatchNetworkDown("bot.mqtt", now(), "already_complete");
|
||||||
|
expect(store.dispatch).not.toHaveBeenCalled();
|
||||||
|
resetStats();
|
||||||
|
dispatchNetworkDown("bot.mqtt", now(), "not_complete");
|
||||||
|
expect(store.dispatch).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -52,3 +90,12 @@ describe("autoSync", () => {
|
||||||
expect(handleInbound).toHaveBeenCalledWith(dispatch, getState, rmd);
|
expect(handleInbound).toHaveBeenCalledWith(dispatch, getState, rmd);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("dispatchQosStart", () => {
|
||||||
|
it("dispatches when a QoS ping is starting", () => {
|
||||||
|
const id = "hello";
|
||||||
|
dispatchQosStart(id);
|
||||||
|
expect(store.dispatch)
|
||||||
|
.toHaveBeenCalledWith({ type: "START_QOS_PING", payload: { id } });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -11,18 +11,20 @@ unavoidable. */
|
||||||
/** throttle calls to these functions to avoid unnecessary re-paints. */
|
/** throttle calls to these functions to avoid unnecessary re-paints. */
|
||||||
const SLOWDOWN_TIME = 1500;
|
const SLOWDOWN_TIME = 1500;
|
||||||
|
|
||||||
const lastCalledAt: Record<Edge, number> = {
|
export const networkUptimeThrottleStats: Record<Edge, number> = {
|
||||||
"user.api": 0, "user.mqtt": 0, "bot.mqtt": 0
|
"user.api": 0,
|
||||||
|
"user.mqtt": 0,
|
||||||
|
"bot.mqtt": 0
|
||||||
};
|
};
|
||||||
|
|
||||||
function shouldThrottle(edge: Edge, now: number): boolean {
|
function shouldThrottle(edge: Edge, now: number): boolean {
|
||||||
const then = lastCalledAt[edge];
|
const then = networkUptimeThrottleStats[edge];
|
||||||
const diff = now - then;
|
const diff = now - then;
|
||||||
return diff < SLOWDOWN_TIME;
|
return diff < SLOWDOWN_TIME;
|
||||||
}
|
}
|
||||||
|
|
||||||
function bumpThrottle(edge: Edge, now: number) {
|
function bumpThrottle(edge: Edge, now: number) {
|
||||||
lastCalledAt[edge] = now;
|
networkUptimeThrottleStats[edge] = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const dispatchQosStart = (id: string) => {
|
export const dispatchQosStart = (id: string) => {
|
||||||
|
@ -56,7 +58,7 @@ export let dispatchNetworkDown = (edge: Edge, at: number, qosPingId?: string) =>
|
||||||
// so we need to add a means of cancelling the
|
// so we need to add a means of cancelling the
|
||||||
// "network down" action if the request completed
|
// "network down" action if the request completed
|
||||||
// before the timeout.
|
// before the timeout.
|
||||||
if (qosPingId && pingAlreadyComplete(qosPingId)) { return; }
|
if (pingAlreadyComplete(qosPingId)) { return; }
|
||||||
store.dispatch(networkDown(edge, at, qosPingId));
|
store.dispatch(networkDown(edge, at, qosPingId));
|
||||||
bumpThrottle(edge, at);
|
bumpThrottle(edge, at);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue