From ec5cd2a59a56348e9d2cea2c70071a005f2291ca Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Thu, 26 Jan 2017 09:29:00 -0800 Subject: [PATCH] packager: add BatchProcessor-test Reviewed By: davidaurelio Differential Revision: D4468735 fbshipit-source-id: 8d20b44ef20aa24e72cd53182dbc34139a10df41 --- .../src/lib/__tests__/BatchProcessor-test.js | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 packages/metro-bundler/react-packager/src/lib/__tests__/BatchProcessor-test.js diff --git a/packages/metro-bundler/react-packager/src/lib/__tests__/BatchProcessor-test.js b/packages/metro-bundler/react-packager/src/lib/__tests__/BatchProcessor-test.js new file mode 100644 index 00000000..dabdfc0e --- /dev/null +++ b/packages/metro-bundler/react-packager/src/lib/__tests__/BatchProcessor-test.js @@ -0,0 +1,68 @@ +/** + * Copyright (c) 2016-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +'use strict'; + +jest.dontMock('../BatchProcessor'); + +const BatchProcessor = require('../BatchProcessor'); + +describe('BatchProcessor', () => { + + const options = { + maximumDelayMs: 500, + maximumItems: 3, + concurrency: 2, + }; + + it('aggregate items concurrently', () => { + const input = [...Array(9).keys()].slice(1); + const transform = e => e * 10; + const batches = []; + let concurrency = 0; + let maxConcurrency = 0; + const bp = new BatchProcessor(options, (items, callback) => { + ++concurrency; + expect(concurrency).toBeLessThanOrEqual(options.concurrency); + maxConcurrency = Math.max(maxConcurrency, concurrency); + batches.push(items); + setTimeout(() => { + callback(null, items.map(transform)); + --concurrency; + }, 0); + }); + const results = []; + const callback = (error, res) => { + expect(error).toBe(null); + results.push(res); + }; + input.forEach(e => bp.queue(e, callback)); + jest.runAllTimers(); + expect(batches).toEqual([ + [1, 2, 3], + [4, 5, 6], + [7, 8], + ]); + expect(maxConcurrency).toEqual(options.concurrency); + expect(results).toEqual(input.map(transform)); + }); + + it('report errors', () => { + const error = new Error('oh noes'); + const bp = new BatchProcessor(options, (items, callback) => { + process.nextTick(callback.bind(null, error)); + }); + let receivedError; + bp.queue('foo', err => { receivedError = err; }); + jest.runAllTimers(); + jest.runAllTicks(); + expect(receivedError).toBe(error); + }); + +});