|
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { TestBed } from '@angular/core/testing'; |
| 3 | +import { DxButtonModule, DxSliderModule } from 'devextreme-angular'; |
| 4 | + |
| 5 | +@Component({ |
| 6 | + standalone: false, |
| 7 | + selector: 'test-container-component', |
| 8 | + template: '', |
| 9 | +}) |
| 10 | +class TestContainerComponent { |
| 11 | + isVisible = true; |
| 12 | +} |
| 13 | + |
| 14 | +async function forceGC(times = 3): Promise<void> { |
| 15 | + for (let i = 0; i < times; i++) { |
| 16 | + Array.from({ length: 10_000 }, () => ({ data: new Array(100) })); |
| 17 | + globalThis.gc?.(); |
| 18 | + } |
| 19 | + |
| 20 | + await new Promise((resolve) => { |
| 21 | + setTimeout(resolve, 0); |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +describe('Memory leak tests', () => { |
| 26 | + const originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; |
| 27 | + |
| 28 | + beforeAll(() => { |
| 29 | + jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000; |
| 30 | + |
| 31 | + TestBed.configureTestingModule({ |
| 32 | + declarations: [TestContainerComponent], |
| 33 | + imports: [DxButtonModule, DxSliderModule], |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + afterAll(() => { |
| 38 | + jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout; |
| 39 | + }); |
| 40 | + |
| 41 | + it('should not memory leak after change @if block with DxButton (T1324584)', async () => { |
| 42 | + TestBed.overrideComponent(TestContainerComponent, { |
| 43 | + set: { |
| 44 | + template: '@if (isVisible) {<dx-button text="BUTTON"></dx-button>}', |
| 45 | + }, |
| 46 | + }); |
| 47 | + |
| 48 | + const fixture = TestBed.createComponent(TestContainerComponent); |
| 49 | + const component: TestContainerComponent = fixture.componentInstance; |
| 50 | + |
| 51 | + fixture.detectChanges(); |
| 52 | + |
| 53 | + for (let i = 0; i < 100; i++) { |
| 54 | + component.isVisible = !component.isVisible; |
| 55 | + fixture.detectChanges(); |
| 56 | + } |
| 57 | + |
| 58 | + const memoryBefore = await (performance as any).measureUserAgentSpecificMemory(); |
| 59 | + |
| 60 | + for (let i = 0; i < 100; i++) { |
| 61 | + component.isVisible = !component.isVisible; |
| 62 | + fixture.detectChanges(); |
| 63 | + } |
| 64 | + |
| 65 | + await forceGC(); |
| 66 | + |
| 67 | + const memoryAfter = await (performance as any).measureUserAgentSpecificMemory(); |
| 68 | + const memoryDiff = Math.round((memoryAfter.bytes - memoryBefore.bytes) / 1024); |
| 69 | + |
| 70 | + expect(memoryDiff).toBeLessThan(150); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should not memory leak after change @if block with DxSlider (T1324584)', async () => { |
| 74 | + TestBed.overrideComponent(TestContainerComponent, { |
| 75 | + set: { |
| 76 | + template: '@if (isVisible) {<dx-slider></dx-slider>}', |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | + const fixture = TestBed.createComponent(TestContainerComponent); |
| 81 | + const component: TestContainerComponent = fixture.componentInstance; |
| 82 | + |
| 83 | + fixture.detectChanges(); |
| 84 | + |
| 85 | + for (let i = 0; i < 100; i++) { |
| 86 | + component.isVisible = !component.isVisible; |
| 87 | + fixture.detectChanges(); |
| 88 | + } |
| 89 | + |
| 90 | + const memoryBefore = await (performance as any).measureUserAgentSpecificMemory(); |
| 91 | + |
| 92 | + for (let i = 0; i < 100; i++) { |
| 93 | + component.isVisible = !component.isVisible; |
| 94 | + fixture.detectChanges(); |
| 95 | + } |
| 96 | + |
| 97 | + await forceGC(); |
| 98 | + |
| 99 | + const memoryAfter = await (performance as any).measureUserAgentSpecificMemory(); |
| 100 | + const memoryDiff = Math.round((memoryAfter.bytes - memoryBefore.bytes) / 1024); |
| 101 | + |
| 102 | + expect(memoryDiff).toBeLessThan(200); |
| 103 | + }); |
| 104 | +}); |
0 commit comments