-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from mWater/issue19
fixing Issue19
- Loading branch information
Showing
6 changed files
with
102 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
async = require 'async' | ||
|
||
PriorityDataSource = require './PriorityDataSource' | ||
|
||
# Creates PriorityDataSource from DataSource | ||
module.exports = class PriorityDataQueue | ||
|
||
constructor: (dataSource, concurrency) -> | ||
@dataSource = dataSource | ||
# Creates a priorityQueue that calls performQuery | ||
worker = (query, callback) -> | ||
dataSource.performQuery(query, callback) | ||
@performQueryPriorityQueue = new async.priorityQueue(worker, concurrency) | ||
|
||
# Creates a PriorityDataSource that will then be used like a DataSource but with a priority | ||
createPriorityDataSource : (priority) -> | ||
return new PriorityDataSource(this, priority) | ||
|
||
# Designed to be called by PriorityDataSource | ||
performQuery: (query, cb, priority) -> | ||
# Push to the priorityQueue | ||
@performQueryPriorityQueue.push query, priority, cb | ||
|
||
# Simply call the dataSource since this is not an async function | ||
getImageUrl: (imageId, height) -> | ||
@dataSource.getImageUrl(imageId, height) | ||
|
||
kill: () -> | ||
if @performQueryPriorityQueue? | ||
@performQueryPriorityQueue.kill() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
# Behaves like a DataSource | ||
# Created by a PriorityDataQueue | ||
# Forwards performQuery call to the PriorityDataQueue that will forward them to the DataSource | ||
module.exports = class PriorityDataSource | ||
|
||
constructor: (priorityDataQueue, priority) -> | ||
@priorityDataQueue = priorityDataQueue | ||
@priority = priority | ||
|
||
performQuery: (query, cb) -> | ||
@priorityDataQueue.performQuery(query, cb, @priority) | ||
|
||
getImageUrl: (imageId, height) -> | ||
@priorityDataQueue.getImageUrl(imageId, height) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
assert = require('chai').assert | ||
|
||
PriorityDataQueue = require '../src/PriorityDataQueue' | ||
DataSource = require '../src/DataSource' | ||
|
||
# Very simple DataSource implementation used for testing | ||
class TestDataSource extends DataSource | ||
performQuery: (query, cb) -> | ||
# Simply does an async callback passing back the query | ||
call = () -> | ||
cb(query) | ||
setTimeout(call, 1) | ||
|
||
describe "PriorityDataQueue", -> | ||
beforeEach -> | ||
testDataSource = new TestDataSource() | ||
@priorityDataQueue = new PriorityDataQueue(testDataSource, 1) | ||
|
||
it "calling performQuery reaches the DataSource", (testCallback) -> | ||
priorityDataSource = @priorityDataQueue.createPriorityDataSource(1) | ||
priorityDataSource.performQuery('test', (data) -> | ||
# Make sure that TestDataSource called back with the right data | ||
assert.equal('test', data) | ||
testCallback() | ||
) | ||
|
||
it "priorityDataQueue properly prioritize the calls", (testCallback) -> | ||
counter = 0 | ||
callDone = (priority) -> | ||
counter++ | ||
# Make sure that the priority is called in the right order | ||
assert.equal priority, counter | ||
if counter == 4 | ||
testCallback() | ||
|
||
priorityDataSource1 = @priorityDataQueue.createPriorityDataSource(1) | ||
priorityDataSource2 = @priorityDataQueue.createPriorityDataSource(2) | ||
priorityDataSource3 = @priorityDataQueue.createPriorityDataSource(3) | ||
priorityDataSource4 = @priorityDataQueue.createPriorityDataSource(4) | ||
# First one needs to be done first since the query is empty and the first call will have best priority | ||
priorityDataSource1.performQuery(1, (data) -> | ||
callDone(1) | ||
) | ||
# Then call with 3 | ||
priorityDataSource3.performQuery(3, (data) -> | ||
callDone(3) | ||
) | ||
# Then call with 4 | ||
priorityDataSource4.performQuery(4, (data) -> | ||
callDone(4) | ||
) | ||
# Then with 2 | ||
priorityDataSource2.performQuery(2, (data) -> | ||
callDone(2) | ||
) |