1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-05 10:39:25 +02:00

Add a simple STF console for testing without the app.

This commit is contained in:
Simo Kinnunen 2014-01-14 18:41:48 +09:00
parent 3be72ae8c9
commit 0bf07ab776
5 changed files with 222 additions and 6 deletions

77
lib/util/tx.js Normal file
View file

@ -0,0 +1,77 @@
var uuid = require('node-uuid')
var Promise = require('bluebird')
function newId() {
return uuid.v4()
}
module.exports.newId = newId
function q(output, input, channel, args) {
var deferred = Promise.defer()
, ourId = newId()
, results = []
, mapping = {}
, remaining = 0 // @todo pass expected number to query
function onMessage(theirId, serial, state, data) {
if (ourId === theirId.toString()) {
serial = serial.toString()
state = state.toString()
var mapped = mapping[serial]
if (!mapped) {
results.push(mapped = mapping[serial] = {
serial: serial
, state: state
, progress: 0
, value: null
})
}
else {
mapped.state = state
}
switch (state) {
case 'ACK':
deferred.progress(results)
++remaining
break
case 'PRG':
mapped.progress = +data
deferred.progress(results)
break
case 'ERR':
mapped.value = data
--remaining
break
case 'OKY':
mapped.progress = 100
mapped.value = data
--remaining
break
}
if (remaining) {
deferred.progress(results)
}
else {
deferred.resolve(results)
}
}
}
input.on('message', onMessage)
input.subscribe(ourId)
output.send([channel, ourId].concat(args))
return deferred.promise.finally(function() {
input.unsubscribe(ourId)
input.removeListener('message', onMessage)
mapping = results = null
})
}
module.exports.q = q