Skip to content

Commit 2de7d26

Browse files
ca-dJakobVogelsang
andauthored
fix(validate-schema): cache validator workers (openscd#901)
* fix(validate-schema): cache validator workers Due to a simple oversight we are currently not actually caching schema validator workers at all, meaning we instantiate a new worker on each validation, leaking memory and wasting time. This fixes that issue. * refactor(ValidateSchema): remove unused error throw * test(validators/Validateschema): adopt tests Co-authored-by: Jakob Vogelsang <[email protected]>
1 parent aa519f3 commit 2de7d26

File tree

3 files changed

+76
-91
lines changed

3 files changed

+76
-91
lines changed

src/validators/ValidateSchema.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export default class ValidateSchema extends LitElement {
4444
});
4545
}
4646

47+
validators[xsdName] = validate;
48+
4749
return new Promise((resolve, reject) => {
4850
worker.addEventListener('message', (e: MessageEvent<WorkerMessage>) => {
4951
if (isLoadSchemaResult(e.data)) {
@@ -108,7 +110,7 @@ export default class ValidateSchema extends LitElement {
108110
title: get('validator.schema.invalid', { name: result.file }),
109111
})
110112
);
111-
throw new Error(get('validator.schema.invalid', { name: result.file }));
113+
return;
112114
}
113115

114116
this.dispatchEvent(

test/integration/validators/ValidateSchema.test.ts

Lines changed: 48 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,6 @@ import { MockEditorLogger } from '../../mock-editor-logger.js';
66
import ValidateSchema from '../../../src/validators/ValidateSchema.js';
77
import { IssueDetail, LogEntry } from '../../../src/foundation.js';
88

9-
import { officialPlugins } from '../../../public/js/plugins.js';
10-
11-
const plugins = officialPlugins
12-
.map(plugin => ({
13-
...plugin,
14-
default: false,
15-
installed: false,
16-
official: true,
17-
}))
18-
.concat([
19-
{
20-
name: 'Substation',
21-
src: '/src/editors/Substation.ts',
22-
icon: 'margin',
23-
default: true,
24-
kind: 'editor',
25-
installed: true,
26-
official: false,
27-
},
28-
]);
29-
309
describe('ValidateSchema plugin', () => {
3110
if (customElements.get('') === undefined)
3211
customElements.define('validate-schema', ValidateSchema);
@@ -37,89 +16,85 @@ describe('ValidateSchema plugin', () => {
3716
let valid2007B4: XMLDocument;
3817
let invalid2007B: XMLDocument;
3918

19+
before(async () => {
20+
parent = await fixture(html`
21+
<mock-editor-logger
22+
><validate-schema></validate-schema
23+
></mock-editor-logger>
24+
`);
25+
26+
element = <ValidateSchema>parent.querySelector('validate-schema')!;
27+
element.pluginId = '/src/validators/ValidateSchema.js';
28+
await element.updateComplete;
29+
});
30+
4031
describe('for valid SCL files', () => {
41-
beforeEach(async () => {
32+
before(async () => {
4233
valid2007B4 = await fetch('/test/testfiles/valid2007B.scd')
4334
.then(response => response.text())
4435
.then(str => new DOMParser().parseFromString(str, 'application/xml'));
4536

46-
localStorage.setItem('plugins', JSON.stringify(plugins));
47-
48-
parent = await fixture(html`
49-
<mock-editor-logger
50-
><validate-schema
51-
.doc=${valid2007B4}
52-
.docName=${'valid2007B4'}
53-
></validate-schema
54-
></mock-editor-logger>
55-
`);
56-
element = <ValidateSchema>parent.querySelector('validate-schema')!;
57-
element.pluginId = '/src/validators/ValidateSchema.js';
58-
await element.requestUpdate();
37+
element.doc = valid2007B4;
38+
element.docName = 'valid2007B';
5939
});
6040

61-
it('zeroissues indication looks like the latest snapshot', async () => {
62-
await parent.requestUpdate();
63-
await expect(parent.diagnosticUI).to.equalSnapshot();
64-
});
41+
beforeEach(async () => {
42+
parent.diagnoses.clear();
43+
await parent.updateComplete;
6544

66-
it('indicates successful schema validation in the diagnoses pane', async () => {
6745
await element.validate();
46+
await parent.updateComplete;
47+
});
6848

49+
it('zeroissues indication looks like the latest snapshot', async () =>
50+
await expect(parent.diagnosticUI).to.equalSnapshot());
51+
52+
it('indicates successful schema validation in the diagnoses pane', async () => {
6953
const lastEntry = <IssueDetail[]>(
7054
parent.diagnoses.get('/src/validators/ValidateSchema.js')
7155
);
7256
expect(lastEntry.length).to.equal(1);
7357
expect(lastEntry[0].title).to.contain('[validator.schema.valid]');
74-
}).timeout(15000);
58+
});
7559

7660
it('indicates successful schema validation in the log', async () => {
77-
await element.validate();
7861
const lastEntry = <LogEntry>parent.history.pop();
7962
expect(lastEntry.kind).to.equal('info');
8063
expect(lastEntry.title).to.contain('[validator.schema.valid]');
81-
}).timeout(15000);
64+
});
8265
});
8366

8467
describe('for invalid SCL files', () => {
85-
beforeEach(async () => {
68+
before(async () => {
8669
invalid2007B = await fetch('/test/testfiles/invalid2007B.scd')
8770
.then(response => response.text())
8871
.then(str => new DOMParser().parseFromString(str, 'application/xml'));
8972

90-
parent = await fixture(html`
91-
<mock-editor-logger
92-
><validate-schema
93-
.doc=${invalid2007B}
94-
.docName=${'invalid2007B'}
95-
></validate-schema
96-
></mock-editor-logger>
97-
`);
98-
99-
element = <ValidateSchema>parent.querySelector('validate-schema')!;
100-
element.pluginId = '/src/validators/ValidateSchema.js';
101-
await element.requestUpdate();
73+
element.doc = invalid2007B;
74+
element.docName = 'invalid2007B';
10275

103-
try {
104-
await element.validate();
105-
} catch (e) {
106-
e;
107-
}
76+
await element.requestUpdate();
10877
});
109-
it('create issues in diagnose', async () => {
110-
const issues = parent.diagnoses.get('/src/validators/ValidateSchema.js');
111-
expect(issues).to.not.be.undefined;
112-
}).timeout(15000);
113-
114-
it('pushes issues to the diagnostics pane that look like the latest snapshot', async () => {
115-
await parent.requestUpdate();
116-
await expect(parent.diagnosticUI).to.equalSnapshot();
78+
79+
beforeEach(async () => {
80+
parent.diagnoses.clear();
81+
await parent.updateComplete;
82+
83+
await element.validate();
84+
await parent.updateComplete;
11785
});
11886

87+
it('pushes issues to the diagnostics pane that look like the latest snapshot', async () =>
88+
await expect(parent.diagnosticUI).to.equalSnapshot());
89+
90+
it('create issues in diagnose', async () =>
91+
expect(parent.diagnoses.get('/src/validators/ValidateSchema.js')).to.not
92+
.be.undefined);
93+
11994
it('generates error messages in the log', async () => {
120-
const lastEntry = <LogEntry>parent.history.pop();
121-
expect(lastEntry.kind).to.equal('warning');
122-
expect(lastEntry.title).to.contain('[validator.schema.invalid]');
123-
}).timeout(5000);
95+
const lastLogEntry = <LogEntry>parent.history.pop();
96+
expect(lastLogEntry.kind).to.equal('warning');
97+
expect(lastLogEntry.title).to.contain('[validator.schema.invalid]');
98+
});
12499
});
125100
});

test/integration/validators/__snapshots__/ValidateSchema.test.snap.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,32 @@ snapshots["ValidateSchema plugin for valid SCL files zeroissues indication looks
1111
wrapfocus=""
1212
>
1313
<mwc-list-item
14-
aria-disabled="true"
15-
disabled=""
16-
graphic="icon"
17-
mwc-list-item=""
18-
tabindex="0"
14+
aria-disabled="false"
15+
noninteractive=""
16+
tabindex="-1"
1917
>
20-
<span>
21-
[diag.placeholder]
22-
</span>
23-
<mwc-icon slot="graphic">
24-
info
25-
</mwc-icon>
18+
Validate Schema
2619
</mwc-list-item>
20+
<li
21+
divider=""
22+
padded=""
23+
role="separator"
24+
>
25+
</li>
26+
<abbr title="[validator.schema.valid]
27+
undefined">
28+
<mwc-list-item
29+
aria-disabled="false"
30+
mwc-list-item=""
31+
tabindex="-1"
32+
>
33+
<span>
34+
[validator.schema.valid]
35+
</span>
36+
<span slot="secondary">
37+
</span>
38+
</mwc-list-item>
39+
</abbr>
2740
</filtered-list>
2841
<mwc-button
2942
dialogaction="close"
@@ -75,12 +88,7 @@ invalid2007B:7 Substation name (Element '{http://www.iec.ch/61850/2003/SCL}Subst
7588
</abbr>
7689
<abbr title="Not all fields of key identity-constraint '{http://www.iec.ch/61850/2003/SCL}SubstationKey' evaluate to a node.
7790
invalid2007B:7 Substation key identity-constraint '{http://www.iec.ch/61850/2003/SCL}SubstationKey' (Element '{http://www.iec.ch/61850/2003/SCL}Substation')">
78-
<mwc-list-item
79-
aria-disabled="false"
80-
mwc-list-item=""
81-
tabindex="-1"
82-
twoline=""
83-
>
91+
<mwc-list-item twoline="">
8492
<span>
8593
Not all fields of key identity-constraint '{http://www.iec.ch/61850/2003/SCL}SubstationKey' evaluate to a node.
8694
</span>

0 commit comments

Comments
 (0)