Skip to content

Commit

Permalink
[Main][Task]29465842: Update Promise Initialization Post Channel Conf…
Browse files Browse the repository at this point in the history
…ig (#2418)

* udpate 1ds post init promise tests

* update
  • Loading branch information
Karlie-777 authored Sep 20, 2024
1 parent 5515866 commit a3a2cd4
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 65 deletions.
2 changes: 1 addition & 1 deletion channels/1ds-post-js/src/HttpManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ export class HttpManager {
}

_self["_getDbgPlgTargets"] = () => {
return [_sendInterfaces[EventSendType.Batched], _killSwitch, _serializer, _sendInterfaces, _getSendPostMgrConfig()];
return [_sendInterfaces[EventSendType.Batched], _killSwitch, _serializer, _sendInterfaces, _getSendPostMgrConfig(), _urlString];
};

function _getSendPostMgrConfig(): _ISendPostMgrConfig {
Expand Down
15 changes: 11 additions & 4 deletions channels/1ds-post-js/src/PostChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
setProcessTelemetryTimings
} from "@microsoft/1ds-core-js";
import { IPromise, createPromise } from "@nevware21/ts-async";
import { ITimerHandler, objDeepFreeze } from "@nevware21/ts-utils";
import { ITimerHandler, isPromiseLike, objDeepFreeze } from "@nevware21/ts-utils";
import {
BE_PROFILE, EventBatchNotificationReason, IChannelConfiguration, IPostChannel, IPostTransmissionTelemetryItem, NRT_PROFILE, RT_PROFILE
} from "./DataModels";
Expand Down Expand Up @@ -180,8 +180,15 @@ export class PostChannel extends BaseTelemetryPlugin implements IChannelControls
_maxUnloadEventSendAttempts = _postConfig.maxUnloadEventRetryAttempts;
_disableAutoBatchFlushLimit = _postConfig.disableAutoBatchFlushLimit;

_setAutoLimits();
if (isPromiseLike(coreConfig.endpointUrl)) {
_self.pause();
} else if (!!_paused) {
// if previous url is promise, resume
_self.resume();
}

_setAutoLimits();

// Override iKey if provided in Post config if provided for during initialization
_overrideInstrumentationKey = _postConfig.overrideInstrumentationKey;

Expand Down Expand Up @@ -468,12 +475,12 @@ export class PostChannel extends BaseTelemetryPlugin implements IChannelControls
_self.pause = () => {
_clearScheduledTimer();
_paused = true;
_httpManager.pause();
_httpManager && _httpManager.pause();
};

_self.resume = () => {
_paused = false;
_httpManager.resume();
_httpManager && _httpManager.resume();
_scheduleTimer();
};

Expand Down
70 changes: 69 additions & 1 deletion channels/1ds-post-js/test/Unit/src/PostChannelTest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { AITestClass, TestHelper } from "@microsoft/ai-test-framework";
import { AITestClass, PollingAssert, TestHelper } from "@microsoft/ai-test-framework";
import { IExtendedConfiguration, AppInsightsCore, EventLatency, ITelemetryItem, IExtendedTelemetryItem, SendRequestReason, EventSendType, isFetchSupported, objKeys, arrForEach, isBeaconsSupported, EventPersistence, isNullOrUndefined } from '@microsoft/1ds-core-js';
import { PostChannel, IXHROverride, IPayloadData } from '../../../src/Index';
import { IPostTransmissionTelemetryItem, IChannelConfiguration } from '../../../src/DataModels';
import { SinonSpy } from 'sinon';
import { createAsyncResolvedPromise } from "@nevware21/ts-async";
import { ActiveStatus } from "@microsoft/1ds-core-js";


interface IEventsSendRequests {
sendReason: number;
Expand Down Expand Up @@ -32,6 +35,7 @@ export class PostChannelTest extends AITestClass {
private eventsSendRequests: Array<IEventsSendRequests> = [];
private testMessage: string;
private beaconCalls = [];
private ctx: any;

constructor(name?: string, emulateEs3?: boolean) {
super(name, emulateEs3);
Expand Down Expand Up @@ -65,6 +69,7 @@ export class PostChannelTest extends AITestClass {
this.testMessage = "testClearTimeout";
clearTimeout(params);
};
this.ctx = {};
}

public testFinishedCleanup(): void {
Expand All @@ -76,6 +81,7 @@ export class PostChannelTest extends AITestClass {
if (this.core && this.core.isInitialized()) {
this.core.unload(false);
}
this.ctx = null;
}

public registerTests() {
Expand Down Expand Up @@ -309,6 +315,68 @@ export class PostChannelTest extends AITestClass {
}
});

this.testCaseAsync({
name: "Init: init with ikey Promise and endpointUrl Promise",
stepDelay: 100,
useFakeTimers: true,
steps: [() => {

let config = this.config;
config.initTimeOut = 80000;
let ikeyPromise = createAsyncResolvedPromise("testIkey-test");
let urlPromise = createAsyncResolvedPromise("https://testEndpoint");
this.ctx.ikeyPromise = ikeyPromise;
this.ctx.urlPromise = urlPromise;
config.instrumentationKey = ikeyPromise;
config.endpointUrl = urlPromise;
let core = this.core;
let postChannel = this.postChannel;
let identifier = postChannel.identifier;
let spy = this.sandbox.spy(this.xhrOverride, "sendPOST");
config.extensionConfig = {[identifier]: {}};
config.extensionConfig[identifier].httpXHROverride = this.xhrOverride;
this.ctx.spy = spy;

core.initialize(config, [postChannel]);
let status = core.activeStatus && core.activeStatus();
QUnit.assert.equal(status, ActiveStatus.PENDING, "status should be set to pending");

let event: IPostTransmissionTelemetryItem = {
name: "testEvent",
iKey: "testIkey-test",
latency: EventLatency.RealTime
};
postChannel.processTelemetry(event);

}].concat(PollingAssert.createPollingAssert(() => {
let core = this.core;
let activeStatus = core.activeStatus && core.activeStatus();
let ikeyPromise = this.ctx.ikeyPromise;
let urlPromise = this.ctx.urlPromise;
let config = this.core.config;
let spy = this.ctx.spy;


if (ikeyPromise.state === "resolved" && urlPromise.state === "resolved" && activeStatus === ActiveStatus.ACTIVE) {
QUnit.assert.equal("testIkey-test", core.config.instrumentationKey, "ikey should be set");
QUnit.assert.equal("https://testEndpoint", core.config.endpointUrl ,"endpoint shoule be set");

let httpManager = this.postChannel["_getDbgPlgTargets"]()[0];
QUnit.assert.ok(httpManager ,"http Manager exists");
let url = httpManager["_getDbgPlgTargets"]()[5];
QUnit.assert.ok(url.indexOf("https://testEndpoint") > -1 ,"http manager endpoint shoule be set");

this.postChannel.flush(false);
QUnit.assert.equal(spy.callCount, 1, "sendPOST count should be 1");
QUnit.assert.equal(spy.getCall(0).args[0].data, "{\"name\":\"testEvent\",\"iKey\":\"o:testIkey\",\"data\":{\"baseData\":{}}}", "data should be set");

return true;
}
return false;
}, "Wait for promise response" + new Date().toISOString(), 60, 1000) as any)
});


this.testCase({
name: "Post Channel: Offline Support",
useFakeTimers: true,
Expand Down
116 changes: 58 additions & 58 deletions common/config/rush/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion shared/1ds-core-js/src/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export {
IConfiguration, ITelemetryItem, ITelemetryPlugin, BaseTelemetryPlugin, IProcessTelemetryContext, ProcessTelemetryContext, ITelemetryPluginChain,
MinChannelPriorty, EventsDiscardedReason, IDiagnosticLogger, DiagnosticLogger, LoggingSeverity, SendRequestReason,
IPerfEvent, IPerfManager, IPerfManagerProvider, PerfEvent, PerfManager, doPerf, ICustomProperties, Tags,
AppInsightsCore as InternalAppInsightsCore, _InternalLogMessage, _InternalMessageId,
AppInsightsCore as InternalAppInsightsCore, _InternalLogMessage, _InternalMessageId, eActiveStatus, ActiveStatus,
createEnumStyle, eLoggingSeverity, _eInternalMessageId, _throwInternal, _warnToConsole, _logInternalMessage,
// The HelperFuncs functions
isTypeof, isUndefined, isNullOrUndefined, hasOwnProperty, isObject, isFunction, attachEvent, detachEvent, normalizeJsName,
Expand Down

0 comments on commit a3a2cd4

Please sign in to comment.