From 0363c1d062483022c7c1d2f2357edee3713073ca Mon Sep 17 00:00:00 2001 From: Kaloian Manassiev Date: Wed, 22 Jan 2020 06:21:27 -0500 Subject: SERVER-45599 Backport the 'assertChangeStreamEventEq' function from master This function is necessary for adding change streams testing and was picked up from these two commits: 065f3ef77de and 38a0e128d66, but it doesn't backport the commits themselves. --- jstests/libs/change_stream_util.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/jstests/libs/change_stream_util.js b/jstests/libs/change_stream_util.js index 5cb8b933ba3..b52896d08b7 100644 --- a/jstests/libs/change_stream_util.js +++ b/jstests/libs/change_stream_util.js @@ -53,6 +53,33 @@ function assertInvalidateOp({cursor, opType}) { } } +/** + * Helper to check whether a change stream event matches the given expected event. Ignores the + * resume token and clusterTime unless they are explicitly listed in the expectedEvent. + */ +function assertChangeStreamEventEq(actualEvent, expectedEvent) { + const testEvent = Object.assign({}, actualEvent); + if (!expectedEvent.hasOwnProperty("_id")) { + delete testEvent._id; // Remove the resume token, if present. + } + if (!expectedEvent.hasOwnProperty("clusterTime")) { + delete testEvent.clusterTime; // Remove the cluster time, if present. + } + + // The change stream transaction passthrough causes operations to have txnNumber and lsid + // values that the test doesn't expect, which can cause comparisons to fail. + if (!expectedEvent.hasOwnProperty("txnNumber")) { + delete testEvent.txnNumber; // Remove the txnNumber, if present. + } + if (!expectedEvent.hasOwnProperty("lsid")) { + delete testEvent.lsid; // Remove the lsid, if present. + } + assert.docEq(testEvent, + expectedEvent, + "Change did not match expected change. Expected change: " + tojson(expectedEvent) + + ", Actual change: " + tojson(testEvent)); +} + function ChangeStreamTest(_db, name = "ChangeStreamTest") { load("jstests/libs/namespace_utils.js"); // For getCollectionNameFromFullNamespace. -- cgit v1.2.1