| 1 |
<?php |
| 2 |
|
| 3 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 4 |
use Wikimedia\UUID\GlobalIdGenerator; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
class GlobalIdGeneratorTest extends PHPUnit\Framework\TestCase { |
| 10 |
|
| 11 |
use MediaWikiCoversValidator; |
| 12 |
use MediaWikiTestCaseTrait; |
| 13 |
|
| 14 |
|
| 15 |
private $globalIdGenerator; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public function testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits ) { |
| 21 |
$id = $this->globalIdGenerator->$method(); |
| 22 |
$this->assertTrue( ctype_digit( $id ), "UID made of digit characters" ); |
| 23 |
$this->assertLessThanOrEqual( $digitlen, strlen( $id ), |
| 24 |
"UID has the right number of digits" ); |
| 25 |
$this->assertLessThanOrEqual( $bits, strlen( Wikimedia\base_convert( $id, 10, 2 ) ), |
| 26 |
"UID has the right number of bits" ); |
| 27 |
|
| 28 |
$ids = []; |
| 29 |
for ( $i = 0; $i < 300; $i++ ) { |
| 30 |
$ids[] = $this->globalIdGenerator->$method(); |
| 31 |
} |
| 32 |
|
| 33 |
$lastId = array_shift( $ids ); |
| 34 |
|
| 35 |
$this->assertSame( array_unique( $ids ), $ids, "All generated IDs are unique." ); |
| 36 |
|
| 37 |
foreach ( $ids as $id ) { |
| 38 |
|
| 39 |
|
| 40 |
$id_bin = Wikimedia\base_convert( $id, 10, 2, $bits ); |
| 41 |
$lastId_bin = Wikimedia\base_convert( $lastId, 10, 2, $bits ); |
| 42 |
|
| 43 |
$timestamp_bin = substr( $id_bin, 0, $tbits ); |
| 44 |
$last_timestamp_bin = substr( $lastId_bin, 0, $tbits ); |
| 45 |
|
| 46 |
$this->assertGreaterThanOrEqual( |
| 47 |
$last_timestamp_bin, |
| 48 |
$timestamp_bin, |
| 49 |
"timestamp ($timestamp_bin) of current ID ($id_bin) >= timestamp ($last_timestamp_bin) " . |
| 50 |
"of prior one ($lastId_bin)" ); |
| 51 |
|
| 52 |
$hostbits_bin = substr( $id_bin, -$hostbits ); |
| 53 |
$last_hostbits_bin = substr( $lastId_bin, -$hostbits ); |
| 54 |
|
| 55 |
if ( $hostbits ) { |
| 56 |
$this->assertEquals( |
| 57 |
$hostbits_bin, |
| 58 |
$last_hostbits_bin, |
| 59 |
"Host ID ($hostbits_bin) of current ID ($id_bin) is same as host ID ($last_hostbits_bin) " . |
| 60 |
"of prior one ($lastId_bin)." ); |
| 61 |
} |
| 62 |
|
| 63 |
$lastId = $id; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public static function provider_testTimestampedUID() { |
| 68 |
|
| 69 |
return [ |
| 70 |
[ 'newTimestampedUID128', 39, 128, 46, 48 ], |
| 71 |
[ 'newTimestampedUID128', 39, 128, 46, 48 ], |
| 72 |
[ 'newTimestampedUID88', 27, 88, 46, 32 ], |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
public function testUUIDv1() { |
| 77 |
$ids = []; |
| 78 |
for ( $i = 0; $i < 100; $i++ ) { |
| 79 |
$id = $this->globalIdGenerator->newUUIDv1(); |
| 80 |
$this->assertMatchesRegularExpression( |
| 81 |
'!^[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$!', |
| 82 |
$id, |
| 83 |
"UID $id has the right format" |
| 84 |
); |
| 85 |
$ids[] = $id; |
| 86 |
|
| 87 |
$id = $this->globalIdGenerator->newRawUUIDv1(); |
| 88 |
$this->assertMatchesRegularExpression( |
| 89 |
'!^[0-9a-f]{12}1[0-9a-f]{3}[89ab][0-9a-f]{15}$!', |
| 90 |
$id, |
| 91 |
"UID $id has the right format" |
| 92 |
); |
| 93 |
|
| 94 |
$id = $this->globalIdGenerator->newRawUUIDv1(); |
| 95 |
$this->assertMatchesRegularExpression( |
| 96 |
'!^[0-9a-f]{12}1[0-9a-f]{3}[89ab][0-9a-f]{15}$!', |
| 97 |
$id, |
| 98 |
"UID $id has the right format" |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
$this->assertEquals( array_unique( $ids ), $ids, "All generated IDs are unique." ); |
| 103 |
} |
| 104 |
|
| 105 |
public function testUUIDv4() { |
| 106 |
$ids = []; |
| 107 |
for ( $i = 0; $i < 100; $i++ ) { |
| 108 |
$id = $this->globalIdGenerator->newUUIDv4(); |
| 109 |
$ids[] = $id; |
| 110 |
$this->assertMatchesRegularExpression( |
| 111 |
'!^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$!', |
| 112 |
$id, |
| 113 |
"UID $id has the right format" |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
$this->assertEquals( array_unique( $ids ), $ids, 'All generated IDs are unique.' ); |
| 118 |
} |
| 119 |
|
| 120 |
public function testRawUUIDv4() { |
| 121 |
for ( $i = 0; $i < 100; $i++ ) { |
| 122 |
$id = $this->globalIdGenerator->newRawUUIDv4(); |
| 123 |
$this->assertMatchesRegularExpression( |
| 124 |
'!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', |
| 125 |
$id, |
| 126 |
"UID $id has the right format" |
| 127 |
); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
public function testNewSequentialID() { |
| 132 |
$id1 = $this->globalIdGenerator->newSequentialPerNodeID( 'test', 32 ); |
| 133 |
$id2 = $this->globalIdGenerator->newSequentialPerNodeID( 'test', 32 ); |
| 134 |
|
| 135 |
$this->assertIsFloat( $id1, "ID returned as float" ); |
| 136 |
$this->assertIsFloat( $id2, "ID returned as float" ); |
| 137 |
$this->assertGreaterThan( 0, $id1, "ID greater than 1" ); |
| 138 |
$this->assertGreaterThan( $id1, $id2, "IDs increasing in value" ); |
| 139 |
} |
| 140 |
|
| 141 |
public function testNewSequentialIDs() { |
| 142 |
$ids = $this->globalIdGenerator->newSequentialPerNodeIDs( 'test', 32, 5 ); |
| 143 |
$lastId = null; |
| 144 |
foreach ( $ids as $id ) { |
| 145 |
$this->assertIsFloat( $id, "ID returned as float" ); |
| 146 |
$this->assertGreaterThan( 0, $id, "ID greater than 1" ); |
| 147 |
if ( $lastId ) { |
| 148 |
$this->assertGreaterThan( $lastId, $id, "IDs increasing in value" ); |
| 149 |
} |
| 150 |
$lastId = $id; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
public static function provideGetTimestampFromUUIDv1() { |
| 155 |
yield [ '65d143b0-3c7a-11ea-b77f-2e728ce88125', '20200121181818' ]; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function testGetTimestampFromUUIDv1( string $uuid, string $ts ) { |
| 164 |
$this->assertEquals( $ts, $this->globalIdGenerator->getTimestampFromUUIDv1( $uuid ) ); |
| 165 |
$this->assertEquals( |
| 166 |
ConvertibleTimestamp::convert( TS_ISO_8601, $ts ), |
| 167 |
$this->globalIdGenerator->getTimestampFromUUIDv1( $uuid, TS_ISO_8601 ) |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
public static function provideGetTimestampFromUUIDv1InvalidUUIDv1() { |
| 172 |
yield [ 'this_is_an_invalid_uuid_v1' ]; |
| 173 |
yield [ 'e5bb7f6b-0f28-4867-a93c-1b33b5c63adf' ]; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public function testGetTimestampFromUUIDv1InvalidUUIDv1( string $uuid ) { |
| 181 |
$this->expectException( InvalidArgumentException::class ); |
| 182 |
$this->globalIdGenerator->getTimestampFromUUIDv1( $uuid ); |
| 183 |
} |
| 184 |
|
| 185 |
protected function setUp(): void { |
| 186 |
$this->globalIdGenerator = new GlobalIdGenerator( |
| 187 |
wfTempDir(), |
| 188 |
static function ( $command ) { |
| 189 |
return wfShellExec( $command ); |
| 190 |
} |
| 191 |
); |
| 192 |
} |
| 193 |
|
| 194 |
protected function tearDown(): void { |
| 195 |
|
| 196 |
$this->globalIdGenerator->unitTestTearDown(); |
| 197 |
parent::tearDown(); |
| 198 |
} |
| 199 |
|
| 200 |
} |