From aef78d13d4d17071554c0d905bc353dfd78125fd Mon Sep 17 00:00:00 2001 From: Matthew Flaschen Date: Mon, 4 Apr 2016 20:16:40 -0400 Subject: [PATCH] Fix UID test and make debugging easier This should fix the test and make the test failures (if any) more informative. The issue was that it was trying to extract a 46-bit segment (the timestamp). But because the whole thing wasn't 0-padded first, the timestamp might be shorter. So it would extract part of the next segment too. That meant it was testing a 46-bit segment with no guaranteed properties, with arbitrary results. Bug: T131549 Change-Id: I11c9e86951cc1d6b186710cda6e42ee58c920db8 --- .../includes/utils/UIDGeneratorTest.php | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/tests/phpunit/includes/utils/UIDGeneratorTest.php b/tests/phpunit/includes/utils/UIDGeneratorTest.php index d746ea1459..60d4e99978 100644 --- a/tests/phpunit/includes/utils/UIDGeneratorTest.php +++ b/tests/phpunit/includes/utils/UIDGeneratorTest.php @@ -9,9 +9,8 @@ class UIDGeneratorTest extends PHPUnit_Framework_TestCase { } /** - * Flaky test (T131549). + * Test that generated UIDs have the expected properties * - * @group Broken * @dataProvider provider_testTimestampedUID * @covers UIDGenerator::newTimestampedUID128 * @covers UIDGenerator::newTimestampedUID88 @@ -34,19 +33,29 @@ class UIDGeneratorTest extends PHPUnit_Framework_TestCase { $this->assertSame( array_unique( $ids ), $ids, "All generated IDs are unique." ); foreach ( $ids as $id ) { - $id_bin = Wikimedia\base_convert( $id, 10, 2 ); - $lastId_bin = Wikimedia\base_convert( $lastId, 10, 2 ); + // Convert string to binary and pad to full length so we can + // extract segments + $id_bin = Wikimedia\base_convert( $id, 10, 2, $bits ); + $lastId_bin = Wikimedia\base_convert( $lastId, 10, 2, $bits ); + + $timestamp_bin = substr( $id_bin, 0, $tbits ); + $last_timestamp_bin = substr( $lastId_bin, 0, $tbits ); $this->assertGreaterThanOrEqual( - substr( $lastId_bin, 0, $tbits ), - substr( $id_bin, 0, $tbits ), - "New ID timestamp ($id_bin) >= prior one ($lastId_bin)." ); + $last_timestamp_bin, + $timestamp_bin, + "timestamp ($timestamp_bin) of current ID ($id_bin) >= timestamp ($last_timestamp_bin) " . + "of prior one ($lastId_bin)" ); + + $hostbits_bin = substr( $id_bin, -$hostbits ); + $last_hostbits_bin = substr( $lastId_bin, -$hostbits ); if ( $hostbits ) { $this->assertEquals( - substr( $id_bin, -$hostbits ), - substr( $lastId_bin, -$hostbits ), - "Host ID of ($id_bin) is same as prior one ($lastId_bin)." ); + $hostbits_bin, + $last_hostbits_bin, + "Host ID ($hostbits_bin) of current ID ($id_bin) is same as host ID ($last_hostbits_bin) " . + "of prior one ($lastId_bin)." ); } $lastId = $id; -- 2.20.1