Skip to content

Commit

Permalink
chore: fix some minor govet/staticheck failures
Browse files Browse the repository at this point in the history
- deprecated functions
- copylocks on range
- t.Fatal in non-test goroutines
- use bytes.Equal
- #nosec for math/crypto
  • Loading branch information
dnwe committed Apr 29, 2021
1 parent ad7d9e2 commit febff58
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ func hint(m string) {
}

func errorf(m string, args ...interface{}) error {
return cli.NewExitError(fmt.Sprintf(m, args...), 1)
return cli.Exit(fmt.Sprintf(m, args...), 1)
}

func printWidth(col string, m string, numTabs int) {
Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Proxy struct {

// NewClient creates a new client which provides the base of all communication
// with Toxiproxy. Endpoint is the address to the proxy (e.g. localhost:8474 if
// not overriden)
// not overridden)
func NewClient(endpoint string) *Client {
if strings.HasPrefix(endpoint, "https://") {
log.Fatal("the toxiproxy client does not support https")
Expand Down
20 changes: 10 additions & 10 deletions proxy_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// ProxyCollection is a collection of proxies. It's the interface for anything
// to add and remove proxies from the toxiproxy instance. It's responsibilty is
// to add and remove proxies from the toxiproxy instance. It's responsibility is
// to maintain the integrity of the proxy set, by guarding for things such as
// duplicate names.
type ProxyCollection struct {
Expand Down Expand Up @@ -79,27 +79,27 @@ func (collection *ProxyCollection) PopulateJson(data io.Reader) ([]*Proxy, error

// Check for valid input before creating any proxies
t := true
for i, p := range input {
if len(p.Name) < 1 {
for i := range input {
if len(input[i].Name) < 1 {
return nil, joinError(fmt.Errorf("name at proxy %d", i+1), ErrMissingField)
}
if len(p.Upstream) < 1 {
if len(input[i].Upstream) < 1 {
return nil, joinError(fmt.Errorf("upstream at proxy %d", i+1), ErrMissingField)
}
if p.Enabled == nil {
if input[i].Enabled == nil {
input[i].Enabled = &t
}
}

proxies := make([]*Proxy, 0, len(input))

for _, p := range input {
for i := range input {
proxy := NewProxy()
proxy.Name = p.Name
proxy.Listen = p.Listen
proxy.Upstream = p.Upstream
proxy.Name = input[i].Name
proxy.Listen = input[i].Listen
proxy.Upstream = input[i].Upstream

err = collection.AddOrReplace(proxy, *p.Enabled)
err = collection.AddOrReplace(proxy, *input[i].Enabled)
if err != nil {
break
}
Expand Down
6 changes: 4 additions & 2 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func WithTCPServer(t *testing.T, f func(string, chan []byte)) {
select {
case <-tomb.Dying():
default:
t.Fatal("Failed to accept client")
t.Error("Failed to accept client")
return
}
return
}
Expand All @@ -55,7 +56,8 @@ func WithTCPServer(t *testing.T, f func(string, chan []byte)) {

val, err := ioutil.ReadAll(src)
if err != nil {
t.Fatal("Failed to read from client")
t.Error("Failed to read from client")
return
}

response <- val
Expand Down
2 changes: 1 addition & 1 deletion stream/io_chan.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
NumDirections
)

// Stores a slice of bytes with its receive timestmap
// Stores a slice of bytes with its receive timestamp
type StreamChunk struct {
Data []byte
Timestamp time.Time
Expand Down
5 changes: 4 additions & 1 deletion toxics/bandwidth.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func (t *BandwidthToxic) Pipe(stub *ToxicStub) {
for int64(len(p.Data)) > t.Rate*100 {
select {
case <-time.After(100 * time.Millisecond):
stub.Output <- &stream.StreamChunk{p.Data[:t.Rate*100], p.Timestamp}
stub.Output <- &stream.StreamChunk{
Data: p.Data[:t.Rate*100],
Timestamp: p.Timestamp,
}
p.Data = p.Data[t.Rate*100:]
sleep -= 100 * time.Millisecond
case <-stub.Interrupt:
Expand Down
2 changes: 1 addition & 1 deletion toxics/bandwidth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestBandwidthToxic(t *testing.T) {
_, err = io.ReadAtLeast(serverConn, buf2, len(buf2))
if err != nil {
t.Errorf("Proxy read failed: %v", err)
} else if bytes.Compare(buf, buf2) != 0 {
} else if !bytes.Equal(buf, buf2) {
t.Errorf("Server did not read correct buffer from client!")
}

Expand Down
3 changes: 2 additions & 1 deletion toxics/latency.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ func (t *LatencyToxic) GetBufferSize() int {
func (t *LatencyToxic) delay() time.Duration {
// Delay = t.Latency +/- t.Jitter
delay := t.Latency
jitter := int64(t.Jitter)
jitter := t.Jitter
if jitter > 0 {
//#nosec
delay += rand.Int63n(jitter*2) - jitter
}
return time.Duration(delay) * time.Millisecond
Expand Down
3 changes: 2 additions & 1 deletion toxics/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// The SlicerToxic slices data into multiple smaller packets
// to simulate real-world TCP behaviour.
// to simulate real-world TCP behavior.
type SlicerToxic struct {
// Average number of bytes to slice at
AverageSize int `json:"average_size"`
Expand Down Expand Up @@ -39,6 +39,7 @@ func (t *SlicerToxic) chunk(start int, end int) []int {

// +1 in the size variation to offset favoring of smaller
// numbers by integer division
//#nosec
mid := start + (end-start)/2 + (rand.Intn(t.SizeVariation*2) - t.SizeVariation) + rand.Intn(1)
left := t.chunk(start, mid)
right := t.chunk(mid, end)
Expand Down
2 changes: 1 addition & 1 deletion toxics/slicer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ L:
if reads < 480/2 || reads > 480/2+480 {
t.Errorf("Expected to read about 480 times, but read %d times.", reads)
}
if bytes.Compare(buf, data) != 0 {
if !bytes.Equal(buf, data) {
t.Errorf("Server did not read correct buffer from client!")
}
}
1 change: 1 addition & 0 deletions toxics/toxic.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func NewToxicStub(input <-chan *stream.StreamChunk, output chan<- *stream.Stream
func (s *ToxicStub) Run(toxic *ToxicWrapper) {
s.running = make(chan struct{})
defer close(s.running)
//#nosec
if rand.Float32() < toxic.Toxicity {
toxic.Pipe(s)
} else {
Expand Down
3 changes: 2 additions & 1 deletion toxics/toxic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func WithEchoServer(t *testing.T, f func(string, chan []byte)) {
select {
case <-tomb.Dying():
default:
t.Fatal("Failed to accept client")
t.Error("Failed to accept client")
return
}
return
}
Expand Down

0 comments on commit febff58

Please sign in to comment.