diff --git a/keygen/id_test.go b/keygen/id_test.go new file mode 100644 index 0000000..f5d538f --- /dev/null +++ b/keygen/id_test.go @@ -0,0 +1,34 @@ +package keygen + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestNext(t *testing.T) { + id := Next(24) + ipv4, err := getIPv4() + + assert.Nil(t, err) + assert.GreaterOrEqual(t, time.Now().UnixNano()/1e6, int64(id>>int64(timeLeft))+twepoch) + assert.Equal(t, int(ipv4[3])%64, getWorkerNumber(id)) + assert.Equal(t, 24, TableIdx(id)) +} + +func TestNextWithLargerCheck(t *testing.T) { + var lastId int64 + tableIdx := int64(1) + + for i := 0; i < 10000; i++ { + newId := Next(tableIdx) + + if lastId >= newId { + t.Errorf("Expect new: %d > last: %d, but not.", newId, lastId) + break + } + + lastId = newId + } +}