Tests for dispatchQosStart()
parent
c516d7fc9f
commit
bdef67704f
|
@ -17,13 +17,10 @@ import I from "i18next";
|
|||
|
||||
describe("entry file", () => {
|
||||
it("Calls the expected callbacks", async () => {
|
||||
console.log = jest.fn();
|
||||
|
||||
await import("../entry");
|
||||
|
||||
expect(stopIE).toHaveBeenCalled();
|
||||
expect(detectLanguage).toHaveBeenCalled();
|
||||
expect(I.init).toHaveBeenCalled();
|
||||
expect(console.log).toHaveBeenCalledWith("ABCD");
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,17 +1,44 @@
|
|||
const mockRedux = { store: { dispatch: jest.fn() } };
|
||||
jest.mock("../../redux/store", () => mockRedux);
|
||||
jest.mock("../../redux/store", () => {
|
||||
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() }));
|
||||
|
||||
import { dispatchNetworkUp, dispatchNetworkDown } from "../index";
|
||||
import { dispatchNetworkUp, dispatchNetworkDown, dispatchQosStart, networkUptimeThrottleStats } from "../index";
|
||||
import { networkUp, networkDown } from "../actions";
|
||||
import { GetState } from "../../redux/interfaces";
|
||||
import { autoSync, routeMqttData } from "../auto_sync";
|
||||
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 SHORT_TIME_LATER = new Date(NOW.getTime() + 500).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", () => {
|
||||
const NOW_UP = networkUp("bot.mqtt", NOW.getTime());
|
||||
|
@ -19,25 +46,36 @@ describe("dispatchNetworkUp", () => {
|
|||
|
||||
it("calls redux directly", () => {
|
||||
dispatchNetworkUp("bot.mqtt", NOW.getTime());
|
||||
expect(mockRedux.store.dispatch).toHaveBeenLastCalledWith(NOW_UP);
|
||||
expect(store.dispatch).toHaveBeenLastCalledWith(NOW_UP);
|
||||
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);
|
||||
expect(mockRedux.store.dispatch).toHaveBeenLastCalledWith(LATER_UP);
|
||||
expect(store.dispatch).toHaveBeenLastCalledWith(LATER_UP);
|
||||
});
|
||||
});
|
||||
|
||||
describe("dispatchNetworkDown", () => {
|
||||
const NOW_DOWN = networkDown("user.api", NOW.getTime());
|
||||
const LATER_DOWN = networkDown("user.api", LONGER_TIME_LATER);
|
||||
beforeEach(resetStats);
|
||||
|
||||
it("calls redux directly", () => {
|
||||
dispatchNetworkDown("user.api", NOW.getTime());
|
||||
expect(mockRedux.store.dispatch).toHaveBeenLastCalledWith(NOW_DOWN);
|
||||
expect(store.dispatch).toHaveBeenLastCalledWith(NOW_DOWN);
|
||||
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);
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
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. */
|
||||
const SLOWDOWN_TIME = 1500;
|
||||
|
||||
const lastCalledAt: Record<Edge, number> = {
|
||||
"user.api": 0, "user.mqtt": 0, "bot.mqtt": 0
|
||||
export const networkUptimeThrottleStats: Record<Edge, number> = {
|
||||
"user.api": 0,
|
||||
"user.mqtt": 0,
|
||||
"bot.mqtt": 0
|
||||
};
|
||||
|
||||
function shouldThrottle(edge: Edge, now: number): boolean {
|
||||
const then = lastCalledAt[edge];
|
||||
const then = networkUptimeThrottleStats[edge];
|
||||
const diff = now - then;
|
||||
return diff < SLOWDOWN_TIME;
|
||||
}
|
||||
|
||||
function bumpThrottle(edge: Edge, now: number) {
|
||||
lastCalledAt[edge] = now;
|
||||
networkUptimeThrottleStats[edge] = now;
|
||||
}
|
||||
|
||||
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
|
||||
// "network down" action if the request completed
|
||||
// before the timeout.
|
||||
if (qosPingId && pingAlreadyComplete(qosPingId)) { return; }
|
||||
if (pingAlreadyComplete(qosPingId)) { return; }
|
||||
store.dispatch(networkDown(edge, at, qosPingId));
|
||||
bumpThrottle(edge, at);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue