Back in 2011 I blogged "Adding latency during service development for backend simulation":

I used <dp:url-open> against an unreachable target with timeout specifying the delay in seconds.

In 2012 I blogged "Adding sub-second latency with firmware >=":

I misused "xc10:" target with <dp:url-open>" to get millisecond precision delays.


There is no built in delay for good reason -- be careful with what you do!


On the weekend I learned how to do it correctly in nodejs in Jeff's blog:

First I wanted to get it working with an older (v6) nodejs version and found how to do that using asyncawait module:

It turned out that nothing special is needed for DataPower GatewayScript, Jeff's posting directly works!

I modified it a bit for DataPower, and added code that verifies that indeed the synchronous wait works by returning the measured delay:

$ cat sync_wait.js 
async function wait(ms) {
  return new Promise(resolve => {
    setTimeout(resolve, ms);

session.input.readAsJSON(async function(error,json){
  var t0 = new Date().getTime();
  await wait(json);
  var t1 = new Date().getTime();


Here you can see it:

$ coproc2 sync_wait.js <(echo '1234') http://dp3-l3:2227; echo

$ coproc2 sync_wait.js <(echo '2000') http://dp3-l3:2227; echo



"await wait()" needs to be run inside an async function -- I just made the readAsJSON callback async.
I did choose readAsJSON() because GatewayScript implements JSON rfc7159 allowing any top level element, here a number.


Next I wanted to make use of this synchronous wait in DataPower XSLT as well via "dp:gatewayScript()".

I stored "wait.js" into "local:" folder, only changing interface from JSON to XML:

$ diff sync_wait.js wait.js 
< session.input.readAsJSON(async function(error,json){
> session.input.readAsXML(async function(error,nodelist){
<   await wait(json);
>   await wait( parseInt(nodelist.item(0).textContent) );



Now wait.xsl can just call out to "local:///wait.js" for synchronous delay at millisecond precision:

$ cat wait.xsl
<xsl:stylesheet version='1.0'
  <xsl:template match='/'>
    <xsl:copy-of select="dp:gatewayscript('local:///wait.js', ., false())"/>

    <xsl:variable name="d">2345</xsl:variable>
    <xsl:copy-of select="dp:gatewayscript('local:///wait.js', $d, false())"/>



Here you see output for passed as well as XSLT coded delay values measured:

$ coproc2 wait.xsl <(echo '<t>2000</t>') http://dp3-l3:2223; echo



Again, be careful with what you use this synchronous wait for,