From 6b50c8dcf2eebd8da838a985ed5bbd01d71338d9 Mon Sep 17 00:00:00 2001 From: Vladimir Domnich Date: Sun, 18 Sep 2022 19:00:33 +0400 Subject: [PATCH] [#17] Add unit tests for the generator Signed-off-by: Vladimir Domnich --- go.mod | 4 +++ go.sum | 4 +++ internal/datagen/generator.go | 3 ++ internal/datagen/generator_test.go | 56 ++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 internal/datagen/generator_test.go diff --git a/go.mod b/go.mod index 531e504..2d45414 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/nspcc-dev/neo-go v0.98.2 github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.3.0.20220504192402-12ea1e8d740f github.com/nspcc-dev/tzhash v1.5.2 + github.com/stretchr/testify v1.7.1 go.k6.io/k6 v0.38.2 ) @@ -31,6 +32,7 @@ require ( github.com/aws/aws-sdk-go-v2/service/sts v1.16.4 // indirect github.com/aws/smithy-go v1.11.2 // indirect github.com/btcsuite/btcd v0.22.0-beta // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91 // indirect github.com/fatih/color v1.13.0 // indirect github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible // indirect @@ -47,6 +49,7 @@ require ( github.com/nxadm/tail v1.4.8 // indirect github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e // indirect github.com/sirupsen/logrus v1.8.1 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect @@ -61,4 +64,5 @@ require ( google.golang.org/grpc v1.45.0 // indirect google.golang.org/protobuf v1.27.1 // indirect gopkg.in/guregu/null.v3 v3.3.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/go.sum b/go.sum index 3f3ff6d..16ebeb4 100644 --- a/go.sum +++ b/go.sum @@ -207,9 +207,11 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxv github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= @@ -315,6 +317,7 @@ github.com/prometheus/procfs v0.0.5/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDa github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= +github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= @@ -519,6 +522,7 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= diff --git a/internal/datagen/generator.go b/internal/datagen/generator.go index fc6699e..9801c7e 100644 --- a/internal/datagen/generator.go +++ b/internal/datagen/generator.go @@ -40,6 +40,9 @@ func init() { } func NewGenerator(vu modules.VU, size int) Generator { + if size <= 0 { + panic("size should be positive") + } return Generator{vu: vu, size: size, buf: nil, offset: 0} } diff --git a/internal/datagen/generator_test.go b/internal/datagen/generator_test.go new file mode 100644 index 0000000..755c5bb --- /dev/null +++ b/internal/datagen/generator_test.go @@ -0,0 +1,56 @@ +package datagen + +import ( + "testing" + + "github.com/dop251/goja" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.k6.io/k6/js/modulestest" +) + +func TestGenerator(t *testing.T) { + vu := &modulestest.VU{ + RuntimeField: goja.New(), + } + + t.Run("fails on negative size", func(t *testing.T) { + require.Panics(t, func() { + _ = NewGenerator(vu, -1) + }) + }) + + t.Run("fails on zero size", func(t *testing.T) { + require.Panics(t, func() { + _ = NewGenerator(vu, 0) + }) + }) + + t.Run("creates slice of specified size", func(t *testing.T) { + size := 10 + g := NewGenerator(vu, size) + slice := g.nextSlice() + require.Len(t, slice, size) + }) + + t.Run("creates a different slice on each call", func(t *testing.T) { + g := NewGenerator(vu, 1000) + slice1 := g.nextSlice() + slice2 := g.nextSlice() + // Each slice should be unique (assuming that 1000 random bytes will never coincide + // to be identical) + assert.NotEqual(t, slice1, slice2) + }) + + t.Run("keeps generating slices after consuming entire tail", func(t *testing.T) { + g := NewGenerator(vu, 1000) + initialSlice := g.nextSlice() + for i := 0; i < TailSize; i++ { + g.nextSlice() + } + // After we looped around our tail and returned to the beginning we should have a + // unique slice - not the same as in the beginning + sliceAfterTail := g.nextSlice() + assert.NotEqual(t, initialSlice, sliceAfterTail) + }) +}