wiki.techinc.nl/tests/phpunit/unit/includes/libs/Stats/StatsEmitterTest.php
Cole White 738bc85591 Stats: combine testSendGaugeMetric into testSendMetrics
* Removes testSendGaugeMetric
* Ensure we're checking for calls to timing, gauge, and updateCount
* Tune expectations to exactly how many times we expect the calls

Bug: T370636
Change-Id: I73f05b1876e3afbf0995d5605cb17fa5dc6146d2
2024-07-23 13:56:35 +00:00

75 lines
2.1 KiB
PHP

<?php
namespace Wikimedia\Tests\Stats;
use IBufferingStatsdDataFactory;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use UDPTransport;
use Wikimedia\Stats\OutputFormats;
use Wikimedia\Stats\StatsCache;
use Wikimedia\Stats\StatsFactory;
/**
* @covers \Wikimedia\Stats\Emitters\UDPEmitter
* @covers \Wikimedia\Stats\StatsCache
* @covers \Wikimedia\Stats\Formatters\DogStatsdFormatter
* @covers \Wikimedia\Stats\OutputFormats
*/
class StatsEmitterTest extends TestCase {
public function testSendMetrics() {
// set up a mock statsd data factory
$statsd = $this->createMock( IBufferingStatsdDataFactory::class );
$statsd->expects( $this->exactly( 3 ) )->method( "updateCount" );
$statsd->expects( $this->once() )->method( "gauge" );
$statsd->expects( $this->once() )->method( "timing" );
// initialize cache
$cache = new StatsCache();
// emitter
$emitter = OutputFormats::getNewEmitter(
'mediawiki',
$cache,
OutputFormats::getNewFormatter( OutputFormats::DOGSTATSD )
);
// transport
$transport = $this->createMock( UDPTransport::class );
$transport->expects( $this->once() )->method( "emit" )
->with(
"mediawiki.test.bar:1|c\n" .
"mediawiki.test.bar:1|c\n" .
"mediawiki.test.foo:3.14|ms\n" .
"mediawiki.test.gauge:1|g\n" .
"mediawiki.test.stats_buffered_total:4|c\n"
);
$emitter = $emitter->withTransport( $transport );
// initialize metrics factory
$m = new StatsFactory( $cache, $emitter, new NullLogger, 'test' );
// inject statsd factory
$m->withStatsdDataFactory( $statsd );
// populate metric with statsd copy
$m->getCounter( 'bar' )->copyToStatsdAt( 'test.metric' )->increment();
// fetch same metric from cache and use it
$metric = $m->getCounter( 'bar' );
$metric->increment();
$metric = $m->getTiming( 'foo' )->copyToStatsdAt( 'test.metric.timing' );
$metric->observe( 3.14 );
// name collision: gauge should not appear in output nor throw exception
$metric = @$m->getGauge( 'bar' );
$metric->set( 42 );
$m->getGauge( 'gauge' )->copyToStatsdAt( 'test.metric.gauge' )->set( 1 );
// send metrics
$m->flush();
}
}