hypersistence-tsid icon indicating copy to clipboard operation
hypersistence-tsid copied to clipboard

Node ID out of range when larger tsid.node is used

Open postalservice14 opened this issue 6 months ago • 0 comments

Beacsuse of these guys:

public static final Factory INSTANCE_256 = newInstance256();

public static final Factory INSTANCE_1024 = newInstance1024();

public static final Factory INSTANCE_4096 = newInstance4096();

Whenever the Factory is instantiated, these factories are instantiated and when it pulls the tsid.node number from the system property, it fails with "Node ID out of range"

Here's a test that causes it to happen:

@Test
public void testWithNodeBitsSetFromProperty() {
	final int randomBits = 22;
	// test all allowed values of node bits
	for (int i = 20; i >= 0; i--) {
		final int nodeBits = i;
		final int counterBits = randomBits - nodeBits;
		final int node = (1 << nodeBits) - 1; // max: 2^nodeBits - 1
		System.setProperty(NODE, String.valueOf(node));
		System.setProperty(NODE_COUNT, String.valueOf(1 << nodeBits));
		TSID tsid = TSID.Factory.builder().withNodeBits(nodeBits).withNode(node).build().generate();
		int actual = (int) tsid.getRandom() >>> counterBits;
		assertEquals(node, actual);
	}
}

postalservice14 avatar Aug 02 '24 13:08 postalservice14