You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2023/05/22 12:39:55 UTC

[plc4x] 01/03: test(plc4go/spi): add test for id generator

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit a2a6ca25826810b15371a7f47192112287999cc0
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Mon May 22 13:56:43 2023 +0200

    test(plc4go/spi): add test for id generator
---
 plc4go/spi/utils/IdGenerator.go                    |  7 ++++-
 .../utils/{IdGenerator.go => IdGenerator_test.go}  | 33 ++++++++++++++++++----
 2 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/plc4go/spi/utils/IdGenerator.go b/plc4go/spi/utils/IdGenerator.go
index f2ba78ca50..a27f4b0373 100644
--- a/plc4go/spi/utils/IdGenerator.go
+++ b/plc4go/spi/utils/IdGenerator.go
@@ -22,10 +22,15 @@ package utils
 import (
 	"encoding/hex"
 	"math/rand"
+
+	"github.com/rs/zerolog/log"
 )
 
+var randomByteFiller = rand.Read
+
 func GenerateId(numBytes int) string {
 	transactionIdBytes := make([]byte, numBytes)
-	rand.Read(transactionIdBytes)
+	n, err := randomByteFiller(transactionIdBytes)
+	log.Trace().Err(err).Msgf("Read %d bytes", n)
 	return hex.EncodeToString(transactionIdBytes)
 }
diff --git a/plc4go/spi/utils/IdGenerator.go b/plc4go/spi/utils/IdGenerator_test.go
similarity index 58%
copy from plc4go/spi/utils/IdGenerator.go
copy to plc4go/spi/utils/IdGenerator_test.go
index f2ba78ca50..514d930dee 100644
--- a/plc4go/spi/utils/IdGenerator.go
+++ b/plc4go/spi/utils/IdGenerator_test.go
@@ -20,12 +20,33 @@
 package utils
 
 import (
-	"encoding/hex"
-	"math/rand"
+	"github.com/stretchr/testify/assert"
+	"testing"
 )
 
-func GenerateId(numBytes int) string {
-	transactionIdBytes := make([]byte, numBytes)
-	rand.Read(transactionIdBytes)
-	return hex.EncodeToString(transactionIdBytes)
+func TestGenerateId(t *testing.T) {
+	type args struct {
+		numBytes int
+	}
+	tests := []struct {
+		name string
+		args args
+		want string
+	}{
+		{
+			name: "generate it",
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			oldRandomByteFiller := randomByteFiller
+			t.Cleanup(func() {
+				randomByteFiller = oldRandomByteFiller
+			})
+			randomByteFiller = func(_ []byte) (n int, err error) {
+				return 0, nil
+			}
+			assert.Equalf(t, tt.want, GenerateId(tt.args.numBytes), "GenerateId(%v)", tt.args.numBytes)
+		})
+	}
 }