0

I am currently creating a test for a complex observable inside of an Angular project, but the test does not work at all (JavaScript heap out of memory).

Basically the Observable emits a new value from a REST service every n seconds (the polling rate, which can be set by the user). If the polling rate is 0 the Observable emits only once. On each update of the FormControl the Observable should re-emit.

The basic test I've written should simply check that the Observable emits (at least) 3 times where in between there is always a delay of the defined polling-rate. The return value is not relevant for this test.

The observable:

// Angular FormControl to set the polling-rate
selectedPollingRateControl = new FormControl<number | null>(null);

healthInformation$ = this.selectedPollingRateControl.valueChanges.pipe(
    switchMap(rate => {
        if (!rate) {
            return of(0);
        }

        return timer(0, rate * 1000);
    }),
    switchMap(() => this.healthInformationService.getHealthInformation())
);

The test:

it('should be reloaded depending on the selected rate', () => {
    const selectedRate = 60;

    const testScheduler = new TestScheduler((actual, expected) => {
        expect(actual).toEqual(expected);
    });

    testScheduler.run(({ expectObservable }) => {
        const expected = `a ${selectedRate}s b ${selectedRate}s (c|)`;

        component.selectedPollingRateControl.setValue(selectedRate);

        expectObservable(component.healthInformation$).toBe(expected);
    });
});

Note:

  • The component references the Angular component that contains the Observable and FormControl
  • I am aware that the timing of the marble-string may not yet be correct, which I will figure out once the test at least fails instead of running into memory problems

Stacktrace of the error:

 RUNS   infrastruktur-test  apps/infrastruktur-test/src/app/health-information/health-information.component.spec.ts

<--- Last few GCs --->

[16556:0000028EC2B9F240]    36546 ms: Scavenge 2039.1 (2078.5) -> 2037.4 (2083.0) MB, 2.7 / 0.0 ms  (average mu = 0.312, current mu = 0.258) allocation failure;
[16556:0000028EC2B9F240]    36552 ms: Scavenge 2041.2 (2083.0) -> 2037.7 (2083.5) MB, 2.5 / 0.0 ms  (average mu = 0.312, current mu = 0.258) allocation failure;
[16556:0000028EC2B9F240]    36560 ms: Scavenge 2041.6 (2083.5) -> 2038.3 (2083.5) MB, 3.0 / 0.0 ms  (average mu = 0.312, current mu = 0.258) allocation failure;


<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 00007FF759741B7F node_api_throw_syntax_error+203775
 2: 00007FF7596C1556 v8::internal::wasm::WasmCode::safepoint_table_offset+63558
 3: 00007FF7596C28C2 v8::internal::wasm::WasmCode::safepoint_table_offset+68530
 4: 00007FF75A1647F4 v8::Isolate::ReportExternalAllocationLimitReached+116
 5: 00007FF75A14FB52 v8::Isolate::Exit+674
 6: 00007FF759FD1BBC v8::internal::EmbedderStackStateScope::ExplicitScopeForTesting+124
 7: 00007FF759FDEE9D v8::internal::Heap::PublishPendingAllocations+1117
 8: 00007FF759FDBF27 v8::internal::Heap::PageFlagsAreConsistent+3367
 9: 00007FF759FCE657 v8::internal::Heap::CollectGarbage+2039
10: 00007FF759FD6A35 v8::internal::Heap::GlobalSizeOfObjects+341
11: 00007FF75A0260AF v8::internal::StackGuard::HandleInterrupts+863
12: 00007FF759CE7F7F v8::internal::DateCache::Weekday+7327
13: 00007FF75A201E81 v8::internal::SetupIsolateDelegate::SetupHeap+558193
14: 00007FF6DAA18B8B

Does anyone have an idea how to fix this test?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

0 Answers0