Skip to content

Commit e911a05

Browse files
ROBUSTNESS: Add internal sanity checks to catch mistakes sooner
1 parent 818dff2 commit e911a05

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

R/progression_handler.R

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,21 @@ make_progression_handler <- function(name, reporter = list(), handler = NULL, en
7575
## Progress cache
7676
owner <- NULL
7777
done <- list()
78+
79+
## Sanity checks
80+
.validate_internal_state <- function(label = "<no-label>") {
81+
error <- function(...) {
82+
msg <- sprintf(...)
83+
stop(sprintf(".validate_internal_state(%s): %s", sQuote(label), msg))
84+
}
85+
if (!is.null(timestamps)) {
86+
if (length(timestamps) == 0L) error("length(timestamp) == 0L")
87+
}
88+
}
7889

7990
reporter_args <- function(progression) {
91+
.validate_internal_state("reporter_args() ... begin")
92+
8093
if (!enabled && !is.null(timestamps)) {
8194
dt <- difftime(Sys.time(), timestamps[1L], units = "secs")
8295
enabled <<- (dt >= enable_after)
@@ -100,11 +113,13 @@ make_progression_handler <- function(name, reporter = list(), handler = NULL, en
100113
)
101114
if (length(state$delta) == 0L) state$delta <- 0L
102115

116+
.validate_internal_state("reporter_args() ... end")
117+
103118
c(config, state, list(
104119
config = config,
105120
state = state,
106121
progression = progression
107-
))
122+
))
108123
}
109124

110125
reset_reporter <- function(p) {
@@ -115,6 +130,7 @@ make_progression_handler <- function(name, reporter = list(), handler = NULL, en
115130
mstr(args)
116131
}
117132
do.call(reporter$reset, args = args)
133+
.validate_internal_state("reset_reporter() ... done")
118134
if (debug) mprintf("reset_reporter() ... done")
119135
}
120136

@@ -128,6 +144,7 @@ make_progression_handler <- function(name, reporter = list(), handler = NULL, en
128144
stop_if_not(is.null(prev_milestone), length(milestones) > 0L)
129145
do.call(reporter$initiate, args = args)
130146
finished <<- FALSE
147+
.validate_internal_state("initiate_reporter() ... done")
131148
if (debug) mprintf("initiate_reporter() ... done")
132149
}
133150

@@ -140,6 +157,7 @@ make_progression_handler <- function(name, reporter = list(), handler = NULL, en
140157
}
141158
stop_if_not(!is.null(step), length(milestones) > 0L)
142159
do.call(reporter$update, args = args)
160+
.validate_internal_state("update_reporter() ... done")
143161
if (debug) mprintf("update_reporter() ... done")
144162
}
145163

@@ -152,6 +170,7 @@ make_progression_handler <- function(name, reporter = list(), handler = NULL, en
152170
}
153171
do.call(reporter$finish, args = args)
154172
finished <<- TRUE
173+
.validate_internal_state("finish_reporter() ... done")
155174
if (debug) mprintf("finish_reporter() ... done")
156175
}
157176

@@ -195,11 +214,14 @@ make_progression_handler <- function(name, reporter = list(), handler = NULL, en
195214
owner <<- NULL
196215
done <<- list()
197216
reset_reporter(p)
217+
.validate_internal_state(sprintf("handler(type=%s) ... end", type))
198218
} else if (type == "shutdown") {
199219
finish_reporter(p)
220+
.validate_internal_state(sprintf("handler(type=%s) ... end", type))
200221
} else {
201222
stop("Unknown control_progression type: ", sQuote(type))
202223
}
224+
.validate_internal_state(sprintf("control_progression ... end", type))
203225
return(invisible())
204226
}
205227

@@ -230,6 +252,8 @@ make_progression_handler <- function(name, reporter = list(), handler = NULL, en
230252

231253
if (type == "initiate") {
232254
max_steps <<- p$steps
255+
if (debug) mstr(list(max_steps=max_steps))
256+
stop_if_not(!is.null(max_steps), is.numeric(max_steps), length(max_steps) == 1L, max_steps >= 1)
233257
auto_finish <<- p$auto_finish
234258
times <- min(times, max_steps)
235259
if (debug) mstr(list(auto_finish = auto_finish, times = times, interval = interval, intrusiveness = intrusiveness))
@@ -251,16 +275,21 @@ make_progression_handler <- function(name, reporter = list(), handler = NULL, en
251275
if (debug) mstr(list(finished = finished, milestones = milestones))
252276
initiate_reporter(p)
253277
prev_milestone <<- step
278+
.validate_internal_state(sprintf("handler(type=%s) ... end", type))
254279
} else if (type == "finish") {
255280
if (debug) mstr(list(finished = finished, milestones = milestones))
256281
finish_reporter(p)
257282
timestamps[max_steps] <<- Sys.time()
258283
prev_milestone <<- max_steps
284+
.validate_internal_state()
259285
} else if (type == "update" && p$amount == 0) {
260286
if (debug) mstr(list(amount = 0, finished = finished, step = step, milestones = milestones, prev_milestone = prev_milestone, interval = interval))
261287
update_reporter(p)
288+
.validate_internal_state(sprintf("handler(type=%s, amount=0) ... end", type))
262289
} else if (type == "update") {
290+
if (debug) mstr(list(step=step, "p$amount"=p$amount, max_steps=max_steps))
263291
step <<- min(max(step + p$amount, 0L), max_steps)
292+
stop_if_not(step >= 1L)
264293
msg <- conditionMessage(p)
265294
if (length(msg) > 0) message <<- msg
266295
timestamps[step] <<- Sys.time()
@@ -283,12 +312,16 @@ make_progression_handler <- function(name, reporter = list(), handler = NULL, en
283312
finish_reporter(p)
284313
}
285314
}
315+
.validate_internal_state(sprintf("handler(type=%s) ... end", type))
286316
} else {
287317
stop("Unknown 'progression' type: ", sQuote(type))
288318
}
289-
319+
320+
## Sanity checks
321+
.validate_internal_state(sprintf("handler() ... end", type))
322+
290323
if (debug) mprintf("Progression calling handler %s ... done", sQuote(type))
291-
}
324+
} ## handler()
292325
}
293326

294327
class(handler) <- c(sprintf("%s_progression_handler", name),

0 commit comments

Comments
 (0)