Skip to content

Commit

Permalink
Remove use of io/ioutil
Browse files Browse the repository at this point in the history
io/ioutil was deprecated in go 1.16,
https://pkg.go.dev/io/ioutil
  • Loading branch information
serprex committed Jan 11, 2024
1 parent 7576b14 commit e6875ef
Show file tree
Hide file tree
Showing 21 changed files with 47 additions and 60 deletions.
3 changes: 1 addition & 2 deletions arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"bufio"
"encoding/json"
"io"
"io/ioutil"

"github.com/uber/tchannel-go/internal/argreader"
)
Expand Down Expand Up @@ -86,7 +85,7 @@ func (r ArgReadHelper) read(f func() error) error {
func (r ArgReadHelper) Read(bs *[]byte) error {
return r.read(func() error {
var err error
*bs, err = ioutil.ReadAll(r.reader)
*bs, err = io.ReadAll(r.reader)
return err
})
}
Expand Down
3 changes: 1 addition & 2 deletions arguments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package tchannel
import (
"bytes"
"io"
"io/ioutil"
"strings"
"testing"

Expand Down Expand Up @@ -79,7 +78,7 @@ func TestReadNotEmpty(t *testing.T) {
r := bytes.NewReader([]byte("{}" + strings.Repeat("{}\n", 10000)))

var data map[string]interface{}
reader := NewArgReader(ioutil.NopCloser(r), nil)
reader := NewArgReader(io.NopCloser(r), nil)
require.Error(t, reader.ReadJSON(&data), "Read should fail due to extra bytes")
}

Expand Down
3 changes: 1 addition & 2 deletions benchmark/build_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package benchmark

import (
"io/ioutil"
"os"
"os/exec"
"sync"
Expand Down Expand Up @@ -60,7 +59,7 @@ func (m *buildManager) GoBinary(mainFile string) (string, error) {
}

func (b *build) Build() {
tempFile, err := ioutil.TempFile("", "bench")
tempFile, err := os.CreateTemp("", "bench")
if err != nil {
panic("Failed to create temp file: " + err.Error())
}
Expand Down
3 changes: 1 addition & 2 deletions benchmark/tcp_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package benchmark

import (
"io"
"io/ioutil"
"net"
"testing"

Expand Down Expand Up @@ -56,7 +55,7 @@ func benchmarkClient(b *testing.B, dst string, reqSize int) {
readerDone := make(chan struct{})
go func() {
defer close(readerDone)
n, err := io.CopyN(ioutil.Discard, conn, int64(totalExpected))
n, err := io.CopyN(io.Discard, conn, int64(totalExpected))
assert.NoError(b, err, "Expected %v response bytes, got %v", totalExpected, n)
}()

Expand Down
8 changes: 4 additions & 4 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
package tchannel

import (
"io/ioutil"
"io"
"math"
"os"
"runtime"
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestNewChannel(t *testing.T) {

func TestLoggers(t *testing.T) {
ch, err := NewChannel("svc", &ChannelOptions{
Logger: NewLogger(ioutil.Discard),
Logger: NewLogger(io.Discard),
})
require.NoError(t, err, "NewChannel failed")
defer ch.Close()
Expand All @@ -83,7 +83,7 @@ func TestLoggers(t *testing.T) {

func TestStats(t *testing.T) {
ch, err := NewChannel("svc", &ChannelOptions{
Logger: NewLogger(ioutil.Discard),
Logger: NewLogger(io.Discard),
})
require.NoError(t, err, "NewChannel failed")
defer ch.Close()
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestRelayMaxTTL(t *testing.T) {

func TestIsolatedSubChannelsDontSharePeers(t *testing.T) {
ch, err := NewChannel("svc", &ChannelOptions{
Logger: NewLogger(ioutil.Discard),
Logger: NewLogger(io.Discard),
})
require.NoError(t, err, "NewChannel failed")
defer ch.Close()
Expand Down
4 changes: 2 additions & 2 deletions conn_leak_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package tchannel_test

import (
"io/ioutil"
"io"
"runtime"
"testing"
"time"
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestPeerConnectionLeaks(t *testing.T) {

testutils.WithTestServer(t, opts, func(t testing.TB, ts *testutils.TestServer) {
s2Opts := testutils.NewOpts().SetServiceName("s2")
s2Opts.Logger = NewLogger(ioutil.Discard)
s2Opts.Logger = NewLogger(io.Discard)
s2 := ts.NewServer(s2Opts)

// Set a finalizer to detect when the connection from s1 -> s2 is freed.
Expand Down
3 changes: 1 addition & 2 deletions fragmentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package tchannel
import (
"bytes"
"io"
"io/ioutil"
"sync"
"testing"

Expand Down Expand Up @@ -300,7 +299,7 @@ func TestFragmentationChecksumMismatch(t *testing.T) {
reader, err := r.ArgReader(true /* last */)
assert.NoError(t, err)

_, err = io.Copy(ioutil.Discard, reader)
_, err = io.Copy(io.Discard, reader)
assert.Equal(t, errMismatchedChecksums, err)
}

Expand Down
2 changes: 1 addition & 1 deletion fragmenting_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (r *fragmentingReader) Read(b []byte) (int, error) {
// There wasn't enough data in the current chunk to satisfy the
// current read. If there are more chunks in the current
// fragment, then we've reach the end of this argument. Return
// an io.EOF so functions like ioutil.ReadFully know to finish
// an io.EOF so functions like io.ReadAll know to finish
if len(r.remainingChunks) > 0 {
return totalRead, io.EOF
}
Expand Down
5 changes: 2 additions & 3 deletions http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -165,9 +164,9 @@ func makeTChanCall(t *testing.T, tchanAddr string, req *http.Request) *http.Resp
}

func compareResponseBasic(t *testing.T, testName string, resp1, resp2 *http.Response) {
resp1Body, err := ioutil.ReadAll(resp1.Body)
resp1Body, err := io.ReadAll(resp1.Body)
require.NoError(t, err, "Read response failed")
resp2Body, err := ioutil.ReadAll(resp2.Body)
resp2Body, err := io.ReadAll(resp2.Body)
require.NoError(t, err, "Read response failed")

assert.Equal(t, resp1.Status, resp2.Status, "%v: Response status mismatch", testName)
Expand Down
3 changes: 1 addition & 2 deletions hyperbahn/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package hyperbahn

import (
"encoding/json"
"io/ioutil"
"os"
"sort"
"testing"
Expand Down Expand Up @@ -92,7 +91,7 @@ func TestParseConfiguration(t *testing.T) {
for _, tt := range tests {
peerFile := ""
if tt.peersFile != "" {
f, err := ioutil.TempFile("", "hosts")
f, err := os.CreateTemp("", "hosts")
if !assert.NoError(t, err, "%v: TempFile failed", tt.name) {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions pprof/pprof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
package pprof

import (
"io/ioutil"
"io"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -52,7 +52,7 @@ func TestPProfEndpoint(t *testing.T) {
require.NoError(t, err, "ReadResponse failed")

assert.Equal(t, http.StatusOK, response.StatusCode)
body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if assert.NoError(t, err, "Read body failed") {
assert.Contains(t, string(body), "contention", "Response does not contain expected string")
}
Expand Down
4 changes: 2 additions & 2 deletions relay_messages_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package tchannel

import (
"fmt"
"io/ioutil"
"io"
"testing"
)

Expand All @@ -48,5 +48,5 @@ func BenchmarkCallReqFrame(b *testing.B) {
}
b.StopTimer()

fmt.Fprint(ioutil.Discard, service, caller, method)
fmt.Fprint(io.Discard, service, caller, method)
}
3 changes: 1 addition & 2 deletions relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"runtime"
Expand Down Expand Up @@ -2114,7 +2113,7 @@ func decodeThriftHeaders(t testing.TB, bs []byte) map[string]string {
require.NoError(t, err, "Failed to read headers")

// Ensure there are no remaining bytes left.
remaining, err := ioutil.ReadAll(r)
remaining, err := io.ReadAll(r)
require.NoError(t, err, "failed to read from arg2 reader")
assert.Empty(t, remaining, "expected no bytes after reading headers")

Expand Down
9 changes: 4 additions & 5 deletions scripts/vbumper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -63,13 +62,13 @@ func main() {
}

func updateVersion(prevVersion string) error {
versionBytes, err := ioutil.ReadFile(*_versionFile)
versionBytes, err := os.ReadFile(*_versionFile)
if err != nil {
return err
}

newContents := insertNewVersion(string(versionBytes), prevVersion, *_version)
return ioutil.WriteFile(*_versionFile, []byte(newContents), 0666)
return os.WriteFile(*_versionFile, []byte(newContents), 0666)
}

func insertNewVersion(contents, prevVersion, newVersion string) string {
Expand All @@ -81,7 +80,7 @@ func insertNewVersion(contents, prevVersion, newVersion string) string {
}

func updateChangelog() (oldVersion string, _ error) {
changelogBytes, err := ioutil.ReadFile(*_changelogFile)
changelogBytes, err := os.ReadFile(*_changelogFile)
if err != nil {
return "", err
}
Expand All @@ -100,7 +99,7 @@ func updateChangelog() (oldVersion string, _ error) {
return oldVersion, nil
}

return oldVersion, ioutil.WriteFile(*_changelogFile, []byte(newLog), 0666)
return oldVersion, os.WriteFile(*_changelogFile, []byte(newLog), 0666)
}

func insertNewChangelog(contents string) (string, string, error) {
Expand Down
7 changes: 3 additions & 4 deletions stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -219,7 +218,7 @@ func TestStreamPartialArg(t *testing.T) {
require.NoError(t, argWriter.Close(), "arg3 close failed")

// Once closed, we expect the reader to return EOF
n, err := io.Copy(ioutil.Discard, argReader)
n, err := io.Copy(io.Discard, argReader)
assert.Equal(t, int64(0), n, "arg2 reader expected to EOF after arg3 writer is closed")
assert.NoError(t, err, "Copy should not fail")
assert.NoError(t, argReader.Close(), "close arg reader failed")
Expand All @@ -234,7 +233,7 @@ func TestStreamSendError(t *testing.T) {
require.NoError(t, argWriter.Close(), "arg3 close failed")

// Now we expect an error on our next read.
_, err = ioutil.ReadAll(argReader)
_, err = io.ReadAll(argReader)
assert.Error(t, err, "ReadAll should fail")
assert.True(t, strings.Contains(err.Error(), "intentional failure"), "err %v unexpected", err)
})
Expand Down Expand Up @@ -280,7 +279,7 @@ func TestStreamCancelled(t *testing.T) {

close(cancelContext)

n, err := io.Copy(ioutil.Discard, arg3Reader)
n, err := io.Copy(io.Discard, arg3Reader)
assert.EqualValues(t, 0, n, "Read should not read any bytes after cancel")
assert.Error(t, err, "Read should fail after cancel")
assert.Error(t, arg3Reader.Close(), "reader.Close should fail after cancel")
Expand Down
3 changes: 1 addition & 2 deletions testutils/random_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package testutils
import (
"bytes"
"io"
"io/ioutil"
"testing"
)

Expand All @@ -33,7 +32,7 @@ func benchmarkRandom(b *testing.B, numBytes int) {
randCache = nil
bs = RandBytes(numBytes)
}
io.Copy(ioutil.Discard, bytes.NewReader(bs))
io.Copy(io.Discard, bytes.NewReader(bs))
}

func BenchmarkRandom256(b *testing.B) {
Expand Down
6 changes: 3 additions & 3 deletions thrift/headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package thrift

import (
"bytes"
"io/ioutil"
"io"
"testing"
"testing/iotest"

Expand Down Expand Up @@ -149,14 +149,14 @@ func TestReadHeadersLeftoverBytes(t *testing.T) {
assert.NoError(t, err, "ReadHeaders failed")
assert.Equal(t, map[string]string(nil), headers, "Headers mismatch")

leftover, err := ioutil.ReadAll(r)
leftover, err := io.ReadAll(r)
assert.NoError(t, err, "ReadAll failed")
assert.Equal(t, []byte{1, 2, 3}, leftover, "Reader consumed leftover bytes")
}

func BenchmarkWriteHeaders(b *testing.B) {
for i := 0; i < b.N; i++ {
WriteHeaders(ioutil.Discard, headers)
WriteHeaders(io.Discard, headers)
}
}

Expand Down
4 changes: 2 additions & 2 deletions thrift/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package thrift_test

import (
"bytes"
"io/ioutil"
"io"
"sync"
"testing"

Expand Down Expand Up @@ -103,7 +103,7 @@ func TestReadStruct(t *testing.T) {
// Even if there's an error, the struct will be partially filled.
assert.Equal(t, tt.s, s, "Unexpected struct")

leftover, err := ioutil.ReadAll(reader)
leftover, err := io.ReadAll(reader)
if assert.NoError(t, err, "Read leftover bytes failed") {
// ReadAll always returns a non-nil byte slice.
if tt.leftover == nil {
Expand Down
Loading

0 comments on commit e6875ef

Please sign in to comment.