Skip to content

Fix #5 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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