Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class SnowflakeEncodingProvider implements EncodingProvider {
private long shiftedMachineId;

public SnowflakeEncodingProvider(long machineId) {
this.shiftedMachineId = ((machineId % MACHINE_CODES) << SHIFT_MACHINE_CODE_BITS);
this.shiftedMachineId = ((Math.abs(machineId) % MACHINE_CODES) << SHIFT_MACHINE_CODE_BITS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,37 @@
import com.github.rholder.fauxflake.api.Id;
import com.github.rholder.fauxflake.api.IdGenerator;
import com.github.rholder.fauxflake.provider.SystemTimeProvider;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;

@RunWith(Parameterized.class)
public class SnowflakeDecodingUtilsTest extends SnowflakeDecodingUtils {

private static final int TEST_MACHINE_ID = 53;
private final int machineId;
private IdGenerator idGenerator;

@Parameters
public static Collection<?> data() {
Object[][] data = new Object[][] { {53}, {-53} };
return Arrays.asList(data);
}

public SnowflakeDecodingUtilsTest(int machineId) {
this.machineId = machineId;
}

@Before
public void before() {
idGenerator = new DefaultIdGenerator(new SystemTimeProvider(), new SnowflakeEncodingProvider(TEST_MACHINE_ID));
idGenerator = new DefaultIdGenerator(new SystemTimeProvider(), new SnowflakeEncodingProvider(machineId));
}

@Test
Expand All @@ -55,7 +72,7 @@ public void encodedValueCheck() throws InterruptedException {
Date idDate = decodeDate(longId);

Assert.assertTrue("Now is greater than generated id", now.getTime() <= idDate.getTime());
Assert.assertEquals("Unexpected machine id", TEST_MACHINE_ID, decodeMachineId(longId));
Assert.assertEquals("Unexpected machine id", Math.abs(machineId), decodeMachineId(longId));
Assert.assertEquals("Unexpected number of bytes in id", 8, byteId.length);
}

Expand Down