]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts
import 15.2.2 octopus source
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / rbd-mirroring.service.spec.ts
index 5bd9abad2e2048876d209b468c6b627bb7ac792f..37be86346dd0cca9112bf8facdb5fa2adc7fc086 100644 (file)
@@ -1,4 +1,9 @@
-import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { HttpRequest } from '@angular/common/http';
+import {
+  HttpClientTestingModule,
+  HttpTestingController,
+  TestRequest
+} from '@angular/common/http/testing';
 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
 
 import { configureTestBed } from '../../../testing/unit-test-helper';
@@ -7,6 +12,8 @@ import { RbdMirroringService } from './rbd-mirroring.service';
 describe('RbdMirroringService', () => {
   let service: RbdMirroringService;
   let httpTesting: HttpTestingController;
+  let getMirroringSummaryCalls: () => TestRequest[];
+  let flushCalls: (call: TestRequest) => void;
 
   const summary: Record<string, any> = {
     status: 0,
@@ -31,10 +38,16 @@ describe('RbdMirroringService', () => {
   beforeEach(() => {
     service = TestBed.get(RbdMirroringService);
     httpTesting = TestBed.get(HttpTestingController);
-
-    const req = httpTesting.expectOne('api/block/mirroring/summary');
-    expect(req.request.method).toBe('GET');
-    req.flush(summary);
+    getMirroringSummaryCalls = () => {
+      return httpTesting.match((request: HttpRequest<any>) => {
+        return request.url.match(/api\/block\/mirroring\/summary/) && request.method === 'GET';
+      });
+    };
+    flushCalls = (call: TestRequest) => {
+      if (!call.cancelled) {
+        call.flush(summary);
+      }
+    };
   });
 
   afterEach(() => {
@@ -46,28 +59,31 @@ describe('RbdMirroringService', () => {
   });
 
   it('should periodically poll summary', fakeAsync(() => {
+    const subs = service.startPolling();
+    tick();
     const calledWith: any[] = [];
-    service.subscribeSummary((data) => {
+    service.subscribeSummary((data: any) => {
       calledWith.push(data);
     });
-    service.refreshAndSchedule();
-    tick(30000);
-    // In order to not trigger setTimeout again,
-    // which would raise 'Error: 1 timer(s) still in the queue.'
-    spyOn(service, 'refreshAndSchedule').and.callFake(() => true);
-    tick(30000);
-
-    const calls = httpTesting.match((request) => {
-      return request.url.match(/api\/block\/mirroring\/summary/) && request.method === 'GET';
-    });
+    tick(service.REFRESH_INTERVAL * 2);
+    const calls = getMirroringSummaryCalls();
 
-    expect(calls.length).toEqual(2);
-    calls.forEach((call) => call.flush(summary));
+    expect(calls.length).toEqual(3);
+    calls.forEach((call: TestRequest) => flushCalls(call));
+    expect(calledWith).toEqual([null, summary]);
 
-    expect(calledWith).toEqual([summary, summary, summary]);
+    subs.unsubscribe();
   }));
 
   it('should get current summary', () => {
+    service.refresh();
+    const calledWith: any[] = [];
+    service.subscribeSummary((data: any) => {
+      calledWith.push(data);
+    });
+    const calls = getMirroringSummaryCalls();
+    calls.forEach((call: TestRequest) => flushCalls(call));
+
     expect(service.getCurrentSummary()).toEqual(summary);
   });