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
IdUtil.getDataCenterId(Long.MAX_VALUE)
偶尔会出现小于等于1的 dataCenterId
#2101
Comments
IdUtil.getDataCenterId(Long.MAX_VALUE)
偶尔会出现不是正数的 dataCenterId
IdUtil.getDataCenterId(Long.MAX_VALUE)
偶尔会出现小于等于1的 dataCenterId
麻烦您看下您的ip地址获取正确吗? |
@CherryRum |
好的 这玩意获取的mac地址,所以每个人的都不太好测试 |
我也是扔到 |
我猜测是 容器的问题 |
我大概看了下算法 确实和我之前背的有一点点偏差 |
[INFO] Running cn.hutool.core.util.IdUtilTest
Error: Tests run: 8, Failures: 1, Errors: 0, Skipped: 3, Time elapsed: 0.026 s <<< FAILURE! - in cn.hutool.core.util.IdUtilTest
Error: getDataCenterIdTest Time elapsed: 0.01 s <<< FAILURE!
java.lang.AssertionError: 校验 'dataCenterId > 1' 不通过:dataCenterId = 1, host = /172.17.0.1, mac = [2,66,87,25,123,0]
at cn.hutool.core.util.IdUtilTest.getDataCenterIdTest(IdUtilTest.java:156) 调整后的测试用例源码: @Test
public void getDataCenterIdTest(){
//final long dataCenterId = IdUtil.getDataCenterId(Long.MAX_VALUE);
//Assert.assertTrue(dataCenterId > 1);
long dataCenterId = 1L;
InetAddress host = NetUtil.getLocalhost();
final byte[] mac = NetUtil.getHardwareAddress(host);
if (null != mac) {
dataCenterId = ((0x000000FF & (long) mac[mac.length - 2])
| (0x0000FF00 & (((long) mac[mac.length - 1]) << 8))) >> 6;
dataCenterId = dataCenterId % (Long.MAX_VALUE + 1);
}
System.out.println("dataCenterId = " + dataCenterId + ", host = " + host + ", mac = " + toString(mac));
Assert.assertTrue("校验 'dataCenterId > 1' 不通过:dataCenterId = " + dataCenterId + ", host = " + host + ", mac = " + toString(mac), dataCenterId > 1);
} |
又复现出来了。 |
您试试这个 id = ((0x000000FF & (long) mac[mac.length - 1])
| (0x0000FF00 & (((long) mac[mac.length - 2]) << 8))) >> 6; |
不行,返回0了。 |
我大概知道了,看了下 代码确实有点问题,下个版本应该会有修正 |
好的。 |
5.7.20做一些修改,允许返回0。 见:#2105 |
测试不通过日志:
IdUtilTest
测试用例源码:The text was updated successfully, but these errors were encountered: