Test for TestToTime fails
So i cloned the repo and ran the tests and here is the output
? github.com/sony/sonyflake/v2/awsutil [no test files]
? github.com/sony/sonyflake/v2/example [no test files]
? github.com/sony/sonyflake/v2/mock [no test files]
? github.com/sony/sonyflake/v2/types [no test files]
sonyflake id: 838915993
decompose: map[id:838915993 machine:55193 sequence:0 time:50]
max sequence: 255
number of id: 24577
number of cpu: 16
number of id: 16000
--- FAIL: TestToTime (0.00s)
sonyflake_test.go:370: unexpected time: 2025-06-22 18:29:14.304 +0530 IST
FAIL
FAIL github.com/sony/sonyflake/v2 0.943s
FAIL
@iyad-f
I have tried to fix this issue: https://github.com/sony/sonyflake/pull/76/files Could you run the test again?
@YoshiyukiMineo Nope, here are the logs
? github.com/sony/sonyflake/v2/awsutil [no test files]
? github.com/sony/sonyflake/v2/example [no test files]
? github.com/sony/sonyflake/v2/mock [no test files]
? github.com/sony/sonyflake/v2/types [no test files]
sonyflake id: 838915993
decompose: map[id:838915993 machine:55193 sequence:0 time:50]
max sequence: 255
number of id: 24053
number of cpu: 16
number of id: 16000
--- FAIL: TestToTime (0.00s)
sonyflake_test.go:370: unexpected time: 2025-06-23 18:21:20.195 +0530 IST
FAIL
FAIL github.com/sony/sonyflake/v2 0.948s
FAIL
i would like to mention a lot of those tests are dependent on how the hardware performs
there is no guarantee that only 1ms will pass from the start to the time when the id was generated, i tested multiple times, sometimes the diff is 1.63ms sometimes 2.552ms or sometimes even around 12ms, but it expects it to be 1m, by the time the nextId func has even been called already more than 1ms has passed since the start, so this test is kind of flawed
I have made TimeUnit 100 times larger to pass the test on almost all CPUs. Could you run the test again?
Ofcourse it will pass for larger timeunits even for 10ms it passes most of the times but again this isnt a perfect test, it comes with alot of dependencies, so it isnt constant. I cant think of any other way to write a test for ToTime but yeah maybe someone can or just maybe leave a warning there that this test may fail or something
This same thing can happen to TestNextID and TestNextID_InSequence.
For TestNextID you have added a buffer of +1 to manage inconsistencies, but again depending upon the hardware this can change. For TestNextID_InSequence it may not be able to generate 1<<bitsSequence-1 in the same time tick, especially if the time unit is the lowest i.e. 1ms. So again both of those tests are pretty hardware dependent
Although I noticed they depends on hardware performance while writing them, I compromised because they will succeed in recent PCs. We can write perfect tests by abstracting time.Now. When I have time, I will do it.
I have made unit tests stabler: https://github.com/sony/sonyflake/pull/78