<commit_before>/**
 * @license
 * Copyright (c) 2018 Google Inc. All rights reserved.
 * This code may only be used under the BSD style license found at
 * http://polymer.github.io/LICENSE.txt
 * Code distributed by Google as part of this project is also
 * subject to an additional IP rights grant found at
 * http://polymer.github.io/PATENTS.txt
 */

import {assert} from '../../runtime/test/chai-web.js';
import {Tracing} from '../trace.js';

/**
 * For an explainer of event types and exact format google 'Trace Event Format',
 * as of writing available at
 * https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview.
 */

describe('Tracing', function() {

  function assertTimestampsIncreasing(...timestmaps) {
    for (let i = 1; i < timestmaps.length; i++) {
      assert.isAbove(timestmaps[i], timestmaps[i - 1],
        `Expected sequence of timestamps not increasing at position ${i}`);
    }
  }

  function assertFlowEnclosedInComplete(flowEvent, completeEvent) {
    if (flowEvent.ph === 's') {
      assert.equal(flowEvent.ts, completeEvent.ts + completeEvent.dur,
        `Flow should start at the end of the first complete event`);
    } else {
      assert.equal(flowEvent.ts, completeEvent.ts,
        `Flow should step and end at the beginning of the respective complete event`);
    }
  }

  const promiseResult = 42;
  async function waitABit() {
    return await new Promise(resolve => {
      setTimeout(resolve, 1, promiseResult);
    });
  }

  it('records progressing time - sanity check', async () => {
    Tracing.enable();

    const one = Tracing.now();
    const two = Tracing.now();
    const three = Tracing.now();

    assertTimestampsIncreasing(one, two, three);
  });

  it('traces a sync event', async () => {
    Tracing.enable();
    Tracing.__clearForTests();

    let beginMicros = Tracing.now();
    let trace = Tracing.start({cat: 'Stuff', name: 'Thingy::thing'});
    const sum = 2 + 2; // Doing some work.
    trace.end();
    let endMicros = Tracing.now();

    const events = Tracing.save().traceEvents;
    assert.lengthOf(events, 1);
    const [event] = events;
    assert.equal(event.ph, 'X');
    assert.equal(event.cat, 'Stuff');
    assert.equal(event.name, 'Thingy::thing');
    assert.isUndefined(event.args);
    assert.isAbove(event.ts, beginMicros);
    assert.isBelow(event.ts + event.dur, endMicros);
    assert.isAbove(event.dur, 0);
  });

  it('traces an asynchronous event', async () => {
    // Trace waits twice for an async operation. Visualizes as:
    // |---| → |---| → |---|

    Tracing.enable();
    Tracing.__clearForTests();

    const beginMicros = Tracing.now();
    const trace = Tracing.start({cat: 'Stuff', name: 'Thingy::thing'});
    const insideFirstSync = Tracing.now();
    let promise = trace.wait(waitABit());
    const betweenFirstAndSecond = Tracing.now();
    assert.equal(await promise, promiseResult);<commit_msg>Fixes flakiness of Tracing tests (#1404)<commit_after>/**
 * @license
 * Copyright (c) 2018 Google Inc. All rights reserved.
 * This code may only be used under the BSD style license found at
 * http://polymer.github.io/LICENSE.txt
 * Code distributed by Google as part of this project is also
 * subject to an additional IP rights grant found at
 * http://polymer.github.io/PATENTS.txt
 */

import {assert} from '../../runtime/test/chai-web.js';
import {Tracing} from '../trace.js';

/**
 * For an explainer of event types and exact format google 'Trace Event Format',
 * as of writing available at
 * https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview.
 */

describe('Tracing', function() {

  function assertTimestampsIncreasing(...timestmaps) {
    for (let i = 1; i < timestmaps.length; i++) {
      // Strict increasing can be flaky one some platforms, equality is good enough too.
      assert.isAtLeast(timestmaps[i], timestmaps[i - 1],
        `Expected sequence of timestamps not increasing at position ${i}`);
    }
  }

  function assertFlowEnclosedInComplete(flowEvent, completeEvent) {
    if (flowEvent.ph === 's') {
      assert.equal(flowEvent.ts, completeEvent.ts + completeEvent.dur,
        `Flow should start at the end of the first complete event`);
    } else {
      assert.equal(flowEvent.ts, completeEvent.ts,
        `Flow should step and end at the beginning of the respective complete event`);
    }
  }

  const promiseResult = 42;
  async function waitABit() {
    return await new Promise(resolve => {
      setTimeout(resolve, 1, promiseResult);
    });
  }

  it('records progressing time - sanity check', async () => {
    Tracing.enable();

    const one = Tracing.now();
    const two = Tracing.now();
    const three = Tracing.now();

    assertTimestampsIncreasing(one, two, three);
  });

  it('traces a sync event', async () => {
    Tracing.enable();
    Tracing.__clearForTests();

    let beginMicros = Tracing.now();
    let trace = Tracing.start({cat: 'Stuff', name: 'Thingy::thing'});
    const sum = 2 + 2; // Doing some work.
    trace.end();
    let endMicros = Tracing.now();

    const events = Tracing.save().traceEvents;
    assert.lengthOf(events, 1);
    const [event] = events;
    assert.equal(event.ph, 'X');
    assert.equal(event.cat, 'Stuff');
    assert.equal(event.name, 'Thingy::thing');
    assert.isUndefined(event.args);

    assertTimestampsIncreasing(
      beginMicros,
      event.ts,
      event.ts + event.dur,
      endMicros
    );
  });

  it('traces an asynchronous event', async () => {
    // Trace waits twice for an async operation. Visualizes as:
    // |---| → |---| → |---|

    Tracing.enable();
    Tracing.__clearForTests();

    const beginMicros = Tracing.now();
    const trace = Tracing.start({cat: 'Stuff', name: 'Thingy::thing'});
    const insideFirstSync = Tracing.now();
    let promise = trace.wait(waitABit());
    const betweenFirstAndSecond = Tracing.now();
    assert.equal(await promise, promiseResult);
{'fraction_non_alphanumeric': 0.13998996487706974, 'fraction_numerical': 0.0068573340023415285, 'mean_word_length': 3.2291371994342293, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3075524', 'n_tokens_mistral': 1932, 'n_tokens_neox': 1848, 'n_words': 518}