wiki.techinc.nl/tests/phpunit/unit/includes/libs/stats/BufferingStatsdDataFactoryTest.php
Timo Tijhof 143a532b41 stats: Simplify normalizeMetricKey() and fix bad output for edge cases
This simplification effort is the result of a (failed) attempt to
micro-optimize this method. I submit it, despite it not being any
faster or slower, because I noticed that the simpler code actually
resulted in behaviours that I consider "less surprising".

* The method previously (I believe, unintentionally) treated all
  colons as segment separators. It now only treats double-colon (::)
  as segment separator.

  Thus "foo.bar:baz" became "foo.bar.baz", now "foo.bar_baz".

* Leading and trailing underscores are no longer removed from
  segments. This makes the former-presence of invalid characters
  more obvious to consumers and seems helpful.
  This also fixed two bugs:
  - In some cases, a segment could become the empty string, which is
    invalid for Graphite as each segment must be a directory name.
  - In some cases, segments could dissappear entirely, thus changing
    the hierarchy specified by the producer in ways they probably
    didn't expect.

* Avoid use of empty().

Change-Id: I8601faf1fe45124370a92d43e854608e623c1214
2021-09-10 19:39:30 +00:00

30 lines
1.1 KiB
PHP

<?php
use Wikimedia\TestingAccessWrapper;
/**
* @covers BufferingStatsdDataFactory
*/
class BufferingStatsdDataFactoryTest extends PHPUnit\Framework\TestCase {
public static function provideNormalizeMetricKey() {
// Reasonable and relied upon
yield 'Keep empty string' => [ '', '' ];
yield 'Keep normal' => [ 'foo.BarbaraBar.baz_quux', 'foo.BarbaraBar.baz_quux' ];
yield 'Skip empty segments' => [ '.missing.start.end.', 'missing.start.end' ];
yield 'Strip separator prefix' => [ '\\Vendor\\Class::method.x', 'Vendor_Class.method.x' ];
// Unreasonable and may change, here to self-document current behaviour
yield [ '__double__under__', 'double_under' ];
yield [ '.:!:.paamayim.:!:.nekudotayim.:!:.', 'paamayim._.nekudotayim' ];
yield [ 'first----.!!.!!.-second', 'first_._._._second' ];
}
/**
* @dataProvider provideNormalizeMetricKey
*/
public function testNormalizeMetricKey( string $input, string $expected ) {
$stats = TestingAccessWrapper::newFromClass( BufferingStatsdDataFactory::class );
$this->assertSame( $expected, $stats->normalizeMetricKey( $input ) );
}
}