Permalink
Browse files

Intermediate commit. All processes seem to be talking to one-another,…

… but there's some trouble buffering JSON streams. Need smarter, line-oriented buffering.
  • Loading branch information...
jbeard4 committed Jul 4, 2011
1 parent f16ed9f commit bff4dcda6ac87e1acbd1a7fe0dcead09c802e9db
@@ -1,5 +1,6 @@
#this is a regular old node.js process
child_process = require('child_process')
+Promise = require("promise").Promise
process.stdin.resume()
process.stdin.setEncoding 'utf8'
@@ -17,9 +18,9 @@ log = -> console.log JSON.stringify {method:"log",args:Array.prototype.slice.cal
debug = -> console.log JSON.stringify {method:"debug",args:Array.prototype.slice.call(arguments)}
+promise = new Promise()
process.stdin.once 'data',(chunk) ->
-
- #debug "received chunk from user thread",chunk
+ #receive first chunk, which is test
o = JSON.parse chunk
#debug "parsed as object",o
@@ -28,40 +29,47 @@ process.stdin.once 'data',(chunk) ->
statechartProcess = child_process.spawn "bash",['bin/run-tests-spartan-shell.sh',test.interpreter,'scxml/test/multi-process/scxml.js']
statechartProcess.stdout.setEncoding 'utf8'
+ statechartProcess.stdin.setEncoding 'utf8'
+
+ #listen for statechart process ready signal
+ statechartProcess.stdout.once 'data',(str) -> promise.resolve(test,str)
+
+promise.then (test,str) ->
+
+ #listen to external events
+ statechartProcess.stdout.on 'data',(str) ->
+ log "received data from statechart process:",str
+
+ o = JSON.parse str
+ switch o.method
+ when "getConfiguration"
+ configuration = o.configuration
+ process.stdout.write(JSON.stringify({method:"configuration",configuration:configuration})+'\n')
+ when "setTimeout"
+ setTimeout( -> sendEvent o.e, o.delay )
+ when "clearTimeout"
+ clearTimeout o.sendid
+ when "log"
+ o.args = ["from statechart process:"].concat o.args
+ process.stdout.write(JSON.stringify(o)+'\n') #just pipe him to the parent who will print him
+ when "debug"
+ o.args = ["from statechart process:"].concat o.args
+ process.stdout.write(JSON.stringify(o)+'\n')
+
+
+ statechartProcess.stdin.write(JSON.stringify({ method : "init", test : test })+'\n')
+
+ #this needs to be moved up, as he's missing event somehow...
+ process.stdin.on 'data',(chunk) ->
+ o = JSON.parse(chunk)
+ log "received event",chunk
+ sendEvent o
+ getConfiguration()
- statechartProcess.stdout.once 'data',(str) ->
- log "statechart ready: ",str
-
- #listen to external events
- statechartProcess.stdout.on 'data',(str) ->
- log "received data from statechart process:",str
-
- o = JSON.parse str
- switch o.method
- when "getConfiguration"
- configuration = o.configuration
- process.stdout.write(JSON.stringify({method:"configuration",configuration:configuration})+'\n')
- when "setTimeout"
- setTimeout( -> sendEvent o.e, o.delay )
- when "clearTimeout"
- clearTimeout o.sendid
- when "log"
- o.args = ["from statechart process:"].concat o.args
- process.stdout.write(JSON.stringify(o)+'\n') #just pipe him to the parent who will print him
- when "debug"
- o.args = ["from statechart process:"].concat o.args
- process.stdout.write(JSON.stringify(o)+'\n')
-
- statechartProcess.stdin.write(JSON.stringify({ method : "init", test : test })+'\n')
-
- #this needs to be moved up, as he's missing event somehow...
- process.stdin.on 'data',(chunk) ->
- log "received event",o
- sendEvent o
- getConfiguration()
ready()
+
###
process.stdin.on 'end',->
statechartProcess.end()
@@ -1,11 +1,15 @@
-define ["scxml/test/report2string","scxml/async-for","scxml/test/multi-process/json-tests","util/set/ArraySet","child_process"],(report2string,asyncFor,jsonTests,Set,child_process) ->
+define ["scxml/test/report2string","scxml/async-for","scxml/test/multi-process/json-tests","util/set/ArraySet","child_process","promise"],(report2string,asyncFor,jsonTests,Set,child_process,promiseModule) ->
- isDebug = false
+ Promise = promiseModule.Promise
+
+ isDebug = true
debug = () -> if isDebug then console.debug.apply(this,arguments)
trim = (s) -> s.replace(/^\s+|\s+$/g, '')
+ nameGroup = (n,g) -> "#{n} (#{g})"
+
printError = (e) ->
if e.stack
#v8
@@ -17,6 +21,46 @@ define ["scxml/test/report2string","scxml/async-for","scxml/test/multi-process/j
console.error e.fileName
console.error e.lineNumber
+ receiveMethod =
+ (test,runNextTest,failback,errback,expectedConfigurations,p) ->
+ (chunk) ->
+ debug "received data from browser-env process:",chunk
+
+ strs = trim(chunk).split("\n")
+ for str in strs
+ debug "processing",str
+ o = JSON.parse str
+
+ switch o.method
+ when "configuration"
+ configuration = new Set o.configuration
+
+ #compare to expected configuration
+ expectedConfiguration =
+ if checkInitialState
+ checkInitialState = false
+ new Set test.testScript.initialConfiguration
+ else
+ expectedConfigurations.shift()
+
+ if not (expectedConfiguration.equals configuration)
+ failback test
+ p.stdin.end()
+ runNextTest()
+
+ #when we receive a configuration, and expectedConfigurations is empty, then we're done with this test
+ if not expectedConfigurations.length
+ p.stdin.end()
+ runNextTest()
+ when "log"
+ console.log.apply this,["from browser-env:"].concat(o.args)
+ when "debug"
+ debug.apply this,["from browser-env:"].concat(o.args)
+ else
+ console.log "Unrcognized message method"
+ console.log o
+
+
->
results =
testCount : 0
@@ -31,63 +75,33 @@ define ["scxml/test/report2string","scxml/async-for","scxml/test/multi-process/j
checkInitialState = true
- #first ready event, send in test
+ promise = new Promise()
p.stdout.once "data",(str) ->
+ #first ready event, means browser-env process is listening to stdin. send in test
debug "received ready string from browser-env process:",str
-
- #second ready event
- p.stdout.once "data",(str) ->
- debug "received ready string from browser-env process:",str
-
- #read new configuration when we receive it
- p.stdout.on "data",(chunk) ->
- debug "received data from browser-env process:",chunk
-
- strs = trim(chunk).split("\n")
- for str in strs
- debug "processing",str
- o = JSON.parse str
-
- switch o.method
- when "configuration"
- configuration = new Set o.configuration
-
- #compare to expected configuration
- expectedConfiguration =
- if checkInitialState
- checkInitialState = false
- new Set test.testScript.initialConfiguration
- else
- expectedConfigurations.shift()
-
- if not (expectedConfiguration.equals configuration)
- failback test
- p.stdin.end()
- runNextTest()
-
- #when we receive a configuration, and expectedConfigurations is empty, then we're done with this test
- if not expectedConfigurations.length
- p.stdin.end()
- runNextTest()
- when "log"
- console.log.apply this,["from browser-env:"].concat(o.args)
- when "debug"
- debug.apply this,["from browser-env:"].concat(o.args)
- else
- console.log "Unrcognized message method"
- console.log o
-
- expectedConfigurations = (new Set event.nextConfiguration for event in test.testScript.events)
- #send events in at fixed rate
- for eventTuple in test.testScript.events
- debug "sending event to browser process:",eventTuple.event
- #TODO: also add support for timeouts/delays
- p.stdin.write JSON.stringify eventTuple.event
+ p.stdout.once "data",(str) -> promise.resolve(str)
p.stdin.write JSON.stringify test
+ promise.then (str) ->
+ debug "received second ready string from browser-env process:",str
+
+ expectedConfigurations = (new Set event.nextConfiguration for event in test.testScript.events)
+
+ #read new configuration when we receive it
+ p.stdout.on "data",receiveMethod(test,runNextTest,failback,errback,expectedConfigurations,p)
+
+ #send in events at whatever rate you want
+ for eventTuple in test.testScript.events
+ debug "sending event to browser process:",eventTuple.event
+ #TODO: also add support for timeouts/delays
+ p.stdin.write JSON.stringify eventTuple.event
+
+
+
+
failBack = (test) ->
console.info "test",nameGroup(test.name,test.group),"...failed"
@@ -97,14 +111,14 @@ define ["scxml/test/report2string","scxml/async-for","scxml/test/multi-process/j
errBack = (test) ->
console.info "test",nameGroup(test.name,test.group),"...errored"
results.testsErrored.push nameGroup(test.name,test.group)
- printError err
+ #printError err
finish = ->
report2string results
process.exit results.testCount == results.testsPassed.length
- interpreters = ["v8-js","spidermonkey-js","webcore-js"] #,"rhino"
+ interpreters = ["v8-js"]#,"spidermonkey-js","webcore-js"] #,"rhino"
tests = []
for interpreter in interpreters
@@ -74,7 +74,7 @@ require ["scxml/SCXML","scxml/json2model","scxml/json2extra-model"],(scxml,json2
StateSet : setPurposes.states.initializedSetClass
BasicStateSet : setPurposes.basicStates.initializedSetClass
- return if testJson.extraModelInfo then [m,model,optimizations] else [extraModel,json2ExtraModel(model),optimizations]
+ return if testJson.extraModelInfo then [m,model,optimizations] else [m,json2ExtraModel(model),optimizations]
printConfiguration = (configuration) -> print JSON.stringify {method : "getConfiguration", configuration : configuration}

0 comments on commit bff4dcd

Please sign in to comment.