Skip to content

Commit

Permalink
Beta Part 6: Part of Mega Dynamic Load/Unload support (#1782)
Browse files Browse the repository at this point in the history
- Add basic minimal unload / teardown support to all remaining components
  • Loading branch information
MSNev committed Mar 22, 2022
1 parent d047267 commit 2d74434
Show file tree
Hide file tree
Showing 44 changed files with 2,814 additions and 1,825 deletions.
19 changes: 19 additions & 0 deletions AISKU/Tests/Unit/src/AISKUSize.Tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { AITestClass, Assert } from "@microsoft/ai-test-framework";
import { strUndefined } from "@microsoft/applicationinsights-core-js";
import * as pako from "pako";
import { Snippet } from "../../../src/Initialization";

export class AISKUSizeCheck extends AITestClass {
private readonly MAX_DEFLATE_SIZE = 44;
Expand All @@ -9,6 +11,23 @@ export class AISKUSizeCheck extends AITestClass {
public testInitialize() {
}

public testFinishedCleanup(): void {
if (typeof window !== strUndefined) {
var _window = window;
let aiName = _window["appInsightsSDK"] || "appInsights";
if (_window[aiName] !== undefined) {
const snippet: Snippet = _window[aiName] as any;
if (snippet["unload"]) {
snippet["unload"](false);
} else {
if (snippet["appInsightsNew"]) {
snippet["appInsightsNew"].unload();
}
}
}
}
}

public testCleanup() {
}

Expand Down
17 changes: 16 additions & 1 deletion AISKU/Tests/Unit/src/ApplicationInsightsDeprecatedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export class ApplicationInsightsDeprecatedTests extends AITestClass {

constructor() {
super("ApplicationInsightsDeprecatedTests");

// this.assertNoEvents = false;
// this.assertNoHooks = false;
}

public testInitialize() {
try {
this.useFakeServer = false;
Expand All @@ -46,7 +50,18 @@ export class ApplicationInsightsDeprecatedTests extends AITestClass {
}
}

public testCleanup() {
public testFinishedCleanup(): void {
if (this._snippet) {
if (this._snippet["unload"]) {
// force unload
this._snippet["unload"](false);
} else {
const appInsights = (this._aiDeprecated as any).appInsightsNew;
if (appInsights.unload) {
appInsights.unload(false);
}
}
}
}

public registerTests() {
Expand Down
12 changes: 11 additions & 1 deletion AISKU/Tests/Unit/src/SnippetInitialization.Tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ export class SnippetInitializationTests extends AITestClass {
// that this functions exists we can't validate that it is called
} else if (method === "setAuthenticatedUserContext" || method === "clearAuthenticatedUserContext") {
funcSpy = this.sandbox.spy(theSnippet.context.user, method);
} else if (method === "addTelemetryInitializer") {
funcSpy = this.sandbox.spy(theSnippet.core, method);
} else {
funcSpy = this.sandbox.spy(theSnippet.appInsights, method);
}
Expand Down Expand Up @@ -857,10 +859,18 @@ export class SnippetInitializationTests extends AITestClass {
this.useFakeServer = false;

// Call the initialization
((ApplicationInsightsContainer.getAppInsights(snippet, snippet.version)) as IApplicationInsights);
((ApplicationInsightsContainer.getAppInsights(snippet, snippet.version)) as IApplicationInsights);

// Setup Sinon stuff
const appInsights = (snippet as any).appInsights;
this.onDone(() => {
if (snippet["unload"]) {
snippet["unload"](false);
} else if (snippet["appInsightsNew"]) {
snippet["appInsightsNew"].unload(false);
}
});

Assert.ok(appInsights, "The App insights instance should be populated");
Assert.ok(appInsights.core, "The Core exists");
Assert.equal(appInsights.core, (snippet as any).core, "The core instances should match");
Expand Down
8 changes: 8 additions & 0 deletions AISKU/Tests/Unit/src/SnippetLegacyInitialization.Tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,14 @@ export class SnippetLegacyInitializationTests extends AITestClass {
((ApplicationInsightsContainer.getAppInsights(snippet, snippet.version)) as IAppInsightsDeprecated);

const appInsights = (snippet as any).appInsights;
this.onDone(() => {
if (snippet["unload"]) {
snippet["unload"](false);
} else if (snippet["appInsightsNew"]) {
snippet["appInsightsNew"].unload(false);
}
});

Assert.ok(appInsights, "The App insights instance should be populated");
Assert.ok(appInsights.core, "The Core exists");
Assert.equal(appInsights.core, (snippet as any).core, "The core instances should match");
Expand Down
7 changes: 6 additions & 1 deletion AISKU/Tests/Unit/src/applicationinsights.e2e.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ export class ApplicationInsightsTests extends AITestClass {
}
}

public testCleanup() {
public testFinishedCleanup(): void {
if (this._ai && this._ai.unload) {
// force unload
this._ai.unload(false);
}

if (this._ai && this._ai["dependencies"]) {
this._ai["dependencies"].teardown();
}
Expand Down
6 changes: 5 additions & 1 deletion AISKU/Tests/Unit/src/sanitizer.e2e.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ export class SanitizerE2ETests extends AITestClass {
}
}

public testCleanup() {
public testFinishedCleanup(): void {
if (this._ai && this._ai.unload) {
// force unload
this._ai.unload(false);
}
}

public registerTests() {
Expand Down
6 changes: 6 additions & 0 deletions AISKU/Tests/Unit/src/sender.e2e.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ export class SenderE2ETests extends AITestClass {
}
}

public testFinishedCleanup(): void {
if (this._ai && this._ai.unload) {
this._ai.unload(false);
}
}

public testCleanup() {
this.successSpy.restore();
}
Expand Down
6 changes: 5 additions & 1 deletion AISKU/Tests/Unit/src/validate.e2e.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ export class ValidateE2ETests extends AITestClass {
}
}

public testCleanup() {
public testFinishedCleanup(): void {
if (this._ai) {
this._ai.unload(false);
this._ai = null;
}
}

public registerTests() {
Expand Down
2 changes: 1 addition & 1 deletion AISKU/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@rollup/plugin-commonjs": "^18.0.0",
"@rollup/plugin-node-resolve": "^11.2.1",
"@rollup/plugin-replace": "^2.3.3",
"rollup-plugin-cleanup": "3.2.1",
"rollup-plugin-cleanup": "^3.2.1",
"rollup": "^2.32.0",
"selenium-server-standalone-jar": "^3.141.5",
"serve-static": "^1.13.2",
Expand Down
14 changes: 6 additions & 8 deletions AISKU/src/Init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@

import { Snippet } from "./Initialization";
import { ApplicationInsightsContainer } from "./ApplicationInsightsContainer";
import { strUndefined } from "@microsoft/applicationinsights-core-js";

export { Initialization as ApplicationInsights, Telemetry, Snippet } from "./Initialization";
export { Initialization as ApplicationInsights, Snippet } from "./Initialization";

"use strict";

const Undefined = "undefined";
function _logWarn(aiName:string, message:string) {
// TODO: Find better place to warn to console when SDK initialization fails
var _console = typeof console !== Undefined ? console : null;
var _console = typeof console !== strUndefined ? console : null;
if (_console && _console.warn) {
_console.warn("Failed to initialize AppInsights JS SDK for instance " + (aiName || "<unknown>") + " - " + message);
}
Expand All @@ -23,10 +21,10 @@ try {
// E2E sku on load initializes core and pipeline using snippet as input for configuration
// tslint:disable-next-line: no-var-keyword
var aiName;
if (typeof window !== Undefined) {
if (typeof window !== strUndefined) {
var _window = window;
aiName = _window["appInsightsSDK"] || "appInsights";
if (typeof JSON !== Undefined) {
if (typeof JSON !== strUndefined) {
// get snippet or initialize to an empty object

if (_window[aiName] !== undefined) {
Expand All @@ -49,7 +47,7 @@ try {
// else write what was there for v2 SDK prior to rollup bundle output name change.
// e.g Microsoft.ApplicationInsights.ApplicationInsights, Microsoft.ApplicationInsights.Telemetry
// @todo uncomment once integration tests for this can be added
// if (typeof window !== Undefined && window && ((window as any).Microsoft && !(window as any).Microsoft.ApplicationInsights)) {
// if (typeof window !== strUndefined && window && ((window as any).Microsoft && !(window as any).Microsoft.ApplicationInsights)) {
// (window as any).Microsoft = (window as any).Microsoft || {};
// (window as any).Microsoft.ApplicationInsights = {
// ApplicationInsights, Telemetry
Expand Down
Loading

0 comments on commit 2d74434

Please sign in to comment.