1 # vim: ft=tcl foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4
3 package require Tcl 8.5
5 if {[lsearch [namespace children] ::tcltest] == -1} {
6 package require tcltest 2.2.5
7 namespace import ::tcltest::*
10 package require TLC-base
12 test scheduler-1.1 {Construct tlc::Scheduler} -setup { #<<<
13 set events_ds [tlc::DSlist ::#auto -headers {id script} -list {}]
15 tlc::Scheduler scheduler -events_ds $events_ds
16 } -result {scheduler} -cleanup {
17 if {[itcl::is object scheduler]} {
18 delete object scheduler
20 if {[info exists events_ds] && [itcl::is object $events_ds]} {
21 delete object $events_ds
26 test scheduler-1.2 {Catch missing -events_ds} -body { #<<<
27 tlc::Scheduler scheduler
31 if {[itcl::is object scheduler]} {
32 delete object scheduler
34 } -result {Must set -events_ds}
36 test scheduler-1.3 {Test event scheduling} -setup { #<<<
37 set events_ds [tlc::DSlist ::#auto -headers {id script} -list {
39 proc get_events {start_s end_s} {
42 set s [expr {(($start_s / 3600) * 3600) + 3600}]
43 while {$s <= $end_s} {
44 lappend events [list $s [list hourly_event $s]]
51 proc get_events {start_s end_s} {
54 set s [expr {(($start_s / 86400) * 86400) + 86400}]
55 while {$s <= $end_s} {
56 lappend events [list $s [list daily_event $s]]
64 # Ensure we aren't on the cusp of a point that could break the prediction
65 if {3600 - ([clock seconds] % 3600) < 3} {
69 # Predict events that will be posted <<<
70 set start_s [clock seconds]
71 set end_s [expr {$start_s + 7 * 86400}]
72 set s [expr {(([clock seconds] / 3600) * 3600) + 3600}]
77 lappend thisevent [list 1 [list hourly_event $s]]
79 if {$s % 86400 == 0} {
80 lappend thisevent [list 2 [list daily_event $s]]
82 if {[llength $thisevent] > 0} {
83 lappend predicted $s $thisevent
87 # Predict events that will be posted >>>
89 tlc::Scheduler scheduler -events_ds $events_ds
90 set events [scheduler _test_pending_events]
91 expr {$events == $predicted && [llength $events] > 0}
92 } -result {1} -cleanup {
93 if {[itcl::is object scheduler]} {
94 delete object scheduler
96 if {[info exists events_ds] && [itcl::is object $events_ds]} {
97 delete object $events_ds
102 test scheduler-1.4 {Catch error with event source script} -setup { #<<<
103 set events_ds [tlc::DSlist ::#auto -headers {id script} -list {
105 # Doesn't define get_events
109 tlc::Scheduler scheduler -events_ds $events_ds
110 } -result {scheduler} -cleanup {
111 if {[itcl::is object scheduler]} {
112 delete object scheduler
114 if {[info exists events_ds] && [itcl::is object $events_ds]} {
115 delete object $events_ds
118 } -match glob -errorOutput {*Event source 1 does not define get_events proc*}
120 test scheduler-1.5 {Test simple event dispatch} -setup { #<<<
122 proc receive_event {script_id event_data} {
123 lassign $event_data name num
124 puts stderr "received $name $num"
128 set events_ds [tlc::DSlist ::#auto -headers {id script} -list {
130 proc get_events {start_s end_s} {
133 for {set s 1} {$s <= 3} {incr s} {
134 lappend events [list [expr {$start_s + $s}] [list test $s]]
136 lappend events [list [expr {$start_s + 20}] [list test 20]]
142 # Ensure we aren't on the cusp of a point that could break the prediction
143 set delta [expr {1000 - ([clock milliseconds] % 1000)}]
144 after [expr {$delta + 100}]
145 # We should be around 102ms into this second window
147 tlc::Scheduler scheduler -events_ds $events_ds
148 scheduler register_handler event_fired receive_event
149 after 5000 {set ::flag 1}
153 if {[itcl::is object scheduler]} {
154 delete object scheduler
156 if {[info exists events_ds] && [itcl::is object $events_ds]} {
157 delete object $events_ds
160 rename receive_event {}
165 ::tcltest::cleanupTests