blob: e85362557f9934955e0675a21c45626139432023 [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { assert, test } from 'vitest'
import { ThrottleLayer } from '../index.mjs'
function assertInvalidArgument(createLayer, field) {
let error
try {
createLayer()
} catch (err) {
error = err
}
assert.instanceOf(error, Error)
assert.match(error.message, new RegExp(field))
}
// N-API converts these values to 0 when it reads the u32 constructor arguments.
const zeroValues = [0, 2 ** 32, NaN, Infinity]
for (const bandwidth of zeroValues) {
test(`ThrottleLayer rejects bandwidth ${bandwidth}`, () => {
assertInvalidArgument(() => new ThrottleLayer(bandwidth, 1024), 'bandwidth')
})
}
for (const burst of zeroValues) {
test(`ThrottleLayer rejects burst ${burst}`, () => {
assertInvalidArgument(() => new ThrottleLayer(1024, burst), 'burst')
})
}
test('ThrottleLayer accepts positive bandwidth and burst', () => {
const layer = new ThrottleLayer(1024, 2048)
assert.ok(layer.build())
})