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

Prevent infinite loop when updating a table row in place #7154

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 18 additions & 17 deletions src/plugins/telemetryTable/collections/TableRowCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,40 +153,41 @@
});
}

mergeSortedRows(rows) {
mergeSortedRows(incomingRows) {
const mergedRows = [];
let i = 0;
let j = 0;
let existingRowIndex = 0;
let incomingRowIndex = 0;

while (i < this.rows.length && j < rows.length) {
const existingRow = this.rows[i];
const incomingRow = rows[j];
while (existingRowIndex < this.rows.length && incomingRowIndex < incomingRows.length) {
const existingRow = this.rows[existingRowIndex];
const incomingRow = incomingRows[incomingRowIndex];

const index = this.getInPlaceUpdateIndex(incomingRow);
if (index > -1) {
this.updateRowInPlace(incomingRow, index);
const inPlaceIndex = this.getInPlaceUpdateIndex(incomingRow);
if (inPlaceIndex > -1) {
this.updateRowInPlace(incomingRow, inPlaceIndex);
incomingRowIndex++;
} else {
if (this.firstRowInSortOrder(existingRow, incomingRow) === existingRow) {
mergedRows.push(existingRow);
i++;
existingRowIndex++;

Check warning on line 172 in src/plugins/telemetryTable/collections/TableRowCollection.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/telemetryTable/collections/TableRowCollection.js#L172

Added line #L172 was not covered by tests
} else {
mergedRows.push(incomingRow);
j++;
incomingRowIndex++;

Check warning on line 175 in src/plugins/telemetryTable/collections/TableRowCollection.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/telemetryTable/collections/TableRowCollection.js#L175

Added line #L175 was not covered by tests
}
}
}

// tail of existing rows is all that is left to merge
if (i < this.rows.length) {
for (i; i < this.rows.length; i++) {
mergedRows.push(this.rows[i]);
if (existingRowIndex < this.rows.length) {
for (existingRowIndex; existingRowIndex < this.rows.length; existingRowIndex++) {
mergedRows.push(this.rows[existingRowIndex]);
}
}

// tail of incoming rows is all that is left to merge
if (j < rows.length) {
for (j; j < rows.length; j++) {
mergedRows.push(rows[j]);
if (incomingRowIndex < incomingRows.length) {
for (incomingRowIndex; incomingRowIndex < incomingRows.length; incomingRowIndex++) {
mergedRows.push(incomingRows[incomingRowIndex]);

Check warning on line 190 in src/plugins/telemetryTable/collections/TableRowCollection.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/telemetryTable/collections/TableRowCollection.js#L189-L190

Added lines #L189 - L190 were not covered by tests
}
}

Expand Down
44 changes: 38 additions & 6 deletions src/plugins/telemetryTable/pluginSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('the plugin', () => {
let tablePlugin;
let element;
let child;
let historicalProvider;
let historicalTelemetryProvider;
let originalRouterPath;
let unlistenConfigMutation;

Expand All @@ -61,12 +61,12 @@ describe('the plugin', () => {
tablePlugin = new TablePlugin();
openmct.install(tablePlugin);

historicalProvider = {
historicalTelemetryProvider = {
request: () => {
return Promise.resolve([]);
}
};
spyOn(openmct.telemetry, 'findRequestProvider').and.returnValue(historicalProvider);
spyOn(openmct.telemetry, 'findRequestProvider').and.returnValue(historicalTelemetryProvider);

element = document.createElement('div');
child = document.createElement('div');
Expand Down Expand Up @@ -137,11 +137,12 @@ describe('the plugin', () => {
let tableView;
let tableInstance;
let mockClock;
let telemetryCallback;

beforeEach(async () => {
openmct.time.timeSystem('utc', {
start: 0,
end: 4
end: 10
});

mockClock = jasmine.createSpyObj('clock', ['on', 'off', 'currentValue']);
Expand All @@ -151,7 +152,7 @@ describe('the plugin', () => {
openmct.time.addClock(mockClock);
openmct.time.clock('mockClock', {
start: 0,
end: 4
end: 10
});

testTelemetryObject = {
Expand Down Expand Up @@ -214,7 +215,21 @@ describe('the plugin', () => {
}
];

historicalProvider.request = () => Promise.resolve(testTelemetry);
historicalTelemetryProvider.request = () => {
return Promise.resolve(testTelemetry);
};

const realtimeTelemetryProvider = {
supportsSubscribe: () => true,
subscribe: (domainObject, passedCallback) => {
telemetryCallback = passedCallback;
return Promise.resolve(() => {});
}
};

spyOn(openmct.telemetry, 'findSubscriptionProvider').and.returnValue(
realtimeTelemetryProvider
);

openmct.router.path = [testTelemetryObject];

Expand Down Expand Up @@ -256,6 +271,23 @@ describe('the plugin', () => {
expect(rows.length).toBe(3);
});

it('Adds a row in place when updating with existing telemetry', async () => {
let rows = element.querySelectorAll('table.c-telemetry-table__body tr');
await nextTick();
expect(rows.length).toBe(3);
// fire some telemetry
const newTelemetry = {
utc: 2,
'some-key': 'some-value 2',
'some-other-key': 'spacecraft'
};
spyOn(tableInstance.tableRows, 'getInPlaceUpdateIndex').and.returnValue(1);
spyOn(tableInstance.tableRows, 'updateRowInPlace').and.callThrough();
telemetryCallback(newTelemetry);

expect(tableInstance.tableRows.updateRowInPlace.calls.count()).toBeGreaterThan(0);
});

it('Renders a column for every item in telemetry metadata', () => {
let headers = element.querySelectorAll('span.c-telemetry-table__headers__label');
expect(headers.length).toBe(4);
Expand Down