| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const async = require('async'); | 
					
						
							|  |  |  | const assert = require('assert'); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const db = require('./mocks/databasemock'); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const batch = require('../src/batch'); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | describe('batch', () => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	const scores = []; | 
					
						
							|  |  |  | 	const values = []; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	before((done) => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		for (let i = 0; i < 100; i++) { | 
					
						
							|  |  |  | 			scores.push(i); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			values.push(`val${i}`); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		db.sortedSetAdd('processMe', scores, values, done); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should process sorted set with callbacks', (done) => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		let total = 0; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		batch.processSortedSet('processMe', (items, next) => { | 
					
						
							|  |  |  | 			items.forEach((item) => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 				total += item.score; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			setImmediate(next); | 
					
						
							|  |  |  | 		}, { | 
					
						
							|  |  |  | 			withScores: true, | 
					
						
							|  |  |  | 			interval: 50, | 
					
						
							|  |  |  | 			batch: 10, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		}, (err) => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.strictEqual(total, 4950); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should process sorted set with callbacks', (done) => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		let total = 0; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		batch.processSortedSet('processMe', (values, next) => { | 
					
						
							|  |  |  | 			values.forEach((val) => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 				total += val.length; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			setImmediate(next); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		}, (err) => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.strictEqual(total, 490); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should process sorted set with async/await', async () => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		let total = 0; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		await batch.processSortedSet('processMe', (values, next) => { | 
					
						
							|  |  |  | 			values.forEach((val) => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 				total += val.length; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			setImmediate(next); | 
					
						
							|  |  |  | 		}, {}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		assert.strictEqual(total, 490); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should process sorted set with async/await', async () => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		let total = 0; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		await batch.processSortedSet('processMe', async (values) => { | 
					
						
							|  |  |  | 			values.forEach((val) => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 				total += val.length; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			await db.getObject('doesnotexist'); | 
					
						
							|  |  |  | 		}, {}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		assert.strictEqual(total, 490); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-26 10:23:27 -04:00
										 |  |  | 	it('should process sorted set with min/max scores', async () => { | 
					
						
							|  |  |  | 		await db.sortedSetAddBulk([ | 
					
						
							|  |  |  | 			['processByScore', 1, 'item1'], | 
					
						
							|  |  |  | 			['processByScore', 2, 'item2'], | 
					
						
							|  |  |  | 			['processByScore', 3, 'item3'], | 
					
						
							|  |  |  | 			['processByScore', 3, 'item4'], | 
					
						
							|  |  |  | 			['processByScore', 4, 'item5'], | 
					
						
							|  |  |  | 			['processByScore', 5, 'item6'], | 
					
						
							|  |  |  | 		]); | 
					
						
							|  |  |  | 		const result = []; | 
					
						
							|  |  |  | 		await batch.processSortedSet('processByScore', async (items) => { | 
					
						
							|  |  |  | 			result.push(...items); | 
					
						
							|  |  |  | 		}, { | 
					
						
							|  |  |  | 			min: 3, | 
					
						
							|  |  |  | 			max: 4, | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		assert(result.includes('item3')); | 
					
						
							|  |  |  | 		assert(result.includes('item4')); | 
					
						
							|  |  |  | 		assert(result.includes('item5')); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should process array with callbacks', (done) => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		let total = 0; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		batch.processArray(scores, (nums, next) => { | 
					
						
							|  |  |  | 			nums.forEach((n) => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 				total += n; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			setImmediate(next); | 
					
						
							|  |  |  | 		}, { | 
					
						
							|  |  |  | 			withScores: true, | 
					
						
							|  |  |  | 			interval: 50, | 
					
						
							|  |  |  | 			batch: 10, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		}, (err) => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.strictEqual(total, 4950); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should process array with async/await', async () => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		let total = 0; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		await batch.processArray(scores, (nums, next) => { | 
					
						
							|  |  |  | 			nums.forEach((n) => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 				total += n; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			setImmediate(next); | 
					
						
							|  |  |  | 		}, { | 
					
						
							|  |  |  | 			withScores: true, | 
					
						
							|  |  |  | 			interval: 50, | 
					
						
							|  |  |  | 			batch: 10, | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		assert.strictEqual(total, 4950); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }); |