Skip to content

Commit

Permalink
Write some notes and improve some parts
Browse files Browse the repository at this point in the history
  • Loading branch information
Gioni06 committed Jan 27, 2021
1 parent 515889f commit 6b512ff
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
8 changes: 5 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/gioni06/go-timeflake/pkg/timeflake"
)

// There will be a cli interface soon.
// Right now this is only used for development purpose only.
func main() {
f := timeflake.Random()

Expand All @@ -22,21 +24,21 @@ func main() {
g1 := timeflake.NewValues(f.Timestamp(), nil)

h := timeflake.FromValues(g)
i := timeflake.FromValues(g1)
h1 := timeflake.FromValues(g1)

c.Log()
f.Log()
d.Log()
e.Log()
h.Log()
i.Log()
h1.Log()

fmt.Println("===Timeflake Comparison===")
fmt.Printf("c == f => %v\n", reflect.DeepEqual(c, f))
fmt.Printf("f == d => %v\n", reflect.DeepEqual(f, d))
fmt.Printf("d == e => %v\n", reflect.DeepEqual(d, e))
fmt.Printf("e == h => %v\n", reflect.DeepEqual(e, h))
fmt.Printf("h == i => %v (expect false)\n", reflect.DeepEqual(h, i))
fmt.Printf("h == h1 => %v (expect false)\n", reflect.DeepEqual(h, h1))
}

func init() {
Expand Down
1 change: 1 addition & 0 deletions pkg/timeflake/timeflake.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func MaxTimeflake() *big.Int {
if errMaxTimeflake != nil {
fmt.Println("binary.Write failed:", errMaxTimeflake)
}

return MaxTimeflake
}

Expand Down
16 changes: 16 additions & 0 deletions test/internal/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ func TestBigIntToASCII(t *testing.T) {
if res2 != "059aa2a89" {
t.Errorf("expected '059aa2a89' got '%s'", res2)
}


b3 := big.NewInt(0)
res3, _ := utils.BigIntToASCII(b3, alphabets.HEX, 5)

if res3 != "0" {
t.Errorf("expected '0' got '%s'", res3)
}
}

func TestASCIIToBigInt(t *testing.T) {
Expand All @@ -34,6 +42,14 @@ func TestASCIIToBigInt(t *testing.T) {
if a.Cmp(b) != 0 {
t.Error("failed")
}

b1 := utils.ASCIIToBigInt("0", alphabets.BASE62)

a1 := big.NewInt(0)

if a1.Cmp(b1) != 0 {
t.Error("failed")
}
}

func TestFillString(t *testing.T) {
Expand Down
25 changes: 21 additions & 4 deletions test/pkg/timeflake/timeflake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
"github.com/gioni06/go-timeflake/pkg/timeflake"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

func bigFromString(s string, base int) (*big.Int, error) {
buf := new(bytes.Buffer)
var b = big.NewInt(0)
Expand All @@ -29,6 +25,8 @@ func bigFromString(s string, base int) (*big.Int, error) {
return b, nil
}

// Run some sanity checks that ensure the overall correctness
// of the created Timeflakes.
func TestTimeflake(t *testing.T) {
now := (time.Now()).Unix()
for range make([]int, 1000) {
Expand All @@ -40,10 +38,12 @@ func TestTimeflake(t *testing.T) {
isTimeflakeGTEZero := zero.Cmp(&f.Int) <= 0
isTimeflakeLTEMaxTimeflake := max.Cmp(&f.Int) >= 0

// 0 < Timeflake integer value (big.Int) < MaxTimeflake
if !(isTimeflakeGTEZero && isTimeflakeLTEMaxTimeflake) {
t.Error("timeflake out of bounds")
}

// Proof that Timeflakes are not capable of time travel into the future. Great!
if !(now <= f.Timestamp()) {
t.Error("timeflake timestamp is greater then expected")
}
Expand All @@ -59,6 +59,7 @@ func TestTimeflake(t *testing.T) {
isTimeflakeGTEZero = zero.Cmp(r) <= 0
isTimeflakeLTEMaxTimeflake = max.Cmp(r) >= 0

// 0 < Timeflake random bits < MaxRandom
if !(isTimeflakeGTEZero && isTimeflakeLTEMaxTimeflake) {
t.Error("timeflake random part out of bounds")
}
Expand All @@ -72,12 +73,24 @@ func TestTimeflake(t *testing.T) {
isTimeflakeGTEZero = zero.Cmp(flakeTS) <= 0
isTimeflakeLTEMaxTimeflake = max.Cmp(flakeTS) >= 0

// We know that a Timeflake can not travel into the future.
// This makes sure, that it can not travel past the boundaries of the Unix
// timestamp.
// @Todo - Check this line before January 19, 2038
if !(isTimeflakeGTEZero && isTimeflakeLTEMaxTimeflake) {
t.Error("timeflake timestamp part out of bounds")
}
}
}

// Test the randomness of created Timeflakes.
// This is achieved by creating 1.000,000 concurrent jobs that create
// Timeflakes. The base62 values are used as keys in a hashmap.
// If a key is already present, this test will fail.
//
// Test takes about 2.5sec on a recent MacBook. Don't hesitate to
// run a billion jobs, but don't forget to get yourself a cup of coffee
// before you start!
func TestRandomnessOfTimeflakes(t *testing.T) {
const numJobs = 1e6
jobs := make(chan int, numJobs)
Expand Down Expand Up @@ -110,3 +123,7 @@ func TestRandomnessOfTimeflakes(t *testing.T) {
}
}
}

func init() {
rand.Seed(time.Now().UnixNano())
}

0 comments on commit 6b512ff

Please sign in to comment.