From: Matt S Trout Date: Sun, 5 Aug 2012 18:16:48 +0000 (+0000) Subject: already-ready tests X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FTenDotTcl.git;a=commitdiff_plain;h=c7db0954d5c41fd117a7cbce5e88f9a89a9a4a54 already-ready tests --- diff --git a/t/future.t b/t/future.t index 28cfd7a..1f61397 100644 --- a/t/future.t +++ b/t/future.t @@ -8,11 +8,14 @@ ok {![f1 is_ready]} "Future not yet ready" set ready_args "" set done_args "" +set do_not_set_this "" f1 on_ready [list set "[namespace current]::ready_args"] f1 on_done [list set "[namespace current]::done_args"] +f1 on_fail [list set "[namespace current]::do_not_set_this"] + f1 done foo bar ok {[f1 is_ready]} "Future ready" @@ -25,6 +28,32 @@ is [lindex $done_args 0] foo "Right first arg" is [lindex $done_args 1] bar "Right second arg" +is $do_not_set_this "" "on_fail not called" + is [f1 get] "foo bar" "get return ok" +f1 destroy + +ten::future f2 + +f2 done one two three + +set done_args "" + +f2 on_done [list set "[namespace current]::done_args"] + +is $done_args "one two three" "on_done called on completed future" + +set ready_args "" + +f2 on_ready [list set "[namespace current]::ready_args"] + +is $ready_args {::f2} "on_ready called on completed future" + +set do_not_set_this "" + +f2 on_fail [list set "[namespace current]::do_not_set_this"] + +is $do_not_set_this "" "on_fail not called on completed future" + done_testing diff --git a/ten/ten.tcl b/ten/ten.tcl index 15be14d..0e22d50 100644 --- a/ten/ten.tcl +++ b/ten/ten.tcl @@ -56,7 +56,7 @@ snit::type ten::connection { snit::type ten::future { - variable callbacks + variable callbacks "" variable is_ready 0 variable result "" variable failure "" @@ -78,15 +78,27 @@ snit::type ten::future { } method on_ready {cb_code} { - lappend callbacks [list ready $cb_code] + if [$self is_ready] { + eval [concat $cb_code $self] + } else { + lappend callbacks [list ready $cb_code] + } } method on_done {cb_code} { - lappend callbacks [list done $cb_code] + if [llength $result] { + eval [concat $cb_code [list $result]] + } else { + lappend callbacks [list done $cb_code] + } } method on_fail {cb_code} { - lappend callbacks [list fail $cb_code] + if [llength $failure] { + eval [concat $cb_code [list $failure]] + } else { + lappend callbacks [list fail $cb_code] + } } method MarkReady {} {