[Beta][Task]16961420: fix throttleMgr tests (#1978)
* fix throttlrMgr config * update throttle mgr * update throttle mgr
This commit is contained in:
Родитель
bc620b524c
Коммит
2cf0854cd1
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -61,7 +61,7 @@ export class ThrottleMgrTest extends AITestClass {
|
|||
interval: {
|
||||
monthInterval: 2,
|
||||
dayInterval: 10,
|
||||
maxTimesPerMonth: 1
|
||||
daysOfMonth: undefined
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
|
@ -90,8 +90,8 @@ export class ThrottleMgrTest extends AITestClass {
|
|||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: 3,
|
||||
dayInterval: 28,
|
||||
maxTimesPerMonth: 1
|
||||
dayInterval: undefined,
|
||||
daysOfMonth: [28]
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
|
@ -104,6 +104,161 @@ export class ThrottleMgrTest extends AITestClass {
|
|||
}
|
||||
});
|
||||
|
||||
this.testCase({
|
||||
name: "ThrottleMgrTest: monthInterval should be set to 3 when dayInterval and monthInterval are both undefined",
|
||||
test: () => {
|
||||
|
||||
let config = {
|
||||
msgKey: this._msgKey,
|
||||
disabled: false,
|
||||
limit: {
|
||||
samplingRate: 100,
|
||||
maxSendNumber:1
|
||||
} as IThrottleLimit,
|
||||
interval: {
|
||||
daysOfMonth: [25, 26, 28]
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
let expectedConfig = {
|
||||
msgKey: this._msgKey,
|
||||
disabled: false,
|
||||
limit: {
|
||||
samplingRate: 100,
|
||||
maxSendNumber:1
|
||||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: 3,
|
||||
dayInterval: undefined,
|
||||
daysOfMonth: [25, 26, 28]
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
let throttleMgr = new ThrottleMgr(config, this._core);
|
||||
let actualConfig = throttleMgr.getConfig();
|
||||
Assert.deepEqual(expectedConfig, actualConfig);
|
||||
|
||||
let isTriggered = throttleMgr.isTriggered();
|
||||
Assert.equal(isTriggered, false);
|
||||
}
|
||||
});
|
||||
|
||||
this.testCase({
|
||||
name: "ThrottleMgrTest: monthInterval and daysOfMonth should be changed to default when dayInterval is defined",
|
||||
test: () => {
|
||||
|
||||
let config = {
|
||||
msgKey: this._msgKey,
|
||||
disabled: false,
|
||||
limit: {
|
||||
samplingRate: 100,
|
||||
maxSendNumber:1
|
||||
} as IThrottleLimit,
|
||||
interval: {
|
||||
dayInterval: 100
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
let expectedConfig = {
|
||||
msgKey: this._msgKey,
|
||||
disabled: false,
|
||||
limit: {
|
||||
samplingRate: 100,
|
||||
maxSendNumber:1
|
||||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: undefined,
|
||||
dayInterval: 100,
|
||||
daysOfMonth: undefined
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
let throttleMgr = new ThrottleMgr(config, this._core);
|
||||
let actualConfig = throttleMgr.getConfig();
|
||||
Assert.deepEqual(expectedConfig, actualConfig);
|
||||
|
||||
let isTriggered = throttleMgr.isTriggered();
|
||||
Assert.equal(isTriggered, false);
|
||||
}
|
||||
});
|
||||
|
||||
this.testCase({
|
||||
name: "ThrottleMgrTest: Throttle Manager should trigger when current date is in daysOfMonth",
|
||||
test: () => {
|
||||
let date = new Date();
|
||||
let day = date.getUTCDate();
|
||||
let daysOfMonth;
|
||||
if (day == 1) {
|
||||
daysOfMonth = [31,day];
|
||||
} else {
|
||||
daysOfMonth = [day-1, day];
|
||||
}
|
||||
let config = {
|
||||
msgKey: this._msgKey,
|
||||
disabled: false,
|
||||
limit: {
|
||||
samplingRate: 100,
|
||||
maxSendNumber:1
|
||||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: 3,
|
||||
dayInterval: 28,
|
||||
daysOfMonth: daysOfMonth
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
let throttleMgr = new ThrottleMgr(config, this._core);
|
||||
let actualConfig = throttleMgr.getConfig();
|
||||
Assert.deepEqual(config, actualConfig);
|
||||
|
||||
let isTriggered = throttleMgr.isTriggered();
|
||||
Assert.equal(isTriggered, false);
|
||||
|
||||
let canThrottle = throttleMgr.canThrottle();
|
||||
Assert.equal(canThrottle, true, "should throttle");
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
this.testCase({
|
||||
name: "ThrottleMgrTest: Throttle Manager should trigger when interval config is undefined and current date 28",
|
||||
test: () => {
|
||||
let date = new Date();
|
||||
let day = date.getUTCDate();
|
||||
let config = {
|
||||
msgKey: this._msgKey
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
let expectedConfig = {
|
||||
msgKey: this._msgKey,
|
||||
disabled: false,
|
||||
limit: {
|
||||
samplingRate: 100,
|
||||
maxSendNumber:1
|
||||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: 3,
|
||||
dayInterval: undefined,
|
||||
daysOfMonth: [28]
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
let throttleMgr = new ThrottleMgr(config, this._core);
|
||||
let actualConfig = throttleMgr.getConfig();
|
||||
Assert.deepEqual(expectedConfig, actualConfig);
|
||||
|
||||
let isTriggered = throttleMgr.isTriggered();
|
||||
Assert.equal(isTriggered, false);
|
||||
|
||||
let shouldTrigger = false;
|
||||
if (day === 28) {
|
||||
shouldTrigger = true;
|
||||
}
|
||||
|
||||
let canThrottle = throttleMgr.canThrottle();
|
||||
Assert.equal(canThrottle, shouldTrigger, "should only throttle on 28th");
|
||||
}
|
||||
});
|
||||
|
||||
this.testCase({
|
||||
name: "ThrottleMgrTest: should not trigger throttle when disabled is true",
|
||||
test: () => {
|
||||
|
@ -147,8 +302,7 @@ export class ThrottleMgrTest extends AITestClass {
|
|||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: 3,
|
||||
dayInterval: 1,
|
||||
maxTimesPerMonth: 100
|
||||
dayInterval: 1
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
|
@ -191,8 +345,7 @@ export class ThrottleMgrTest extends AITestClass {
|
|||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: 3,
|
||||
dayInterval: 1,
|
||||
maxTimesPerMonth: 100
|
||||
dayInterval: 1
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
|
@ -210,6 +363,16 @@ export class ThrottleMgrTest extends AITestClass {
|
|||
name: "ThrottleMgrTest: should not trigger throttle when day interval requirements are not meet",
|
||||
test: () => {
|
||||
let date = new Date();
|
||||
let curDate = date.getUTCDate();
|
||||
let curMonth = date.getUTCMonth();
|
||||
if (curDate === 1) {
|
||||
curMonth -= 1;
|
||||
curDate = 28;
|
||||
} else {
|
||||
curDate -= 1;
|
||||
}
|
||||
date.setUTCDate(curDate);
|
||||
date.setUTCMonth(curMonth);
|
||||
let storageObj = {
|
||||
date: date,
|
||||
count: 0
|
||||
|
@ -225,8 +388,7 @@ export class ThrottleMgrTest extends AITestClass {
|
|||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: 1,
|
||||
dayInterval: 31,
|
||||
maxTimesPerMonth: 100
|
||||
dayInterval: 31
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
|
@ -259,8 +421,7 @@ export class ThrottleMgrTest extends AITestClass {
|
|||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: 1,
|
||||
dayInterval: 1,
|
||||
maxTimesPerMonth: 100
|
||||
dayInterval: 1
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
|
@ -295,8 +456,7 @@ export class ThrottleMgrTest extends AITestClass {
|
|||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: 4,
|
||||
dayInterval: 1,
|
||||
maxTimesPerMonth: 100
|
||||
dayInterval: 1
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
|
@ -310,41 +470,6 @@ export class ThrottleMgrTest extends AITestClass {
|
|||
|
||||
});
|
||||
|
||||
this.testCase({
|
||||
name: "ThrottleMgrTest: should not trigger throttle when maxSentTimes is not meet",
|
||||
test: () => {
|
||||
let date = new Date();
|
||||
let day = date.getUTCDate();
|
||||
let storageObj = {
|
||||
date: date,
|
||||
count: 0
|
||||
}
|
||||
window.localStorage[this._storageName] = JSON.stringify(storageObj);
|
||||
let maxTimes = day-1;
|
||||
let config = {
|
||||
msgKey: this._msgKey,
|
||||
disabled: false,
|
||||
limit: {
|
||||
samplingRate: 1000000,
|
||||
maxSendNumber: 100
|
||||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: 1,
|
||||
dayInterval: 1,
|
||||
maxTimesPerMonth: maxTimes
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
let throttleMgr = new ThrottleMgr(config, this._core);
|
||||
let canThrottle = throttleMgr.canThrottle();
|
||||
Assert.equal(canThrottle, false);
|
||||
|
||||
let isTriggered = throttleMgr.isTriggered();
|
||||
Assert.equal(isTriggered, false);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
this.testCase({
|
||||
name: "ThrottleMgrTest: should not trigger throttle when _isTrigger state is true (in valid send message date)",
|
||||
test: () => {
|
||||
|
@ -363,8 +488,7 @@ export class ThrottleMgrTest extends AITestClass {
|
|||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: 1,
|
||||
dayInterval: 1,
|
||||
maxTimesPerMonth: 33
|
||||
dayInterval: 1
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
|
@ -410,8 +534,7 @@ export class ThrottleMgrTest extends AITestClass {
|
|||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: 1,
|
||||
dayInterval: date.getUTCDate(),
|
||||
maxTimesPerMonth: 1
|
||||
dayInterval: date.getUTCDate()
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
|
@ -455,8 +578,7 @@ export class ThrottleMgrTest extends AITestClass {
|
|||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: 1,
|
||||
dayInterval: 1,
|
||||
maxTimesPerMonth: 100
|
||||
dayInterval: 1
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
|
@ -535,8 +657,7 @@ export class ThrottleMgrTest extends AITestClass {
|
|||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: 1,
|
||||
dayInterval: 1,
|
||||
maxTimesPerMonth: 100
|
||||
dayInterval: 1
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
|
@ -584,8 +705,7 @@ export class ThrottleMgrTest extends AITestClass {
|
|||
} as IThrottleLimit,
|
||||
interval: {
|
||||
monthInterval: 1,
|
||||
dayInterval: 1,
|
||||
maxTimesPerMonth: 100
|
||||
dayInterval: 1
|
||||
} as IThrottleInterval
|
||||
} as IThrottleMgrConfig;
|
||||
|
||||
|
|
|
@ -27,21 +27,21 @@ export interface IThrottleInterval {
|
|||
/**
|
||||
* Identifies month interval that items can be sent
|
||||
* For example, if it is set to 2 and start date is in Jan, items will be sent out every two months (Jan, March, May etc.)
|
||||
* Default: 3
|
||||
* If both monthInterval and dayInterval are undefined, it will be set to 3
|
||||
*/
|
||||
monthInterval?: number;
|
||||
|
||||
/**
|
||||
* Identifies days that items can be sent within a month
|
||||
* Default: 28
|
||||
* Identifies days Interval from start date that items can be sent
|
||||
* Default: undefined
|
||||
*/
|
||||
dayInterval?: number;
|
||||
|
||||
/**
|
||||
* Identifies max times items can be sent within a month
|
||||
* Default: 1
|
||||
* Identifies days within each month that items can be sent
|
||||
* If both monthInterval and dayInterval are undefined, it will be default to [28]
|
||||
*/
|
||||
maxTimesPerMonth?: number;
|
||||
daysOfMonth?: number[];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import {
|
||||
IAppInsightsCore, IDiagnosticLogger, _eInternalMessageId, _throwInternal, arrForEach, eLoggingSeverity, isNotNullOrUndefined,
|
||||
isNullOrUndefined, randomValue, safeGetLogger, strTrim
|
||||
isNullOrUndefined, randomValue, safeGetLogger, strTrim, arrIndexOf
|
||||
} from "@microsoft/applicationinsights-core-js";
|
||||
import { IThrottleMsgKey } from "./Enums";
|
||||
import { IThrottleLocalStorageObj, IThrottleMgrConfig, IThrottleResult } from "./Interfaces/IThrottleMgr";
|
||||
import { IThrottleInterval, IThrottleLocalStorageObj, IThrottleMgrConfig, IThrottleResult } from "./Interfaces/IThrottleMgr";
|
||||
import { utlCanUseLocalStorage, utlGetLocalStorage, utlSetLocalStorage } from "./StorageHelperFuncs";
|
||||
|
||||
const THROTTLE_STORAGE_PREFIX = "appInsightsThrottle";
|
||||
|
@ -34,6 +34,7 @@ export class ThrottleMgr {
|
|||
let _namePrefix: string;
|
||||
let _queue: Array<SendMsgParameter>;
|
||||
let _isReady: boolean = false;
|
||||
let _isSpecificDaysGiven: boolean = false;
|
||||
|
||||
_initConfig();
|
||||
|
||||
|
@ -150,14 +151,11 @@ export class ThrottleMgr {
|
|||
_config = {} as any;
|
||||
_config.disabled = !!configMgr.disabled;
|
||||
_config.msgKey = configMgr.msgKey;
|
||||
// default: send data on 28th every 3 month each year
|
||||
let interval = {
|
||||
// dafault: sent every three months
|
||||
monthInterval: configMgr.interval?.monthInterval || 3,
|
||||
dayInterval : configMgr.interval?.dayInterval || 28,
|
||||
maxTimesPerMonth: configMgr.interval?.maxTimesPerMonth || 1
|
||||
};
|
||||
_config.interval = interval;
|
||||
|
||||
let configInterval = configMgr.interval || {};
|
||||
_isSpecificDaysGiven = configInterval?.daysOfMonth && configInterval?.daysOfMonth.length > 0;
|
||||
_config.interval = _getIntervalConfig(configInterval);
|
||||
|
||||
let limit = {
|
||||
samplingRate: configMgr.limit?.samplingRate || 100,
|
||||
// dafault: every time sent only 1 event
|
||||
|
@ -174,15 +172,49 @@ export class ThrottleMgr {
|
|||
}
|
||||
}
|
||||
|
||||
function _getIntervalConfig(interval: IThrottleInterval) {
|
||||
interval = interval || {};
|
||||
let monthInterval = interval?.monthInterval;
|
||||
let dayInterval = interval?.dayInterval;
|
||||
|
||||
// default: send data every 3 month each year
|
||||
if (isNullOrUndefined(monthInterval) && isNullOrUndefined(dayInterval)) {
|
||||
interval.monthInterval = 3;
|
||||
if (!_isSpecificDaysGiven) {
|
||||
// default: send data on 28th
|
||||
interval.daysOfMonth = [28];
|
||||
_isSpecificDaysGiven = true;
|
||||
}
|
||||
}
|
||||
interval = {
|
||||
// dafault: sent every three months
|
||||
monthInterval: interval?.monthInterval,
|
||||
dayInterval: interval?.dayInterval,
|
||||
daysOfMonth: interval?.daysOfMonth
|
||||
} as IThrottleInterval;
|
||||
return interval;
|
||||
}
|
||||
|
||||
function _canThrottle(config: IThrottleMgrConfig, canUseLocalStorage: boolean, localStorageObj: IThrottleLocalStorageObj) {
|
||||
if (!config.disabled && canUseLocalStorage && isNotNullOrUndefined(localStorageObj)) {
|
||||
let curDate = _getThrottleDate();
|
||||
let date = localStorageObj.date;
|
||||
let interval = config.interval;
|
||||
let monthExpand = (curDate.getUTCFullYear() - date.getUTCFullYear()) * 12 + curDate.getUTCMonth() - date.getUTCMonth();
|
||||
let monthCheck = _checkInterval(interval.monthInterval, 0, monthExpand);
|
||||
let dayCheck = _checkInterval(interval.dayInterval, 0, curDate.getUTCDate()) -1;
|
||||
return monthCheck >= 0 && dayCheck >= 0 && dayCheck <= config.interval.maxTimesPerMonth;
|
||||
let monthCheck = 1;
|
||||
if (interval?.monthInterval) {
|
||||
let monthExpand = (curDate.getUTCFullYear() - date.getUTCFullYear()) * 12 + curDate.getUTCMonth() - date.getUTCMonth();
|
||||
monthCheck = _checkInterval(interval.monthInterval, 0, monthExpand);
|
||||
}
|
||||
|
||||
let dayCheck = 1;
|
||||
if (_isSpecificDaysGiven) {
|
||||
dayCheck = arrIndexOf(interval.daysOfMonth, curDate.getUTCDate());
|
||||
} else if (interval?.dayInterval) {
|
||||
let daySpan = Math.floor((curDate.getTime() - date.getTime()) / 86400000);
|
||||
dayCheck = _checkInterval(interval.dayInterval, 0, daySpan);
|
||||
}
|
||||
|
||||
return monthCheck >= 0 && dayCheck >= 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче