<commit_before>/* tslint:disable:no-unused-expression */

import "reflect-metadata";
import "source-map-support/register";

import * as chai from "chai";
import * as nock from "nock";
import { SinonSpy, spy } from "sinon";
import * as sinonChai from "sinon-chai";

import {
    Buckets, MetricRegistry, Scheduler, SECOND, SimpleGauge, SlidingWindowReservoir,
} from "inspector-metrics";
import { suite, test } from "mocha-typescript";
import {
    Percentiles,
    PrometheusMetricReporter,
    PushgatewayMetricReporter,
    PushgatewayReporterOptions } from "../../lib/metrics";
import { MockedClock } from "./mocked-clock";

chai.use(sinonChai);

const expect = chai.expect;

nock("http://localhost:9091")
    .put("/metrics/job/test-pushgateway-reporter/instance/localhost")
    .reply(202, "Accepted");

@suite
export class PushgatewayReporterTest {

    private clock: MockedClock = new MockedClock();
    private registry: MetricRegistry;
    private prometheusReporter: PrometheusMetricReporter;
    private reporter: PushgatewayMetricReporter;
    private internalCallback: () => void;
    private scheduler: Scheduler;
    private schedulerSpy: SinonSpy;

    public before() {
        this.clock.setCurrentTime({ milliseconds: 0, nanoseconds: 0 });
        this.registry = new MetricRegistry();
        this.prometheusReporter = new PrometheusMetricReporter(undefined, undefined, this.clock);
        this.prometheusReporter.addMetricRegistry(this.registry);

        this.scheduler = (prog: () => void, interval: number): NodeJS.Timer => {
            this.internalCallback = prog;
            return null;
        };
        this.schedulerSpy = spy(this.scheduler);

        this.reporter = new PushgatewayMetricReporter(
            this.prometheusReporter,
            new PushgatewayReporterOptions(
                "localhost",
                9091,
                "test-pushgateway-reporter",
                "localhost",
            ),
            10,
            SECOND,
            global.console,
            this.schedulerSpy,
        );
    }

    @test
    public "check reporting with PrometheusMetricReporter"(): void {
        this.registry.newMonotoneCounter("test_monotone_counter_total");
        this.registry.newCounter("test_counter_total");
        this.registry.registerMetric(new SimpleGauge("test_gauge"));
        this.registry.newHistogram("test_histo", null, null, null, Buckets.linear(10, 10, 5));
        this.registry.newMeter("test_meter");
        const timer = this.registry.newTimer(
            "test_timer",
            null,
            this.registry.getDefaultClock(),<commit_msg>fixed test for nodejs 7 & 6<commit_after>/* tslint:disable:no-unused-expression */

import "reflect-metadata";
import "source-map-support/register";

import * as chai from "chai";
import * as nock from "nock";
import { SinonSpy, spy } from "sinon";
import * as sinonChai from "sinon-chai";

import {
    Buckets, MetricRegistry, Scheduler, SECOND, SimpleGauge, SlidingWindowReservoir,
} from "inspector-metrics";
import { suite, test } from "mocha-typescript";
import {
    Percentiles,
    PrometheusMetricReporter,
    PushgatewayMetricReporter,
    PushgatewayReporterOptions } from "../../lib/metrics";
import { MockedClock } from "./mocked-clock";

chai.use(sinonChai);

const expect = chai.expect;

nock("http://localhost:9091")
    .put("/metrics/job/test-pushgateway-reporter/instance/localhost")
    .reply(202, "Accepted");

@suite
export class PushgatewayReporterTest {

    private clock: MockedClock = new MockedClock();
    private registry: MetricRegistry;
    private prometheusReporter: PrometheusMetricReporter;
    private reporter: PushgatewayMetricReporter;
    private internalCallback: () => void;
    private scheduler: Scheduler;
    private schedulerSpy: SinonSpy;

    public before() {
        this.clock.setCurrentTime({ milliseconds: 0, nanoseconds: 0 });
        this.registry = new MetricRegistry();
        this.prometheusReporter = new PrometheusMetricReporter(undefined, undefined, this.clock);
        this.prometheusReporter.addMetricRegistry(this.registry);

        this.scheduler = (prog: () => void, interval: number): NodeJS.Timer => {
            this.internalCallback = prog;
            return null;
        };
        this.schedulerSpy = spy(this.scheduler);

        this.reporter = new PushgatewayMetricReporter(
            this.prometheusReporter,
            new PushgatewayReporterOptions(
                "localhost",
                9091,
                "test-pushgateway-reporter",
                "localhost",
            ),
            10,
            SECOND,
            null,
            this.schedulerSpy,
        );
    }

    @test
    public "check reporting with PrometheusMetricReporter"(): void {
        this.registry.newMonotoneCounter("test_monotone_counter_total");
        this.registry.newCounter("test_counter_total");
        this.registry.registerMetric(new SimpleGauge("test_gauge"));
        this.registry.newHistogram("test_histo", null, null, null, Buckets.linear(10, 10, 5));
        this.registry.newMeter("test_meter");
        const timer = this.registry.newTimer(
            "test_timer",
            null,
            this.registry.getDefaultClock(),
{'fraction_non_alphanumeric': 0.11157495256166983, 'fraction_numerical': 0.007969639468690701, 'mean_word_length': 2.921875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '2770403', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1505, 'n_words': 310}