UNPKG

mof-bottleneck

Version:

A middleware wrapped Bottleneckp module for floodesh

57 lines (43 loc) 1.49 kB
'use strict' const should = require('should') const sinon = require('sinon') const co = require('co') const Mof = require('mof') const bottleneckp = require('../') require('should-sinon') describe('test bottleneckp in middleware', () => { let ctx, app,options , log = console.log , application={logger:{debug:log,info:log,verbose:log,warn:log,error:log}}; beforeEach(()=>{ app = new Mof(); ctx = {opt:{}, app:application, resourceList:{},func:"price"}; options = {defaultCfg:{rate: 10, concurrent:1, priorityRange:10,defaultPrioty:5, homogenous:true}, price:{rate: 10, concurrent:1, priorityRange:10,defaultPrioty:5, homogenous:true}}; }); it('should exist bottleneckp', done=>{ app.use(co.wrap(bottleneckp(options))); app.use((ctx, next) => { should.exist(ctx.app.scheduler); ctx.resourceList.should.have.property("bottleneck"); return next(); }); const handle = app.callback( () => { done(); }); handle(ctx); }); it('should handle more than one asynchronous task', done => { const ctx1={id:1,opt:{limiter:"abc"}, app:application, resourceList:{},func:"price"}, ctx2={id:2,opt:{limiter:"bbc"}, app:application, resourceList:{},func:"list"}; app.use(co.wrap(bottleneckp(options))); const handle = app.callback( (ctx) => { /*done()*/ ctx.id.should.eql(1); }); handle(ctx1); const handle2 = app.callback( (ctx) => { ctx.id.should.eql(2); done(); }); handle2(ctx2); }) });