Skip to content

Commit

Permalink
Add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
gagik committed Oct 1, 2024
1 parent 7641c1d commit 91b6ff2
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions packages/e2e-tests/test/e2e-current-op.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { expect } from 'chai';
import {
skipIfApiStrict,
startSharedTestServer,
} from '../../../testing/integration-testing-hooks';
import { TestShell } from './test-shell';

describe('e2e currentOp', function () {
skipIfApiStrict();
afterEach(TestShell.cleanup);

const testServer = startSharedTestServer();

context('with 2 shells', function () {
let helperShell: TestShell;
let currentOpShell: TestShell;

const OPERATION_TIME = 1_000;
const CURRENT_OP_WAIT_TIME = 100;
this.timeout(OPERATION_TIME * 2);

beforeEach(async function () {
helperShell = TestShell.start({
args: [await testServer.connectionString()],
});
currentOpShell = TestShell.start({
args: [await testServer.connectionString()],
});
await helperShell.waitForPrompt();

// Insert a dummy object so find commands will actually run with the delay.
await helperShell.executeLine('db.coll.insertOne({})');
});

it('should return the correct operation', async function () {
const regexOperation = helperShell.executeLine(
`db.coll.find({$where: function() { sleep(${OPERATION_TIME}) }}).projection({test: 1})`
);
helperShell.assertNoErrors();
await currentOpShell.executeLine(`sleep(${CURRENT_OP_WAIT_TIME})`);
let currentOpCall = await currentOpShell.executeLine(`db.currentOp()`);

currentOpShell.assertNoErrors();
expect(currentOpCall).to.include("find: 'coll'");
expect(currentOpCall).to.include(
`filter: { '$where': Code('function() { sleep(${OPERATION_TIME}) }') }`
);
expect(currentOpCall).to.include('projection: { test: 1 }');

console.log(currentOpCall);

await regexOperation;

currentOpCall = await currentOpShell.executeLine(`db.currentOp()`);

currentOpShell.assertNoErrors();
expect(currentOpCall).not.to.include("find: 'coll'");
expect(currentOpCall).not.to.include(
`filter: { '$where': Code('function() { sleep(${OPERATION_TIME}) }') }`
);
expect(currentOpCall).not.to.include('projection: { test: 1 }');
});

it('should work when the operation contains regex', async function () {
const regExpString = '^(?i)\\Qchho0842\\E';

void helperShell.executeLine(
`db.coll.find({$where: function() { sleep(${OPERATION_TIME}) }}).projection({re: BSONRegExp('${regExpString}')})`
);
helperShell.assertNoErrors();

await currentOpShell.executeLine(`sleep(${CURRENT_OP_WAIT_TIME})`);

const currentOpCall = await currentOpShell.executeLine(`db.currentOp()`);
currentOpShell.assertNoErrors();

const regExpStringWithoutEscapes = regExpString.replace(/\\/g, '');

expect(currentOpCall).to.include(
`projection: { re: BSONRegExp('${regExpStringWithoutEscapes}', '') }`
);
});
});
});

0 comments on commit 91b6ff2

Please sign in to comment.