// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

const {assert} = chai;

import * as Common from '../../../../../front_end/core/common/common.js';

const Throttler = Common.Throttler.Throttler;

describe('Throttler class', () => {
  it('is able to schedule a process as soon as possible', () => {
    let result = 'original value';

    async function assignVar1() {
      result = 'new value';
    }

    const throttler = new Throttler(10);
    const promiseTest = throttler.schedule(assignVar1, true);
    void promiseTest.then(() => {
      assert.strictEqual(result, 'new value', 'process was not scheduled correctly');
    });

    assert.strictEqual(result, 'original value', 'process was not scheduled correctly');
  });

  it('is able to schedule two processes as soon as possible', () => {
    let result = 'original value';

    async function assignVar1() {
      result = 'new value 1';
    }

    async function assignVar2() {
      result = 'new value 2';
    }

    const throttler = new Throttler(10);
    const promiseTest = throttler.schedule(assignVar1, true);
    void throttler.schedule(assignVar2, true);
    void promiseTest.then(() => {
      assert.strictEqual(result, 'new value 2', 'process was not scheduled correctly');
    });

    assert.strictEqual(result, 'original value', 'process was not scheduled correctly');
  });
});

// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

const {assert} = chai;

import * as Common from '../../../../../front_end/core/common/common.js';

const Throttler = Common.Throttler.Throttler;

describe('Throttler class', () => {
  it('is able to schedule a process as soon as possible', async () => {
    let result = 'original value';

    async function assignVar1() {
      result = 'new value';
    }

    const throttler = new Throttler(10);
    const promiseTest = throttler.schedule(assignVar1, true);

    assert.strictEqual(result, 'original value', 'process was not scheduled correctly');
    await promiseTest;
    assert.strictEqual(result, 'new value', 'process was not scheduled correctly');
  });

  it('is able to schedule two processes as soon as possible', async () => {
    let result = 'original value';

    async function assignVar1() {
      result = 'new value 1';
    }

    async function assignVar2() {
      result = 'new value 2';
    }

    const throttler = new Throttler(10);
    const promiseTest = throttler.schedule(assignVar1, true);
    void throttler.schedule(assignVar2, true);

    assert.strictEqual(result, 'original value', 'process was not scheduled correctly');
    await promiseTest;
    assert.strictEqual(result, 'new value 2', 'process was not scheduled correctly');
  });
});

Make throttler tests to properly wait for the promise before assertions.
Make throttler tests to properly wait for the promise before assertions.

Bug: 1376495
Change-Id: Ic914b03a4eaf3a3af1e64b18346fd5da77582ff1
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3966439
Auto-Submit: Danil Somsikov <f16a5ee11618cab93684e694f9aa762d34f02920@chromium.org>
Reviewed-by: Kateryna Prokopenko <04b7d4bdbd93555cee90f085bb91b72524d21fca@chromium.org>
Commit-Queue: Danil Somsikov <f16a5ee11618cab93684e694f9aa762d34f02920@chromium.org>
