-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add tests for ioutil and refactor existing
Signed-off-by: Noble Mittal <[email protected]>
- Loading branch information
1 parent
8f087d9
commit d19f2ad
Showing
5 changed files
with
190 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters