Skip to content

Commit

Permalink
[Main][BUG] Don't attempt to send message batch when known to be offl…
Browse files Browse the repository at this point in the history
…ine #2034 (#2036)
  • Loading branch information
peitschie authored and MSNev committed Apr 7, 2023
1 parent 5a71f82 commit 290aed9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class SenderTests extends AITestClass {

if (this._sender && this._sender.isInitialized()) {
this._sender.pause();
this._sender._buffer.clear();
this._sender.teardown();
}
this._sender = null;
Expand Down Expand Up @@ -2152,6 +2153,54 @@ export class SenderTests extends AITestClass {
}
});

this.testCase({
name: "Channel Config: Process telemetry when offline and exceeding the batch size limits",
useFakeTimers: true,
test: () => {
const maxBatchSizeInBytes = 1024;
let core = new AppInsightsCore();

this._sender.initialize(
{
instrumentationKey: 'abc',
maxBatchInterval: 123,
maxBatchSizeInBytes: maxBatchSizeInBytes,
endpointUrl: 'https://example.com',
extensionConfig: {
}

}, core, []
);

const triggerSendSpy = this.sandbox.spy(this._sender, "triggerSend");
const telemetryItem: ITelemetryItem = {
name: 'fake item with some really long name to take up space quickly',
iKey: 'iKey',
baseType: 'some type',
baseData: {}
};

// Act - Go offline
const offlineEvent = new Event('offline');
window.dispatchEvent(offlineEvent);

// Keep sending events until the max payload size is exceeded
while (!triggerSendSpy.called && this._sender._buffer.size() < maxBatchSizeInBytes) {
try {
this._sender.processTelemetry(telemetryItem, null);
} catch(e) {
QUnit.assert.ok(false);
}
}

QUnit.assert.equal(false, triggerSendSpy.called);

this.clock.tick(1);

QUnit.assert.equal(false, triggerSendSpy.called);
}
});

this.testCase({
name: 'Envelope: operation.name is correctly truncated if required',
test: () => {
Expand Down
5 changes: 4 additions & 1 deletion channels/applicationinsights-channel-js/src/Sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,10 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControls {
function _checkMaxSize(incomingPayload?: string): boolean {
let incomingSize = incomingPayload? incomingPayload.length : 0;
if ((_self._buffer.size() + incomingSize) > _maxBatchSizeInBytes) {
_self.triggerSend(true, null, SendRequestReason.MaxBatchSize);
if (!_offlineListener || _offlineListener.isOnline()) { // only trigger send when currently online
_self.triggerSend(true, null, SendRequestReason.MaxBatchSize);
}

return true;
}
return false;
Expand Down

0 comments on commit 290aed9

Please sign in to comment.