Issue with testing Rate Limiter


#1

I am trying to set a rule to limit a collection’s methods from having too many operations in a certain amount of time using rate limiter. Then, I am trying to test that rule and I am getting errors in the testing and I am not sure why.

Example:

Collection - Employees - RateLimiter Rule

const EMPLOYEES_METHODS = _.pluck([
  insert,
  updateName
])

// limit rate of operations on employees
if (Meteor.isServer) {
  DDPRateLimiter.addRule({
    name(name) {
      return _.contains(EMPLOYEES_METHODS, name)
    },

    connectionId() { return true }
  }, 5, 1000)
}

Tests - Employees - RateLimiter Rule

describe('rate limiting', function () {
      it('does not allow more than 5 operations rapidly', function () {
        const connection = DDP.connect(Meteor.absoluteUrl());

        const newCompanyId = Factory.create('company')._id
        const number = faker.random.number()
        const lastName = faker.name.lastName()
        const firstName = faker.name.firstName()
        const isActive = faker.random.boolean()
        const email = faker.internet.email()

        const newEmployee = {
          companyId: newCompanyId,
          number,
          lastName,
          firstName,
          isActive,
          email,
        }


        _.times(5, () => {
          connection.call(insert.name, newEmployee);
        });

        assert.throws(() => {
          connection.call(insert.name, newEmployee);
        }, Meteor.Error, /too-many-requests/);

        connection.disconnect();
      });
    })

Result

Not Pictured

  • Sometimes I will get an error that says Internal Server Error 500 instead of the error pictured above for just this test.

Thanks in advance for the help :slight_smile:


#2

Is it possible that the methods are just not running fast enough to trigger the throw error?