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

[BUG] Don't attempt to send message batch when known to be offline #2034 #2036

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realised while writing my test that session storage wasn't being cleared between tests 😅

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