Permalink
Browse files

Update timer and tcp examples

  • Loading branch information...
1 parent e2c953e commit 37cf12ffe8266c3d48e18873de06462cd21f9a27 @creationix creationix committed Mar 22, 2012
Showing with 26 additions and 14 deletions.
  1. +7 −7 test-tcp.can
  2. +19 −7 test-timer.can
View
@@ -22,26 +22,26 @@ server = Tcp.create()
check(server:bind("0.0.0.0", 8080))
onConnection = (client) {
- p("onConnection")
+ p("new client", client)
check(client:readStart((nread, chunk) {
if (nread == -1) {
err = require('uv').lastError()
if (err.name == "EOF") {
- p("EOF")
- client:shutdown(() {
- p("shutdown")
+ p("client end", client)
+ client:shutdown((status) {
+ p("client shutdown", client)
+ check(status)
client:close(() {
- p("closed")
- })
+ p("client left", client)
+ })
})
} else {
throw(err)
}
return
}
if (nread > 0) {
- p("Echoing", chunk)
client:write(chunk)
return
}
View
@@ -1,34 +1,46 @@
print = global.print
p = global.prettyPrint
require = global.require
+throw = (err) {
+ p(err)
+ global.exit()
+}
+
+// Used to emit on errors when libuv badness happens
+check = (status) {
+ if (status) {
+ throw(require('uv').lastError())
+ }
+}
Timer = require('timer')
p("Timer", Timer)
+
timer = Timer.create()
timer2 = Timer.create()
p("timer", timer)
p("timer2", timer2)
-timer:start(2400, 0, () {
+check(timer:start(1000, 0, () {
p("onTimeout", timer)
- timer2:stop()
- timer:stop()
+ check(timer2:stop())
+ check(timer:stop())
timer:close(() {
p("close", timer)
})
timer2:close(() {
p("close2", timer2)
})
-})
+}))
-timer2:start(333, 333, () {
+check(timer2:start(0, 150, () {
p("onInterval", timer2)
period = timer2:getRepeat()
p("period", period)
- timer2:setRepeat(period / 1.2 + 1)
-})
+ timer2:setRepeat(period / 1.2 + 2)
+}))
p("timer", timer)
p("timer2", timer2)

0 comments on commit 37cf12f

Please sign in to comment.