Skip to content

Commit

Permalink
Fix #5
Browse files Browse the repository at this point in the history
  • Loading branch information
jonross committed May 12, 2015
1 parent 54631fa commit 90abbcf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
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

0 comments on commit 90abbcf

Please sign in to comment.