1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
/*jshint node: true, indent:2, loopfunc: true, asi: true, undef:true, mocha: true */

// require('longjohn')

var sync   = require('../sync')
var Fiber = require('fibers')
var expect = require('chai').expect

describe('Control Flow', function(){
  var waitAndReturn = function(time, err, value, cb){
    setTimeout(function(){cb(err, value)}, time)
  }

  var fn = function(arg, cb){
    expect(arg).to.eql('something')
    process.nextTick(function(){
      cb(null, 'ok')
    })
  }

  it('should provide await & defer', function(done){
    sync.fiber(function(){
      var result = sync.await(fn('something', sync.defer()))
      expect(result).to.eql('ok')
    }, done)
  })

  it('should synchronize function', function(done){
    fn = sync(fn)
    sync.fiber(function(){
      expect(fn('something')).to.eql('ok')
    }, done)
  })

  it('should be save aginst synchronizing function twice', function(done){
    fn = sync(sync(fn))
    sync.fiber(function(){
      expect(fn('something')).to.eql('ok')
    }, done)
  })

  it('should allow call synchronized function explicitly', function(done){
    fn = sync(fn)
    fn('something', function(err, result){
      expect(result).to.eql('ok')
      done(err)
    })
  })

  it("should catch asynchronous errors", function(done){
    var fn = function(cb){
      process.nextTick(function(){
        cb(new Error('an error'))
      })
    }
    fn = sync(fn)
    sync.fiber(function(){
      var err
      try {
        fn()
      } catch (e) {
        err = e
      }
      expect(err.message).to.eql('an error')
    }, done)
  })

  it("should be compatible with not asynchronous cbs", function(done){
    fn = function(cb){
      cb(null, 'ok')
    }
    fn = sync(fn)
    sync.fiber(function(){
      expect(fn()).to.eql('ok')
    }, done)
  })

  it("should catch non asynchronous errors", function(done){
    fn = function(cb){
      cb(new Error('an error'))
    }
    fn = sync(fn)
    sync.fiber(function(){
      var err
      try {
        fn()
      } catch (e) {
        err = e
      }
      expect(err.message).to.eql('an error')
    }, done)
  })

  describe("Special cases", function(){
    it('should be able to emulate sleep', function(done){
      var sleep = function(ms){
        sync.await(setTimeout(sync.defer(), ms))
      }

      sync.fiber(function(){
        var start = new Date().getTime()
        sleep(50)
        expect(new Date().getTime()).to.be.greaterThan(start)
      }, done)
    })
  })

  it('should support parallel calls', function(done){
    sync.fiber(function(){
      var calls = []
      var readA = function(cb){
        calls.push('readA')
        process.nextTick(function(){
          calls.push('nextTick')
          cb(null, 'dataA')
        })
      }
      var readB = function(cb){
        calls.push('readB')
        process.nextTick(function(){
          calls.push('nextTick')
          cb(null, 'dataB')
        })
      }

      sync.parallel(function(){
        readA(sync.defer())
        readB(sync.defer())
      })
      var results = sync.await()

      expect(results).to.eql(['dataA', 'dataB'])
      expect(calls).to.eql(['readA', 'readB', 'nextTick', 'nextTick'])
    }, done)
  })

  it('should support multiple arguments', function(done){
    sync.fiber(function(){
      var read = function(cb){
        process.nextTick(function(){
          cb(null, 'data1', 'data2')
        })
      }

      var result = sync.await(read(sync.defers()))
      expect(result).to.eql(['data1', 'data2'])
    }, done)
  })

  it('should support multiple `named` arguments', function(done){
    sync.fiber(function(){
      var read = function(cb){
        process.nextTick(function(){
          cb(null, 'data1', 'data2')
        })
      }

      var result = sync.await(read(sync.defers('a', 'b')))
      expect(result).to.eql({a: 'data1', b: 'data2'})
    }, done)
  })

  it('should support multiple arguments parallel calls', function(done){
    sync.fiber(function(){
      var read = function(cb){
        process.nextTick(function(){
          cb(null, 'data1', 'data2')
        })
      }

      sync.parallel(function(){
        read(sync.defers())
        read(sync.defers('a', 'b'))
      })
      var results = sync.await()
      expect(results).to.eql([['data1', 'data2'], {a: 'data1', b: 'data2'}])
    }, done)
  })

  it('should fiber call just once at raise error', function(done){
    var read = function(cb){
      process.nextTick(function(){
        cb(new Error('an error'))
      })
    }

    var called = 0
    sync.fiber(function(){
      called += 1
      sync.await(read(sync.defers()))
    }, function() {})

    setTimeout(function(){
      expect(called).to.eql(1)
      done()
    }, 100)
  })

  it('should handle parallel errors', function(done){
    sync.fiber(function(){
      var calls = []
      var readA = function(cb){
        process.nextTick(function(){
          cb(new Error("error a"))
        })
      }
      var readB = function(cb){
        process.nextTick(function(){
          cb(new Error("error b"))
        })
      }

      try {
        sync.parallel(function(){
          readA(sync.defer())
          readB(sync.defer())
        })
        var results = sync.await()
      }catch(err){
        if(err.message == 'error a') expect(err.message).to.eql('error a')
        else expect(err.message).to.eql('error b')
      }
    }, done)
  })

  it('should handle parallel errors with multiple arguments', function(done){
    sync.fiber(function(){
      var calls = []
      var readA = function(cb){
        process.nextTick(function(){
          cb(new Error("error a"))
        })
      }
      var readB = function(cb){
        process.nextTick(function(){
          cb(new Error("error b"))
        })
      }

      try {
        sync.parallel(function(){
          readA(sync.defers())
          readB(sync.defers())
        })
        var results = sync.await()
      }catch(err){
        if(err.message == 'error a') expect(err.message).to.eql('error a')
        else expect(err.message).to.eql('error b')
      }
    }, done)
  })

  it('should not unwind when await is called after an empty parallel block', function(done){
    sync.fiber(function(){
      sync.parallel(function(){
        // Imagine that the user intends to enumerate an array here, calling
        // `defer` once per array item, but the array is empty.
      })
      expect(sync.await()).to.eql([])
    }, done)
  })

  it('should return result from fiber', function(done){
    sync.fiber(function(){
      return 'some value'
    }, function(err, result){
      expect(err).to.eql(null)
      expect(result).to.eql('some value')
      done()
    })
  })

  it('should abort on timeout', function(done){
    sync.fiber(function(){
      waitAndReturn(10, null, 'some result', sync.deferWithTimeout(100))
      expect(sync.await()).to.eql('some result')

      try{
        waitAndReturn(10, null, 'some result', sync.deferWithTimeout(1))
        expect(sync.await()).to.eql('some result')
      }catch(err){
        expect(err.message).to.eql('defer timed out!')
      }
    }, done)
  })

  // TODO, add also the same specs for `defers`.
  it('should not resume terminated fiber with value', function(done){
    var runCount = 0
    var results = []
    sync.fiber(function(){
      runCount += 1
      waitAndReturn(1, null, 'some value', sync.defer())
    }, function(err){
      results.push(err || null)
    })

    // Need to wait for some time after the fiber ends its execution to make sure
    // it won't be runned one more time.
    setTimeout(function(){
      expect(runCount).to.eql(1)
      expect(results.length).to.eql(1)
      expect(results[0]).to.eql(null)
      done()
    }, 10)
  })

  // TODO, add also the same specs for `defers`.
  it('should not resume terminated fiber with error', function(done){
    var runCount = 0
    var results = []
    sync.fiber(function(){
      runCount += 1
      waitAndReturn(1, (new Error('some error')), null, sync.defer())
    }, function(err){
      results.push(err)
    })

    // Need to wait for some time after the fiber ends its execution to make sure
    // it won't be runned one more time.
    setTimeout(function(){
      expect(runCount).to.eql(1)
      expect(results.length).to.eql(1)
      expect(results[0]).to.eql(null)
      done()
    }, 10)
  })

  it('should call terminate callback just once', function(done) {
    var callCount = 0
    var callback = function(error) {
      callCount += 1
      throw new Error('some error')
    }
    expect(function() {sync.fiber(function() {}, callback)}).to.throw(Error)
    setTimeout(function() {
      expect(callCount).to.eql(1)
      done()
    }, 10)
  })

  it('should throw error if defer called twice', function(done){
    sync.fiber(function(){
      var defer = sync.defer()
      defer()
      sync.await()
      expect(defer).to.throw(Error)
    }, done)
  })

  it('should call defer only once in the fiber process', function(done){
    var broken = function(cb) {
      sync.fiber(function() {
        cb()
      }, cb)
    }
    sync.fiber(function(){
      broken(sync.defer())
      sync.await()
      throw new Error('an error')
    }, function(err) {
      expect(err).to.exist
      expect(err.message).to.eql("defer can't be used twice!")
      done()
    })
  })

  it('should throw error if defers called twice', function(done){
    sync.fiber(function(){
      var defer = sync.defers()
      defer()
      sync.await()
      expect(defer).to.throw(Error)
    }, done)
  })

  it('should prevent defers call just once in fiber process', function(done){
    var broken = function(cb) {
      sync.fiber(function() {
        cb()
      }, cb)
    }
    sync.fiber(function(){
      broken(sync.defers())
      sync.await()
      throw new Error('an error')
    }, function(err) {
      expect(err).to.exist
      expect(err.message).to.eql("defer can't be used twice!")
      done()
    })
  })

  it('should prevent restart fiber', function(done){
    var currentFiber
    var called = 0
    sync.fiber(function(){
      called += 1
      currentFiber = sync.Fiber.current
    })
    setTimeout(function() {
      currentFiber.run()
    }, 1)
    setTimeout(function() {
      expect(called).to.eql(1)
      done()
    }, 10);
  })

  it('should throw error when not matched defer-await pair', function(done){
    sync.fiber(function(){
	    process.nextTick(sync.defer());
      expect(function() { process.nextTick(sync.defer()) }).to.throw(Error)
      sync.await()
	    process.nextTick(sync.defers());
      expect(function() { process.nextTick(sync.defers()) }).to.throw(Error)
      sync.await()
    }, done)
  })

  beforeEach(function(){
    this.someKey = 'some value'
  })
  it('should provide asyncIt helper for tests', sync.asyncIt(function(){
    expect(Fiber.current).to.exist
    expect(this.someKey).to.eql('some value')
  }))
})