Skip to content

Commit

Permalink
moving to test files
Browse files Browse the repository at this point in the history
  • Loading branch information
wcharczuk committed Apr 12, 2024
1 parent 40eda78 commit d1d9903
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
15 changes: 0 additions & 15 deletions identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"strings"
"sync"
"sync/atomic"
)

// Identifier is a unique id.
Expand Down Expand Up @@ -71,19 +70,6 @@ func cryptoRandIdentifierProvider() (output Identifier) {
return
}

func counterIdentifierProvider() (output Identifier) {
newCounter := atomic.AddUint64(&identifierCounter, 1)
output[15] = byte(newCounter)
output[14] = byte(newCounter >> 8)
output[13] = byte(newCounter >> 16)
output[12] = byte(newCounter >> 24)
output[11] = byte(newCounter >> 32)
output[10] = byte(newCounter >> 40)
output[9] = byte(newCounter >> 48)
output[8] = byte(newCounter >> 56)
return
}

const randPoolSize = 16 * 16

var (
Expand All @@ -92,7 +78,6 @@ var (
identifierRandPoolPos = randPoolSize // protected with poolMu
identifierRandPool [randPoolSize]byte // protected with poolMu
randomSource = rand.Reader // random function
identifierCounter uint64
)

var zero Identifier
Expand Down
16 changes: 16 additions & 0 deletions identifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"sync/atomic"
"testing"

"github.com/wcharczuk/go-incr/testutil"
Expand All @@ -20,6 +21,21 @@ func Test_Identifier(t *testing.T) {
testutil.Equal(t, hex.EncodeToString(id[12:]), id.Short())
}

var identifierCounter uint64

func counterIdentifierProvider() (output Identifier) {
newCounter := atomic.AddUint64(&identifierCounter, 1)
output[15] = byte(newCounter)
output[14] = byte(newCounter >> 8)
output[13] = byte(newCounter >> 16)
output[12] = byte(newCounter >> 24)
output[11] = byte(newCounter >> 32)
output[10] = byte(newCounter >> 40)
output[9] = byte(newCounter >> 48)
output[8] = byte(newCounter >> 56)
return
}

func Test_SetIdentifierProvider(t *testing.T) {
t.Cleanup(func() {
SetIdentifierProvider(cryptoRandIdentifierProvider)
Expand Down

0 comments on commit d1d9903

Please sign in to comment.