Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Task]14569737: Add support to throttle the deprecation message #1882

Merged
merged 21 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 39 additions & 42 deletions shared/AppInsightsCommon/Tests/Unit/src/ThrottleMgr.tests.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { Assert, AITestClass } from "@microsoft/ai-test-framework";
import { AppInsightsCore, IAppInsightsCore, _eInternalMessageId } from "@microsoft/applicationinsights-core-js";
import { IThrottleMsgKey } from "../../../src/Enums";
import { IThrottleInterval, IThrottleLimit, IthrottleMgrConfig, IthrottleResult } from "../../../src/Interfaces/IThrottleMgr";
import { IThrottleInterval, IThrottleLimit, IThrottleMgrConfig, IThrottleResult } from "../../../src/Interfaces/IThrottleMgr";
import { ThrottleMgr} from "../../../src/ThrottleMgr";
import { SinonSpy } from "sinon";
import { Util } from "../../../src/Util";

const compareDates = (date1: Date, date: string | Date) => {
let isSame = false;
try {
if (date1 && date) {
let date2 = typeof date == "string"? new Date(date) : date;
return date1.getUTCFullYear() === date2.getUTCFullYear() &&
isSame = date1.getUTCFullYear() === date2.getUTCFullYear() &&
date1.getUTCMonth() === date2.getUTCMonth() &&
date1.getUTCDate() === date2.getUTCDate();
}
} catch (e) {
console.log("compare dates error" + e);
Assert.ok(false,"compare dates error" + e);
Karlie-777 marked this conversation as resolved.
Show resolved Hide resolved
}
return false;
Assert.ok(isSame, "two dates are not same");
return isSame;
}

export class ThrottleMgrTest extends AITestClass {
Expand Down Expand Up @@ -54,15 +56,15 @@ export class ThrottleMgrTest extends AITestClass {
msgKey: this._msgKey,
disabled: false,
limit: {
samplingPercentage: 50,
samplingRate: 50,
maxSendNumber: 100
} as IThrottleLimit,
interval: {
monthInterval: 2,
dayInterval: 10,
maxTimesPerMonth: 1
} as IThrottleInterval
} as IthrottleMgrConfig;
} as IThrottleMgrConfig;

let throttleMgr = new ThrottleMgr(config, this._core);
let actualConfig = throttleMgr.getConfig();
Expand All @@ -78,21 +80,21 @@ export class ThrottleMgrTest extends AITestClass {
test: () => {
let config = {
msgKey: this._msgKey
} as IthrottleMgrConfig;
} as IThrottleMgrConfig;

let expectedConfig = {
msgKey: this._msgKey,
disabled: false,
limit: {
samplingPercentage: 100,
samplingRate: 100,
maxSendNumber:1
} as IThrottleLimit,
interval: {
monthInterval: 3,
dayInterval: 28,
maxTimesPerMonth: 1
} as IThrottleInterval
} as IthrottleMgrConfig;
} as IThrottleMgrConfig;

let throttleMgr = new ThrottleMgr(config, this._core);
let actualConfig = throttleMgr.getConfig();
Expand All @@ -109,7 +111,7 @@ export class ThrottleMgrTest extends AITestClass {
let config = {
msgKey: this._msgKey,
disabled: true
} as IthrottleMgrConfig;
} as IThrottleMgrConfig;
let throttleMgr = new ThrottleMgr(config, this._core);
let canThrottle = throttleMgr.canThrottle();
Assert.equal(canThrottle, false);
Expand Down Expand Up @@ -141,15 +143,15 @@ export class ThrottleMgrTest extends AITestClass {
msgKey: this._msgKey,
disabled: false,
limit: {
samplingPercentage: 1000,
samplingRate: 1000000,
maxSendNumber: 100
} as IThrottleLimit,
interval: {
monthInterval: 3,
dayInterval: 1,
maxTimesPerMonth: 100
} as IThrottleInterval
} as IthrottleMgrConfig;
} as IThrottleMgrConfig;

let throttleMgr = new ThrottleMgr(config, this._core);
let canThrottle = throttleMgr.canThrottle();
Expand Down Expand Up @@ -185,15 +187,15 @@ export class ThrottleMgrTest extends AITestClass {
msgKey: this._msgKey,
disabled: false,
limit: {
samplingPercentage: 1000,
samplingRate: 1000000,
maxSendNumber: 100
} as IThrottleLimit,
interval: {
monthInterval: 3,
dayInterval: 1,
maxTimesPerMonth: 100
} as IThrottleInterval
} as IthrottleMgrConfig;
} as IThrottleMgrConfig;

let throttleMgr = new ThrottleMgr(config, this._core);
let canThrottle = throttleMgr.canThrottle();
Expand All @@ -219,15 +221,15 @@ export class ThrottleMgrTest extends AITestClass {
msgKey: this._msgKey,
disabled: false,
limit: {
samplingPercentage: 1000,
samplingRate: 1000000,
maxSendNumber: 100
} as IThrottleLimit,
interval: {
monthInterval: 1,
dayInterval: 31,
maxTimesPerMonth: 100
} as IThrottleInterval
} as IthrottleMgrConfig;
} as IThrottleMgrConfig;

let throttleMgr = new ThrottleMgr(config, this._core);
let canThrottle = throttleMgr.canThrottle();
Expand All @@ -253,15 +255,15 @@ export class ThrottleMgrTest extends AITestClass {
msgKey: this._msgKey,
disabled: false,
limit: {
samplingPercentage: 1000,
samplingRate: 1000000,
maxSendNumber: 100
} as IThrottleLimit,
interval: {
monthInterval: 1,
dayInterval: 1,
maxTimesPerMonth: 100
} as IThrottleInterval
} as IthrottleMgrConfig;
} as IThrottleMgrConfig;

let throttleMgr = new ThrottleMgr(config, this._core);
let canThrottle = throttleMgr.canThrottle();
Expand Down Expand Up @@ -289,15 +291,15 @@ export class ThrottleMgrTest extends AITestClass {
msgKey: this._msgKey,
disabled: false,
limit: {
samplingPercentage: 1000,
samplingRate: 1000000,
maxSendNumber: 100
} as IThrottleLimit,
interval: {
monthInterval: 4,
dayInterval: 1,
maxTimesPerMonth: 100
} as IThrottleInterval
} as IthrottleMgrConfig;
} as IThrottleMgrConfig;

let throttleMgr = new ThrottleMgr(config, this._core);
let canThrottle = throttleMgr.canThrottle();
Expand All @@ -324,15 +326,15 @@ export class ThrottleMgrTest extends AITestClass {
msgKey: this._msgKey,
disabled: false,
limit: {
samplingPercentage: 1000,
samplingRate: 1000000,
maxSendNumber: 100
} as IThrottleLimit,
interval: {
monthInterval: 1,
dayInterval: 1,
maxTimesPerMonth: maxTimes
} as IThrottleInterval
} as IthrottleMgrConfig;
} as IThrottleMgrConfig;

let throttleMgr = new ThrottleMgr(config, this._core);
let canThrottle = throttleMgr.canThrottle();
Expand All @@ -357,15 +359,15 @@ export class ThrottleMgrTest extends AITestClass {
msgKey: this._msgKey,
disabled: false,
limit: {
samplingPercentage: 1000,
samplingRate: 1000000,
maxSendNumber: 100
} as IThrottleLimit,
interval: {
monthInterval: 1,
dayInterval: 1,
maxTimesPerMonth: 33
} as IThrottleInterval
} as IthrottleMgrConfig;
} as IThrottleMgrConfig;

let throttleMgr = new ThrottleMgr(config, this._core);
let canThrottle = throttleMgr.canThrottle();
Expand Down Expand Up @@ -404,15 +406,15 @@ export class ThrottleMgrTest extends AITestClass {
msgKey: this._msgKey,
disabled: false,
limit: {
samplingPercentage: 1000,
samplingRate: 1000000,
maxSendNumber: 1
} as IThrottleLimit,
interval: {
monthInterval: 1,
dayInterval: date.getUTCDate(),
maxTimesPerMonth: 1
} as IThrottleInterval
} as IthrottleMgrConfig;
} as IThrottleMgrConfig;

let throttleMgr = new ThrottleMgr(config, this._core);
let canThrottle = throttleMgr.canThrottle();
Expand All @@ -426,7 +428,7 @@ export class ThrottleMgrTest extends AITestClass {
let expectedRetryRlt = {
isThrottled: true,
throttleNum: 1
} as IthrottleResult
} as IThrottleResult
Assert.deepEqual(result, expectedRetryRlt);
let isPostTriggered = throttleMgr.isTriggered();
Assert.equal(isPostTriggered, true);
Expand All @@ -447,24 +449,19 @@ export class ThrottleMgrTest extends AITestClass {
test: () => {
let msg = "Instrumentation key support will end soon, see aka.ms/IkeyMigrate";
let date = new Date();
let testStorageObj = {
date: date,
count: 0
};

let config = {
msgKey: IThrottleMsgKey.ikeyDeprecate,
disabled: false,
limit: {
samplingPercentage: 1000,
samplingRate: 1000000,
maxSendNumber:1
} as IThrottleLimit,
interval: {
monthInterval: 1,
dayInterval: 1,
maxTimesPerMonth: 100
} as IThrottleInterval
} as IthrottleMgrConfig;
} as IThrottleMgrConfig;

let throttleMgr = new ThrottleMgr(config, this._core);

Expand Down Expand Up @@ -510,7 +507,7 @@ export class ThrottleMgrTest extends AITestClass {
let expectedRlt = {
isThrottled: false,
throttleNum: 0
} as IthrottleResult
} as IThrottleResult
Assert.equal(onReadyCount,1);
Assert.deepEqual(onReadyResult, expectedRlt);
let onReadyIsTriggered = throttleMgr.isTriggered();
Expand Down Expand Up @@ -542,15 +539,15 @@ export class ThrottleMgrTest extends AITestClass {
msgKey: IThrottleMsgKey.ikeyDeprecate,
disabled: false,
limit: {
samplingPercentage: 1000,
samplingRate: 1000000,
maxSendNumber:1
} as IThrottleLimit,
interval: {
monthInterval: 1,
dayInterval: 1,
maxTimesPerMonth: 100
} as IThrottleInterval
} as IthrottleMgrConfig;
} as IThrottleMgrConfig;

let throttleMgr = new ThrottleMgr(config, this._core);

Expand All @@ -568,7 +565,7 @@ export class ThrottleMgrTest extends AITestClass {
let expectedRlt = {
isThrottled: true,
throttleNum: 1
} as IthrottleResult
} as IThrottleResult
Assert.deepEqual(result, expectedRlt);

let val = window.localStorage[this._storageName];
Expand All @@ -593,15 +590,15 @@ export class ThrottleMgrTest extends AITestClass {
msgKey: IThrottleMsgKey.ikeyDeprecate,
disabled: false,
limit: {
samplingPercentage: 1000,
samplingRate: 1000000,
maxSendNumber:1
} as IThrottleLimit,
interval: {
monthInterval: 1,
dayInterval: 1,
maxTimesPerMonth: 100
} as IThrottleInterval
} as IthrottleMgrConfig;
} as IThrottleMgrConfig;

let throttleMgr = new ThrottleMgr(config, this._core);

Expand All @@ -620,7 +617,7 @@ export class ThrottleMgrTest extends AITestClass {
let expectedRlt = {
isThrottled: true,
throttleNum: 1
} as IthrottleResult
} as IThrottleResult
Assert.deepEqual(result, expectedRlt);

let isTriggeredPost = throttleMgr.isTriggered();
Expand All @@ -634,7 +631,7 @@ export class ThrottleMgrTest extends AITestClass {
let expectedRetryRlt = {
isThrottled: false,
throttleNum: 0
} as IthrottleResult
} as IThrottleResult
Assert.deepEqual(retryRlt, expectedRetryRlt);
}
});
Expand Down
12 changes: 6 additions & 6 deletions shared/AppInsightsCommon/src/Interfaces/IThrottleMgr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { IThrottleMsgKey } from "../Enums";
export interface IThrottleLimit {
/**
* Identifies sampling percentage of items per time
* In /1000, for example: 200 means 0.2
* Default: 100
* The percentage is set to 4 decimal places, for example: 1 means 0.0001%
* Default: 100 (0.01%)
*/
samplingPercentage?: number;
samplingRate?: number;

/**
* Identifies limit number of items per time
Expand Down Expand Up @@ -47,7 +47,7 @@ export interface IThrottleInterval {
/**
* Identifies basic config
*/
export interface IthrottleMgrConfig {
export interface IThrottleMgrConfig {
/**
* Identifies message key to be used for local storage key
*/
Expand All @@ -74,7 +74,7 @@ export interface IthrottleMgrConfig {
/**
* Identifies object for local storage
*/
export interface IthrottleLocalStorageObj {
export interface IThrottleLocalStorageObj {
/**
* Identifies start date
*/
Expand All @@ -95,7 +95,7 @@ export interface IthrottleLocalStorageObj {
/**
* Identifies throttle result
*/
export interface IthrottleResult {
export interface IThrottleResult {
/**
* Identifies if items are sent
*/
Expand Down
Loading