Skip to content

Commit

Permalink
[BUG] Don't attempt to send message batch when known to be offline mi…
Browse files Browse the repository at this point in the history
  • Loading branch information
peitschie committed Apr 6, 2023
1 parent 4361915 commit 13b6900
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class SenderTests extends AITestClass {

if (this._sender && this._sender.isInitialized()) {
this._sender.pause();
this._sender._buffer.clear();
this._sender.teardown();
}

Expand Down Expand Up @@ -1576,6 +1577,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
4 changes: 3 additions & 1 deletion channels/applicationinsights-channel-js/src/Sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControlsAI {
const bufferSize = buffer.size();

if ((bufferSize + payload.length) > _self._senderConfig.maxBatchSizeInBytes()) {
_self.triggerSend(true, null, SendRequestReason.MaxBatchSize);
if (!_offlineListener || _offlineListener.isOnline()) { // only trigger send when currently online
_self.triggerSend(true, null, SendRequestReason.MaxBatchSize);
}
}

// enqueue the payload
Expand Down

0 comments on commit 13b6900

Please sign in to comment.