From d19f2ad5f6682427dbb58077dd3ed3b25ec67f46 Mon Sep 17 00:00:00 2001 From: Noble Mittal Date: Wed, 8 May 2024 16:06:54 +0530 Subject: [PATCH] test: Add tests for ioutil and refactor existing Signed-off-by: Noble Mittal --- go/hack/hack_test.go | 27 ++++++++++++------- go/ioutil/meter_test.go | 58 ++++++++++++++++++++++++++++++++++++++++ go/ioutil/reader_test.go | 55 +++++++++++++++++++++++++++++++++++++ go/ioutil/writer_test.go | 55 +++++++++++++++++++++++++++++++++++++ go/jsonutil/json_test.go | 10 +++---- 5 files changed, 190 insertions(+), 15 deletions(-) create mode 100644 go/ioutil/meter_test.go create mode 100644 go/ioutil/reader_test.go create mode 100644 go/ioutil/writer_test.go diff --git a/go/hack/hack_test.go b/go/hack/hack_test.go index cf8b6423aff..9f71d82cf11 100644 --- a/go/hack/hack_test.go +++ b/go/hack/hack_test.go @@ -20,21 +20,30 @@ package hack import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestByteToString(t *testing.T) { v1 := []byte("1234") - if s := String(v1); s != "1234" { - t.Errorf("String(\"1234\"): %q, want 1234", s) - } + s := String(v1) + assert.Equal(t, "1234", s) v1 = []byte("") - if s := String(v1); s != "" { - t.Errorf("String(\"\"): %q, want empty", s) - } + s = String(v1) + assert.Equal(t, "", s) v1 = nil - if s := String(v1); s != "" { - t.Errorf("String(\"\"): %q, want empty", s) - } + s = String(v1) + assert.Equal(t, "", s) +} + +func TestStringToByte(t *testing.T) { + s := "1234" + b := StringBytes(s) + assert.Equal(t, []byte("1234"), b) + + s = "" + b = StringBytes(s) + assert.Nil(t, b) } diff --git a/go/ioutil/meter_test.go b/go/ioutil/meter_test.go new file mode 100644 index 00000000000..10e2b83b485 --- /dev/null +++ b/go/ioutil/meter_test.go @@ -0,0 +1,58 @@ +/* +Copyright 2024 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package ioutil + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +var ( + calledBytes int + calledDuration time.Duration +) + +func testfn(b int, d time.Duration) { + calledBytes = b + calledDuration = d +} + +func TestMeter(t *testing.T) { + tm := meter{ + fs: []func(b int, d time.Duration){testfn}, + bytes: 123, + duration: time.Second, + } + + assert.Equal(t, int64(123), tm.Bytes()) + assert.Equal(t, time.Second, tm.Duration()) + + tf := func(p []byte) (int, error) { + return 1, nil + } + + b, err := tm.measure(tf, []byte("")) + wantDuration := time.Second + calledDuration + wantBytes := int64(123) + int64(calledBytes) + + assert.NoError(t, err) + assert.Equal(t, 1, b) + assert.Equal(t, wantDuration, tm.duration) + assert.Equal(t, wantBytes, tm.bytes) +} diff --git a/go/ioutil/reader_test.go b/go/ioutil/reader_test.go new file mode 100644 index 00000000000..ea284f13225 --- /dev/null +++ b/go/ioutil/reader_test.go @@ -0,0 +1,55 @@ +/* +Copyright 2024 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package ioutil + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +type mockReadCloser struct{} + +func (*mockReadCloser) Read([]byte) (int, error) { + return 2, nil +} + +func (*mockReadCloser) Close() error { + return nil +} + +type mockRead struct{} + +func (*mockRead) Read([]byte) (int, error) { + return 3, nil +} + +func TestMeteredReader(t *testing.T) { + mrc := NewMeteredReadCloser(&mockReadCloser{}, testfn) + n, err := mrc.Read([]byte("")) + assert.NoError(t, err) + assert.Equal(t, 2, n) + assert.Equal(t, 2, calledBytes) + assert.Equal(t, calledDuration, mrc.Duration()) + + mr := NewMeteredReader(&mockRead{}, testfn) + n, err = mr.Read([]byte("")) + assert.NoError(t, err) + assert.Equal(t, 3, n) + assert.Equal(t, 3, calledBytes) + assert.Equal(t, calledDuration, mr.Duration()) +} diff --git a/go/ioutil/writer_test.go b/go/ioutil/writer_test.go new file mode 100644 index 00000000000..b21fb34b397 --- /dev/null +++ b/go/ioutil/writer_test.go @@ -0,0 +1,55 @@ +/* +Copyright 2024 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package ioutil + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +type mockWriteCloser struct{} + +func (*mockWriteCloser) Write([]byte) (int, error) { + return 2, nil +} + +func (*mockWriteCloser) Close() error { + return nil +} + +type mockWrite struct{} + +func (*mockWrite) Write([]byte) (int, error) { + return 3, nil +} + +func TestMeteredWriter(t *testing.T) { + mwc := NewMeteredWriteCloser(&mockWriteCloser{}, testfn) + n, err := mwc.Write([]byte("")) + assert.NoError(t, err) + assert.Equal(t, 2, n) + assert.Equal(t, 2, calledBytes) + assert.Equal(t, calledDuration, mwc.Duration()) + + mw := NewMeteredWriter(&mockWrite{}, testfn) + n, err = mw.Write([]byte("")) + assert.NoError(t, err) + assert.Equal(t, 3, n) + assert.Equal(t, 3, calledBytes) + assert.Equal(t, calledDuration, mw.Duration()) +} diff --git a/go/jsonutil/json_test.go b/go/jsonutil/json_test.go index 50488dd3f3c..00d53d92021 100644 --- a/go/jsonutil/json_test.go +++ b/go/jsonutil/json_test.go @@ -18,6 +18,8 @@ package jsonutil import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestMarshalNoEscape(t *testing.T) { @@ -53,9 +55,7 @@ func TestMarshalNoEscape(t *testing.T) { t.Run(c.name, func(t *testing.T) { json, _ := MarshalNoEscape(c.v) sjson := string(json[:len(json)-1]) - if sjson != c.expected { - t.Errorf("expected: %v, got: %v", c.expected, sjson) - } + assert.Equal(t, c.expected, sjson) }) } } @@ -97,9 +97,7 @@ func TestMarshalIndentNoEscape(t *testing.T) { t.Run(c.name, func(t *testing.T) { json, _ := MarshalIndentNoEscape(c.v, c.prefix, c.ident) sjson := string(json[:len(json)-1]) - if sjson != c.expected { - t.Errorf("expected: %v, got: %v", c.expected, sjson) - } + assert.Equal(t, c.expected, sjson) }) } }