Skip to content

Commit b1bf653

Browse files
committed
small suggests and new test case for issue queicherius#74
1 parent 05fa149 commit b1bf653

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/endpoint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ module.exports = class AbstractEndpoint {
2929

3030
this.autoBatching = parent.autoBatching
3131
this.autoBatchDelay = parent.autoBatchDelay
32-
this._autoBatch = null
3332
this.setupAutoBatchSharedData()
3433

3534
this._skipCache = false
@@ -97,6 +96,7 @@ module.exports = class AbstractEndpoint {
9796
// Sets _autoBatch to shared batching object based on _cacheHash
9897
setupAutoBatchSharedData() {
9998
const autoBatchId = this._cacheHash(this.autoBatchDelay)
99+
console.log('@@',autoBatchId, this.url)
100100
if (!autoBatchSharedData[autoBatchId]) {
101101
autoBatchSharedData[autoBatchId] = {
102102
idsForNextBatch: new Set(),

tests/endpoint.spec.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ describe('abstract endpoint', () => {
111111
describe('auto batching', () => {
112112
const batchDelay = 10
113113

114+
beforeEach(() => {
115+
mockClient.autoBatching = true
116+
})
117+
118+
afterEach(() => {
119+
mockClient.autoBatching = false
120+
})
121+
114122
it('sets up _autoBatch variable', () => {
115123
let x = endpoint.autoBatch(batchDelay)
116124
expect(x).toBeInstanceOf(Module)
@@ -137,10 +145,11 @@ describe('abstract endpoint', () => {
137145
endpoint.autoBatch(batchDelay)
138146
fetchMock.addResponse(content)
139147

140-
let [entry1, entry2] = await Promise.all([endpoint.get(1), endpoint.get(2)])
148+
let [entry1, entry2, entry3] = await Promise.all([endpoint.get(1), endpoint.get(2), endpoint.get(1)])
141149
expect(fetchMock.lastUrl()).toEqual('https://api.guildwars2.com/v2/test?v=schema&ids=1,2')
142150
expect(entry1).toEqual(content[0])
143151
expect(entry2).toEqual(content[1])
152+
expect(entry3).toEqual(content[0])
144153
})
145154

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

192201
})
202+
203+
it('can batch requests from different endpoints in parallel', async () => {
204+
console.log('@@@@@@@@@@@@@@@@@@@@@@@')
205+
let content1 = [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }]
206+
let content2 = [{ id: 1, name: 'bar' }]
207+
208+
endpoint = new Module(mockClient)
209+
endpoint.caches.map(cache => cache.flush())
210+
endpoint.isBulk = true
211+
endpoint.url = '/v2/test'
212+
endpoint.schemaVersion = 'schema'
213+
// endpoint.autoBatch(batchDelay*100)
214+
215+
differentEndpoint = new Module(mockClient)
216+
differentEndpoint.caches.map(cache => cache.flush())
217+
differentEndpoint.isBulk = true
218+
differentEndpoint.url = '/v2/differentTest'
219+
differentEndpoint.schemaVersion = 'schema'
220+
// differentEndpoint.autoBatch(batchDelay*100)
221+
222+
fetchMock.addResponse(content1)
223+
fetchMock.addResponse(content2)
224+
225+
let [entry1, entry2, entry3] = await Promise.all([
226+
endpoint.get(1),
227+
differentEndpoint.get(1),
228+
endpoint.get(2),
229+
])
230+
console.log('@@@@@@@@@@@@@@@@@@@@@@@')
231+
expect(fetchMock.urls()).toEqual([
232+
'https://api.guildwars2.com/v2/test?v=schema&ids=1,2',
233+
'https://api.guildwars2.com/v2/differentTest?v=schema&ids=1'
234+
])
235+
expect(entry1).toEqual(content1[0])
236+
expect(entry2).toEqual(content2[0])
237+
expect(entry3).toEqual(content1[1])
238+
239+
console.log('@@@@@@@@@@@@@@@@@@@@@@@')
240+
})
193241
})
194242

195243
describe('get', () => {

0 commit comments

Comments
 (0)