Skip to content

Commit

Permalink
small suggests and new test case for issue queicherius#74
Browse files Browse the repository at this point in the history
  • Loading branch information
ShinyMeta committed Sep 27, 2021
1 parent 05fa149 commit b1bf653
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ module.exports = class AbstractEndpoint {

this.autoBatching = parent.autoBatching
this.autoBatchDelay = parent.autoBatchDelay
this._autoBatch = null
this.setupAutoBatchSharedData()

this._skipCache = false
Expand Down Expand Up @@ -97,6 +96,7 @@ module.exports = class AbstractEndpoint {
// Sets _autoBatch to shared batching object based on _cacheHash
setupAutoBatchSharedData() {
const autoBatchId = this._cacheHash(this.autoBatchDelay)
console.log('@@',autoBatchId, this.url)
if (!autoBatchSharedData[autoBatchId]) {
autoBatchSharedData[autoBatchId] = {
idsForNextBatch: new Set(),
Expand Down
50 changes: 49 additions & 1 deletion tests/endpoint.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ describe('abstract endpoint', () => {
describe('auto batching', () => {
const batchDelay = 10

beforeEach(() => {
mockClient.autoBatching = true
})

afterEach(() => {
mockClient.autoBatching = false
})

it('sets up _autoBatch variable', () => {
let x = endpoint.autoBatch(batchDelay)
expect(x).toBeInstanceOf(Module)
Expand All @@ -137,10 +145,11 @@ describe('abstract endpoint', () => {
endpoint.autoBatch(batchDelay)
fetchMock.addResponse(content)

let [entry1, entry2] = await Promise.all([endpoint.get(1), endpoint.get(2)])
let [entry1, entry2, entry3] = await Promise.all([endpoint.get(1), endpoint.get(2), endpoint.get(1)])
expect(fetchMock.lastUrl()).toEqual('https://api.guildwars2.com/v2/test?v=schema&ids=1,2')
expect(entry1).toEqual(content[0])
expect(entry2).toEqual(content[1])
expect(entry3).toEqual(content[0])
})

it('returns null from get with no response', async () => {
Expand Down Expand Up @@ -190,6 +199,45 @@ describe('abstract endpoint', () => {
expect(entry2).toEqual(content2[0])

})

it('can batch requests from different endpoints in parallel', async () => {
console.log('@@@@@@@@@@@@@@@@@@@@@@@')
let content1 = [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }]
let content2 = [{ id: 1, name: 'bar' }]

endpoint = new Module(mockClient)
endpoint.caches.map(cache => cache.flush())
endpoint.isBulk = true
endpoint.url = '/v2/test'
endpoint.schemaVersion = 'schema'
// endpoint.autoBatch(batchDelay*100)

differentEndpoint = new Module(mockClient)
differentEndpoint.caches.map(cache => cache.flush())
differentEndpoint.isBulk = true
differentEndpoint.url = '/v2/differentTest'
differentEndpoint.schemaVersion = 'schema'
// differentEndpoint.autoBatch(batchDelay*100)

fetchMock.addResponse(content1)
fetchMock.addResponse(content2)

let [entry1, entry2, entry3] = await Promise.all([
endpoint.get(1),
differentEndpoint.get(1),
endpoint.get(2),
])
console.log('@@@@@@@@@@@@@@@@@@@@@@@')
expect(fetchMock.urls()).toEqual([
'https://api.guildwars2.com/v2/test?v=schema&ids=1,2',
'https://api.guildwars2.com/v2/differentTest?v=schema&ids=1'
])
expect(entry1).toEqual(content1[0])
expect(entry2).toEqual(content2[0])
expect(entry3).toEqual(content1[1])

console.log('@@@@@@@@@@@@@@@@@@@@@@@')
})
})

describe('get', () => {
Expand Down

0 comments on commit b1bf653

Please sign in to comment.