Skip to content

Commit

Permalink
Merge pull request #118 from monti-apm/fix/flaky-redis-tests
Browse files Browse the repository at this point in the history
fix: flaky tests
  • Loading branch information
zodern authored May 9, 2024
2 parents 6ab8b6f + 1f4befe commit eb61d49
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions tests/hijack/redis_oplog.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TestData, TestDataRedis, TestDataRedisNoRaceProtection } from '../_helpers/globals';
import { GetMeteorClient, RegisterMethod, RegisterPublication, SubscribeAndWait } from '../_helpers/helpers';
import { FindMetricsForPub, GetMeteorClient, RegisterMethod, RegisterPublication, SubscribeAndWait, addTestWithRoundedTime } from '../_helpers/helpers';

/**
* We only track the observers coming from subscriptions (which have `ownerInfo`)
*/
Tinytest.add('Database - Redis Oplog - Added', function (test) {
addTestWithRoundedTime('Database - Redis Oplog - Added', function (test) {
const pub = RegisterPublication(() => TestData.find({}));

TestData.remove({});
Expand All @@ -23,7 +23,7 @@ Tinytest.add('Database - Redis Oplog - Added', function (test) {

Meteor._sleepForMs(100);

const metrics = Kadira.models.pubsub._getMetrics(new Date(), pub);
const metrics = FindMetricsForPub(pub);

test.equal(metrics.initiallyAddedDocuments, 4);
test.equal(metrics.totalObservers, 1);
Expand All @@ -36,7 +36,7 @@ Tinytest.add('Database - Redis Oplog - Added', function (test) {
Meteor._sleepForMs(100);
});

Tinytest.add('Database - Redis Oplog - Added with limit/skip', function (test) {
addTestWithRoundedTime('Database - Redis Oplog - Added with limit/skip', function (test) {
const pub = RegisterPublication(() => TestData.find({name: 'test'}, {limit: 2, skip: 0}));

TestData.remove({});
Expand All @@ -45,14 +45,14 @@ Tinytest.add('Database - Redis Oplog - Added with limit/skip', function (test) {

const client = GetMeteorClient();
const sub = SubscribeAndWait(client, pub);
let metrics = Kadira.models.pubsub._getMetrics(new Date(), pub);
let metrics = FindMetricsForPub(pub);

test.equal(metrics.polledDocuments, 1);

TestData.insert({ name: 'test' });
Meteor._sleepForMs(100);
// as the selector IS matched, redis-oplog triggers a requery
metrics = Kadira.models.pubsub._getMetrics(new Date(), pub);
metrics = FindMetricsForPub(pub);
// 1 from initial subscription, 1 findOne + requery(2)
test.equal(metrics.polledDocuments, 4);

Expand All @@ -61,7 +61,7 @@ Tinytest.add('Database - Redis Oplog - Added with limit/skip', function (test) {

Meteor._sleepForMs(100);

metrics = Kadira.models.pubsub._getMetrics(new Date(), pub);
metrics = FindMetricsForPub(pub);

test.equal(metrics.initiallyAddedDocuments, 1);
test.equal(metrics.totalObservers, 1);
Expand All @@ -76,7 +76,7 @@ Tinytest.add('Database - Redis Oplog - Added with limit/skip', function (test) {

Meteor._sleepForMs(100);
});
Tinytest.add('Database - Redis Oplog - With protect against race condition - Check Trace', function (test) {
addTestWithRoundedTime('Database - Redis Oplog - With protect against race condition - Check Trace', function (test) {
// in this case, the mutator will refetch the doc when publishing it
const methodId = RegisterMethod(() => TestDataRedis.update({name: 'test'}, {$set: {name: 'abv'}}));

Expand All @@ -96,7 +96,7 @@ Tinytest.add('Database - Redis Oplog - With protect against race condition - Che

Meteor._sleepForMs(100);
});
Tinytest.add('Database - Redis Oplog - With protect against race condition - check for finds after receiving the msg', function (test) {
addTestWithRoundedTime('Database - Redis Oplog - With protect against race condition - check for finds after receiving the msg', function (test) {
// in this case, every subscriber will refetch the doc once when receiving it
const pub = RegisterPublication(() => TestDataRedis.find({name: 'test'}));

Expand All @@ -108,19 +108,19 @@ Tinytest.add('Database - Redis Oplog - With protect against race condition - che
const client2 = GetMeteorClient();
const sub = SubscribeAndWait(client, pub);
const sub2 = SubscribeAndWait(client2, pub);
let metrics = Kadira.models.pubsub._getMetrics(new Date(), pub);
let metrics = FindMetricsForPub(pub);

test.equal(metrics.polledDocuments, 1);

TestDataRedis.insert({ name: 'test' });
Meteor._sleepForMs(100);

metrics = Kadira.models.pubsub._getMetrics(new Date(), pub);
metrics = FindMetricsForPub(pub);
test.equal(metrics.polledDocuments, 2);

Meteor._sleepForMs(100);

metrics = Kadira.models.pubsub._getMetrics(new Date(), pub);
metrics = FindMetricsForPub(pub);

test.equal(metrics.initiallyAddedDocuments, 1);
test.equal(metrics.totalObservers, 2);
Expand All @@ -136,7 +136,7 @@ Tinytest.add('Database - Redis Oplog - With protect against race condition - che
Meteor._sleepForMs(100);
});

Tinytest.add('Database - Redis Oplog - Without protect against race condition - no extraneous finds', function (test) {
addTestWithRoundedTime('Database - Redis Oplog - Without protect against race condition - no extraneous finds', function (test) {
// in this case, no subscriber will refetch the doc when receiving it
const pub = RegisterPublication(() => TestDataRedisNoRaceProtection.find({}));

Expand All @@ -147,19 +147,19 @@ Tinytest.add('Database - Redis Oplog - Without protect against race condition -
const client = GetMeteorClient();
const sub = SubscribeAndWait(client, pub);
const sub2 = SubscribeAndWait(client, pub);
let metrics = Kadira.models.pubsub._getMetrics(new Date(), pub);
let metrics = FindMetricsForPub(pub);

test.equal(metrics.polledDocuments, 1);

TestDataRedisNoRaceProtection.insert({ name: 'test' });
Meteor._sleepForMs(100);

metrics = Kadira.models.pubsub._getMetrics(new Date(), pub);
metrics = FindMetricsForPub(pub);
test.equal(metrics.polledDocuments, 1);

Meteor._sleepForMs(100);

metrics = Kadira.models.pubsub._getMetrics(new Date(), pub);
metrics = FindMetricsForPub(pub);

test.equal(metrics.initiallyAddedDocuments, 1);
test.equal(metrics.totalObservers, 2);
Expand All @@ -174,7 +174,7 @@ Tinytest.add('Database - Redis Oplog - Without protect against race condition -

Meteor._sleepForMs(100);
});
Tinytest.add('Database - Redis Oplog - Without protect against race condition - Check Trace', function (test) {
addTestWithRoundedTime('Database - Redis Oplog - Without protect against race condition - Check Trace', function (test) {
// in this case, the mutator will refetch the doc when publishing it
const methodId = RegisterMethod(() => TestDataRedisNoRaceProtection.update({name: 'test'}, {$set: {name: 'abv'}}));

Expand All @@ -196,7 +196,7 @@ Tinytest.add('Database - Redis Oplog - Without protect against race condition -
Meteor._sleepForMs(100);
});

Tinytest.add('Database - Redis Oplog - Removed', function (test) {
addTestWithRoundedTime('Database - Redis Oplog - Removed', function (test) {
const pub = RegisterPublication(() => TestData.find({}));

TestData.remove({});
Expand All @@ -214,7 +214,7 @@ Tinytest.add('Database - Redis Oplog - Removed', function (test) {

Meteor._sleepForMs(100);

let metrics = Kadira.models.pubsub._getMetrics(new Date(), pub);
let metrics = FindMetricsForPub(pub);

test.equal(metrics.totalObservers, 1);
test.equal(metrics.liveRemovedDocuments, 1);
Expand All @@ -223,7 +223,7 @@ Tinytest.add('Database - Redis Oplog - Removed', function (test) {

Meteor._sleepForMs(100);

metrics = Kadira.models.pubsub._getMetrics(new Date(), pub);
metrics = FindMetricsForPub(pub);

test.equal(metrics.totalObservers, 1);
test.equal(metrics.liveRemovedDocuments, 3);
Expand All @@ -234,7 +234,7 @@ Tinytest.add('Database - Redis Oplog - Removed', function (test) {
Meteor._sleepForMs(100);
});

Tinytest.add('Database - Redis Oplog - Changed', function (test) {
addTestWithRoundedTime('Database - Redis Oplog - Changed', function (test) {
const pub = RegisterPublication(() => TestData.find({}));

TestData.remove({});
Expand All @@ -252,16 +252,16 @@ Tinytest.add('Database - Redis Oplog - Changed', function (test) {

Meteor._sleepForMs(100);

let metrics = Kadira.models.pubsub._getMetrics(new Date(), pub);
let metrics = FindMetricsForPub(pub);

test.equal(metrics.totalObservers, 1);
test.equal(metrics.liveChangedDocuments, 1);

TestData.update({}, { $set: { name: 'test5' } }, { multi: true });

Meteor._sleepForMs(100);
Meteor._sleepForMs(200);

metrics = Kadira.models.pubsub._getMetrics(new Date(), pub);
metrics = FindMetricsForPub(pub);

test.equal(metrics.totalObservers, 1);
test.equal(metrics.liveChangedDocuments, 4);
Expand Down

0 comments on commit eb61d49

Please sign in to comment.