From 9addbb57f0554e64cfcb0cb258979bc1fe983de6 Mon Sep 17 00:00:00 2001 From: Tao Jiang Date: Tue, 4 Sep 2018 09:09:06 -0700 Subject: [PATCH] KCL: Fix random number generator Fix the random number generator by adding seed. https://stackoverflow.com/questions/12321133/golang-random-number-generator-how-to-seed-properly Jira CNA-1119 Change-Id: Idfe23d84f31a47dcf43c8025632ff6f115614d34 --- clientlibrary/utils/random.go | 2 ++ clientlibrary/utils/random_test.go | 50 ++++++++++++++++++++++++++++ clientlibrary/worker/checkpointer.go | 3 +- 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 clientlibrary/utils/random_test.go diff --git a/clientlibrary/utils/random.go b/clientlibrary/utils/random.go index 4a3059a..ef9dbc4 100644 --- a/clientlibrary/utils/random.go +++ b/clientlibrary/utils/random.go @@ -20,6 +20,7 @@ package utils import ( "math/rand" + "time" ) const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" @@ -31,6 +32,7 @@ const ( func RandStringBytesMaskImpr(n int) string { b := make([]byte, n) + rand.Seed(time.Now().UTC().UnixNano()) // A rand.Int63() generates 63 random bits, enough for letterIdxMax letters! for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; { if remain == 0 { diff --git a/clientlibrary/utils/random_test.go b/clientlibrary/utils/random_test.go new file mode 100644 index 0000000..c63b21b --- /dev/null +++ b/clientlibrary/utils/random_test.go @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2018 VMware, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial + * portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package utils + +import ( + "fmt" + "math/rand" + "testing" + "time" +) + +func TestRandom(t *testing.T) { + for i := 0; i < 10; i++ { + s1 := RandStringBytesMaskImpr(10) + s2 := RandStringBytesMaskImpr(10) + if s1 == s2 { + t.Fatalf("failed in generating random string. s1: %s, s2: %s", s1, s2) + } + } +} + +func TestRandomNum(t *testing.T) { + rand.Seed(time.Now().UTC().UnixNano()) + + for i := 0; i < 10; i++ { + s1 := rand.Int63() + s2 := rand.Int63() + if s1 == s2 { + t.Fatalf("failed in generating random string. s1: %d, s2: %d", s1, s2) + } + fmt.Println(s1) + fmt.Println(s2) + } +} diff --git a/clientlibrary/worker/checkpointer.go b/clientlibrary/worker/checkpointer.go index 7513af6..ea0a130 100644 --- a/clientlibrary/worker/checkpointer.go +++ b/clientlibrary/worker/checkpointer.go @@ -335,10 +335,9 @@ func (checkpointer *DynamoCheckpoint) getItem(shardID string) (map[string]*dynam } func (checkpointer *DynamoCheckpoint) removeItem(shardID string) error { - var item *dynamodb.DeleteItemOutput err := try.Do(func(attempt int) (bool, error) { var err error - item, err = checkpointer.svc.DeleteItem(&dynamodb.DeleteItemInput{ + _, err = checkpointer.svc.DeleteItem(&dynamodb.DeleteItemInput{ TableName: aws.String(checkpointer.TableName), Key: map[string]*dynamodb.AttributeValue{ LEASE_KEY_KEY: {