aboutsummaryrefslogtreecommitdiffhomepage
path: root/testdata
diff options
context:
space:
mode:
authorKenneth Bell <[email protected]>2022-07-02 17:19:36 +0100
committerRon Evans <[email protected]>2022-08-23 12:37:25 +0200
commit24b45555bd9c82a5486f3063b32c262bfa420253 (patch)
tree8ce811a7b94dcb3d7048b40d6cdc831d63f2356c /testdata
parent80c17c0f320ca54a812d68f4077b0dda49229a62 (diff)
downloadtinygo-24b45555bd9c82a5486f3063b32c262bfa420253.tar.gz
tinygo-24b45555bd9c82a5486f3063b32c262bfa420253.zip
runtime: add support for time.NewTimer and time.NewTicker
This commit adds support for time.NewTimer and time.NewTicker. It also adds support for the Stop() method on time.Timer, but doesn't (yet) add support for the Reset() method. The implementation has been carefully written so that programs that don't use these timers will normally not see an increase in RAM or binary size. None of the examples in the drivers repo change as a result of this commit. This comes at the cost of slightly more complex code and possibly slower execution of the timers when they are used.
Diffstat (limited to 'testdata')
-rw-r--r--testdata/timers.go41
-rw-r--r--testdata/timers.txt11
2 files changed, 52 insertions, 0 deletions
diff --git a/testdata/timers.go b/testdata/timers.go
new file mode 100644
index 000000000..99591c822
--- /dev/null
+++ b/testdata/timers.go
@@ -0,0 +1,41 @@
+package main
+
+import "time"
+
+func main() {
+ // Test ticker.
+ ticker := time.NewTicker(time.Millisecond * 160)
+ println("waiting on ticker")
+ go func() {
+ time.Sleep(time.Millisecond * 80)
+ println(" - after 80ms")
+ time.Sleep(time.Millisecond * 160)
+ println(" - after 240ms")
+ time.Sleep(time.Millisecond * 160)
+ println(" - after 400ms")
+ }()
+ <-ticker.C
+ println("waited on ticker at 160ms")
+ <-ticker.C
+ println("waited on ticker at 320ms")
+ ticker.Stop()
+ time.Sleep(time.Millisecond * 400)
+ select {
+ case <-ticker.C:
+ println("fail: ticker should have stopped!")
+ default:
+ println("ticker was stopped (didn't send anything after 400ms)")
+ }
+
+ timer := time.NewTimer(time.Millisecond * 160)
+ println("waiting on timer")
+ go func() {
+ time.Sleep(time.Millisecond * 80)
+ println(" - after 80ms")
+ time.Sleep(time.Millisecond * 160)
+ println(" - after 240ms")
+ }()
+ <-timer.C
+ println("waited on timer at 160ms")
+ time.Sleep(time.Millisecond * 160)
+}
diff --git a/testdata/timers.txt b/testdata/timers.txt
new file mode 100644
index 000000000..24142d541
--- /dev/null
+++ b/testdata/timers.txt
@@ -0,0 +1,11 @@
+waiting on ticker
+ - after 80ms
+waited on ticker at 160ms
+ - after 240ms
+waited on ticker at 320ms
+ - after 400ms
+ticker was stopped (didn't send anything after 400ms)
+waiting on timer
+ - after 80ms
+waited on timer at 160ms
+ - after 240ms