Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating from 5.10.4 to 5.10.5 is breaking a test using FakeTimers #9431

Closed
rvillane opened this issue Sep 18, 2020 · 6 comments
Closed

Updating from 5.10.4 to 5.10.5 is breaking a test using FakeTimers #9431

rvillane opened this issue Sep 18, 2020 · 6 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@rvillane
Copy link

Do you want to request a feature or report a bug?
report a potential bug or looking for guidance about new functionality recently added

What is the current behavior?
After I updated mongoose from 5.10.4 to 5.10.5 , an Aggregation pipeline that runs a query is now broken. I know that in v5.10.5, AggregationCursor was improved with asyncIterator, however, in my particular case, I do not need to iterate the results, just return the matching records as a JSON response.

If the current behavior is a bug, please provide the steps to reproduce.
the following function executes an aggregation pipeline to find matching documents

async function getOrderCountByProcDate(auditTs) {
    // vars used below are defined here, I removed then as they are not relevant to the issue
    return TransformedOrder.aggregate(
        [
            { $match: {
                'properties.orderCapturePersistTs': {
                    $gte: startCaptureDateStr,
                    $lt: endCaptureDateStr,
                },
            } },
            { $match: {
                'properties.orderProcDate': {
                    $gte: startProcDateStr,
                    $lt: endProcDateStr,
                },
            } },
            { $group: {
                _id: '$properties.orderProcDate',
                orderCount: { $sum: 1 },
            } },
            { $sort: {
                _id: -1,
            } },
            { $project: {
                _id: 0,
                orderProcDate: '$_id',
                orderCount: '$orderCount',
            } },
        ],
    ).exec();
}

and then, the function is called like this:

const results = await readModel.getOrderCountByProcDate(now.toDate());
with the AsyncIterator changes in version 5.10.5 is now throwing the following error:

[LOCAL] failed with the following error - Error: socket hang up
    at createHangUpError (_http_client.js:332:15)
    at Socket.socketOnEnd (_http_client.js:435:23)
    at Socket.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)

What is the expected behavior?
The query is expected to run and return results. I tried adjusting the query according to the example provided in 5.10.5 code:

// Async iterator without explicitly calling `cursor()`. Mongoose still
// creates an AggregationCursor instance internally.
const agg = Model.aggregate([{ $match: { age: { $gte: 25 } } }]);
for await (const doc of agg) {
   console.log(doc.name);
}
// You can also use an AggregationCursor instance for async iteration
const cursor = Model.aggregate([{ $match: { age: { $gte: 25 } } }]).cursor();
for await (const doc of cursor) {
   console.log(doc.name);
}

however when executing the for await loop, I'm getting the exact same socket hang up error indicated above.

Any ideas? if I run that Aggregation in a mongoDB client is works fine, then looks like the query itself is not the problem.

thanks

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
NodeJS v10.19.0
MongoDB Server 3.6.9
MongoDB native driver 3.6.2
Mongoose 5.10.5

@vkarpov15
Copy link
Collaborator

Are you actually using cursors, or are you just using getOrderCountByProcDate() as written? If you're not using cursors, the cursor change shouldn't affect you.

We'll take a look this week and see if we can repro this issue.

@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Sep 19, 2020
@vkarpov15 vkarpov15 added this to the 5.10.7 milestone Sep 19, 2020
@rvillane
Copy link
Author

correct, in this particular case we are not using cursors, just using getOrderCountByProcDate() as written.

thanks

@vkarpov15
Copy link
Collaborator

The below script seems to execute without any errors. Can you please modify the below script to demonstrate your issue?

'use strict';
  
const mongoose = require('mongoose');

mongoose.set('useFindAndModify', false);

const { Schema } = mongoose;

run().catch(err => console.log(err));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

  await mongoose.connection.dropDatabase();

  const Model = mongoose.model('Test', Schema({ name: String }));

  await Model.create([
    { name: 'test1' },
    { name: 'test2' },
    { name: 'test3' }
  ]);

  await Model.aggregate([{ $match: { name: { $exists: true } } }]).exec();
  console.log('done');
}

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Sep 22, 2020
@vkarpov15 vkarpov15 removed this from the 5.10.7 milestone Sep 22, 2020
@rvillane
Copy link
Author

I think the following is closer to my scenario broken in 5.10.5:

'use strict';
  
const mongoose = require('mongoose');

mongoose.set('useFindAndModify', false);

const { Schema } = mongoose;
const Model = mongoose.model('Test', Schema({ name: String }));

run().catch(err => console.log(err));

async someFunc() {
    return Model.aggregate([{ $match: { name: { $exists: true } } }]).exec();
}

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

  await mongoose.connection.dropDatabase();

  await Model.create([
    { name: 'test1' },
    { name: 'test2' },
    { name: 'test3' }
  ]);

  const results = await someFunc();
  console.log(`done with results: ${JSON.stringify(results)}`);
}

@vkarpov15
Copy link
Collaborator

@rvillane that script also executes correctly. Can you please create a script that demonstrates the error you're seeing?

@rvillane
Copy link
Author

I believe my initial assessment of the issue is incorrect, after double checking the context of the failing function, is an integration test that uses FakeTimers "@sinonjs/fake-timers": "6.0.1" before executing, then is not related to Aggregate functions.

FakeTimers.install({ now: new Date(2018, 11, 4) });

the integration test worked fine using mongoose 5.10.4 and is now broken with v5.10.5

This seems to be a duplicate of #9437

@rvillane rvillane changed the title Updating from 5.10.4 to 5.10.5 is breaking Aggregate queries Updating from 5.10.4 to 5.10.5 is breaking a test using FakeTimers Sep 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

No branches or pull requests

2 participants