diff --git a/src/watchdog.toit b/src/watchdog.toit index 2bc879f..09bf1af 100644 --- a/src/watchdog.toit +++ b/src/watchdog.toit @@ -123,6 +123,7 @@ class Watchdog extends ServiceResourceProxy: reacted upon. */ start --s/int -> none: + if is-closed: throw "ALREADY_CLOSED" is-stopped_ = false client := (client_ as WatchdogServiceClient) client.start_ handle_ s @@ -133,6 +134,7 @@ class Watchdog extends ServiceResourceProxy: Does nothing if the watchdog is not started. */ feed -> none: + if is-closed: throw "ALREADY_CLOSED" client := (client_ as WatchdogServiceClient) client.feed_ handle_ @@ -142,6 +144,7 @@ class Watchdog extends ServiceResourceProxy: Does nothing if the watchdog is not started. */ stop -> none: + if is-closed: throw "ALREADY_CLOSED" is-stopped_ = true client := (client_ as WatchdogServiceClient) client.stop_ handle_ @@ -152,4 +155,8 @@ class Watchdog extends ServiceResourceProxy: close -> none: super if not is-stopped_: - throw "WATCHDOG_NOT_STOPPED" + // Produce a stack trace to draw attention to the + // fact that a closed, non-stopped watchdog will + // eventually lead to problems, because it cannot + // be fed anymore. + catch --trace: throw "WATCHDOG_NOT_STOPPED" diff --git a/tests/throw-close-test.toit b/tests/throw-close-test.toit index bf4fc7a..0fcffa3 100644 --- a/tests/throw-close-test.toit +++ b/tests/throw-close-test.toit @@ -4,12 +4,43 @@ import expect show * import watchdog show WatchdogServiceClient +import system.services show ServiceProvider ServiceHandler +import system.api.trace show TraceService import .util main: + service := TraceServiceProvider + service.install run-test: | client/WatchdogServiceClient ms/int system-dog/FakeSystemWatchdog | dog := client.create "toit.io/test/throw-close" dog.start --s=1 - expect-throw "WATCHDOG_NOT_STOPPED": - dog.close + expect-equals 0 service.traces.size + dog.close + expect-equals 1 service.traces.size + dog.close + dog.close + expect-equals 2 service.traces.size + service.uninstall + +class TraceServiceProvider extends ServiceProvider + implements TraceService ServiceHandler: + traces_/List := [] + + constructor: + super "system/trace/test" --major=1 --minor=2 + provides TraceService.SELECTOR --handler=this + + handle index/int arguments/any --gid/int --client/int -> any: + if index == TraceService.HANDLE-TRACE-INDEX: + return handle-trace arguments + unreachable + + traces -> List: + result := traces_ + traces_ = [] + return result + + handle-trace message/ByteArray -> ByteArray?: + traces_.add message + return null