Skip to content

Commit

Permalink
Add tests for keygen
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Jan 14, 2022
1 parent 586a343 commit 9dfd0b2
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions keygen/id_test.go
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 9dfd0b2

Please sign in to comment.