我的测试需要通过make系统在本地加载API。为此,我正在使用child_process.exec
。不幸的是,这个回调只在进程终止后执行,我需要API直到测试完成。我无法想办法让它发挥作用。
request = require "request"
child_process = require "child_process"
tsc = require "training_service_connector"
campaign = "happy"
strategy = campaign
port_number = 54340
service_conf_filename = tsc.writeServiceConfig(strategy, port_number)
exec_callback = (error, stdout, stderr) -> console.log ('stdout:\n' + stdout + '\nstderr:\n' + stderr + "\nerror:\n" + error)
child_process.exec("CONFIG=#{service_conf_filename} make run_bidder_service_runner", exec_callback)
secret = {}
setToken = (error, response, body) -> secret.token = body.token
details = {"email" : "email@email.com", "password" : "password", "sendImmediately" : false}
request.post {uri: "http://127.0.0.1:#{port_number}/authenticate", json: details}, setToken
在REPL中逐行运行,但是一次运行它会产生错误。
opts = {uri: "http://127.0.0.1:#{port_number}/authenticate", json: details}
realtries = 0
val = 0
postAndCount = () -> console.log "#{realtries++}: {#{secret}}"; request.post(opts, setToken); clearInterval(val) if "token" of secret
val = setInterval(postAndCount, 2000)
console.log secret
这是有效的(尽管是一种非常迂回的做事方式),并且还有一个问题,即该代码所用的函数将在API实际准备好之前最终返回。有什么办法迫使JS等待某种触发器吗?是否有任何带有sleep()或wait()的库或者在返回某些内容之前等待函数等待的其他方法?
重新设计此功能所用的整个测试系统,并且回调触发每次测试都是不可行的。