Skip to content

Commit

Permalink
fix: add tests for uniq ids entropy
Browse files Browse the repository at this point in the history
  • Loading branch information
thoas committed Dec 2, 2019
1 parent 071d83d commit 18fe3d8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion task.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewTask(name string, payload interface{}, options ...Option) *Task {
}

t := &Task{
ID: id(),
ID: ID(),
Name: name,
Payload: payload,
Status: taskStatusWaiting,
Expand Down
8 changes: 4 additions & 4 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"github.com/pkg/errors"
)

func id() string {
t := time.Unix(1000000, 0)
entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0)
return ulid.MustNew(ulid.Now(), entropy).String()
func ID() string {
t := time.Now().UTC()
entropy := rand.New(rand.NewSource(t.UnixNano()))
return ulid.MustNew(ulid.Timestamp(t), entropy).String()
}

func reverseDurations(durations []time.Duration) []time.Duration {
Expand Down
23 changes: 23 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package bokchoy_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/thoas/bokchoy"
"github.com/thoas/go-funk"
)

func TestUtils_ID(t *testing.T) {
is := assert.New(t)

iteration := 1000

ids := make([]string, iteration)

for i := 0; i < iteration; i++ {
ids[i] = bokchoy.ID()
}

is.Len(funk.UniqString(ids), iteration)
}

1 comment on commit 18fe3d8

@tsatke
Copy link

@tsatke tsatke commented on 18fe3d8 Dec 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closes #15

Please sign in to comment.