 
       server = app.listen PORT, () ->
         server2 = apiary.listen (PORT + 1), ->
-          # Comment out this timeout to enable race condition bug
-          # Event loop get stuck or idle without this
-          # https://github.com/apiaryio/dredd/issues/282
-          setTimeout () ->
-            undefined
-          , 1000
-
-          execCommand cmd, () ->
-            server2.close ->
-              server.close () ->
-                done()
+          wakeUpEventLoop ->
+            execCommand cmd, () ->
+              server2.close ->
+                server.close () ->
+                  done()
 
     it 'should not print warning about missing APIARY_API_KEY and APIARY_API_NAME', () ->
       assert.notInclude stderr, 'Apiary reporter environment variable APIARY_API_KEY or APIARY_API_NAME not defined.'


        server = app.listen PORT, () ->
          server2 = apiary.listen (PORT + 1), ->
        # Race condition workaround
        # Spawned process doesn't write to stdout before is terminated
        setTimeout () ->
          setTimeout () ->
            undefined
          , 500

          execCommand cmd, () ->
            # Comment out this timeout to enable race condition bug
            # Event loop get stuck or idle without this
            # https://github.com/apiaryio/dredd/issues/282
            setTimeout () ->
              undefined
            , 1000

            server2.close ->
              server.close ->

        server.on 'close', done


      cb null, stdout, stderr, exitStatus
  return


# Event loop gets stuck or idle without this, see
# https://github.com/apiaryio/dredd/issues/282
wakeUpEventLoop = (callback) ->
  setTimeout ->
    undefined
  , 1000
  callback()


describe "Dredd class Integration", () ->
  dreddCommand = null
  custom = {}
