From bfa021aabfeca2dc17b15f3286a59bb3f048e827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Fri, 28 Sep 2018 00:02:10 +0100 Subject: [PATCH] fix(ui): don't let multiple exceptions set multiple timers As reported via #77 countdown timer might be updated more often than 1s since we don't protect against multiple setInterval() calls, add a check --- ui/src/ErrorBoundary.js | 6 +++++- ui/src/ErrorBoundary.test.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ui/src/ErrorBoundary.js b/ui/src/ErrorBoundary.js index 0f805a25f..242f73fc8 100644 --- a/ui/src/ErrorBoundary.js +++ b/ui/src/ErrorBoundary.js @@ -35,6 +35,7 @@ class ErrorBoundary extends Component { constructor(props) { super(props); + this.timer = null; this.state = { cachedError: null, reloadSeconds: 60 }; } @@ -55,7 +56,10 @@ class ErrorBoundary extends Component { }); Sentry.captureException(error); // reload after 60s, this is to fix wall monitors automatically - setInterval(this.reloadApp, 1000); + // but only if the timer isn't set yet + if (this.timer === null) { + this.timer = setInterval(this.reloadApp, 1000); + } } render() { diff --git a/ui/src/ErrorBoundary.test.js b/ui/src/ErrorBoundary.test.js index 1f050867e..f7b87412c 100644 --- a/ui/src/ErrorBoundary.test.js +++ b/ui/src/ErrorBoundary.test.js @@ -70,4 +70,19 @@ describe("", () => { expect(reloadSpy).toHaveBeenCalled(); expect(consoleSpy).toHaveBeenCalled(); }); + + it("reloadSeconds is 40 after 20s with multiple exceptions", () => { + jest.spyOn(console, "error").mockImplementation(() => {}); + const tree = MountedFailingComponent(); + const instance = tree.instance(); + + instance.componentDidCatch("foo", { foo: "bar" }); + jest.runTimersToTime(1000 * 10); + instance.componentDidCatch("foo", { foo: "bar" }); + jest.runTimersToTime(1000 * 5); + instance.componentDidCatch("foo", { foo: "bar" }); + jest.runTimersToTime(1000 * 5); + instance.componentDidCatch("foo", { foo: "bar" }); + expect(tree.instance().state.reloadSeconds).toBe(40); + }); });