Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if watchdogs are closed before operating on them #12

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/watchdog.toit
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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_

Expand All @@ -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_
Expand All @@ -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"
32 changes: 30 additions & 2 deletions tests/throw-close-test.toit
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,40 @@

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
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
Loading