0

I am using a mongo in memory test fixture loader that gets primed before each test. Normally works flawlessly.

I have a function getSample that makes a db call that I test using AVA. Wanting to call this multiple time with different parameters (timestamps) I tried this:

const timestamps = [ '2017-08-14T00:00:00.000Z', '2017-08-13T00:00:00.000Z']

const tasks = timestamps.map(t => getSample(t))
const samples = await Promise.all(tasks)

This failed in an interesting way. My first call works (db results are there) and all others return an empty set - no errors).

Changing code to this format works. All loop instances find the collection and content.

let samples = []
for (let t of timestamps) {
  samples.push(await getSample(t))
}

const getSample = async () => {
   const c = await getCollection('foo') // fetches open mongo connection and returns collection
   return c.find().toArray()
}

With a standard Mongo DB things work fine. But evidently there is a difference in how these 2 pieces of code work and I'd like to understand what that is. To be clear I am not looking for a fix for my in memory db - more wanting to understand what might be happening.

It might be related to this SO post but not certain.

cyberwombat
  • 38,105
  • 35
  • 175
  • 251
  • 1
    You need to show us the code for `getSample()`. If the first one doesn't work properly, then it's probably because of the way `getSample()` is written or how the data store handles multiple requests in flight at the same time. In either case, we can't really help much without seeing the relevant code. – jfriend00 Dec 19 '17 at 21:56
  • @jfriend00 posted - I didn't before because I think its not related - I dumbed it down to simplest model and still fail. – cyberwombat Dec 20 '17 at 15:59
  • Do, you do realize that these two approaches are not equivalent. The first attempts to run all the queries in parallel and the second runs them in series. If your DB is capable of handling things in parallel, then they both should work, but if not, then maybe there are issue. Now that you've fixed the missing `.toArray()`, there is nothing here we can help with. The problem is not in this particular code, it's likely behind the scenes of `c.find().toArray()` or in `getCollection()`. For example, do you have issues trying to get two open DB connections at the same time? – jfriend00 Dec 20 '17 at 22:42

0 Answers0