Skip to content

Commit

Permalink
Handle toxicity of -1 in go client
Browse files Browse the repository at this point in the history
Fix data race on go1.6
  • Loading branch information
xthexder authored and Jacob Wirth committed Mar 29, 2016
1 parent 7ca1d24 commit 6fa6fdd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
15 changes: 13 additions & 2 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func TestUpdateToxics(t *testing.T) {
t.Fatal("Unable to create proxy:", err)
}

latency, err := testProxy.AddToxic("", "latency", "downstream", 1, tclient.Attributes{
latency, err := testProxy.AddToxic("", "latency", "downstream", -1, tclient.Attributes{
"latency": 100,
"jitter": 10,
})
Expand All @@ -609,13 +609,24 @@ func TestUpdateToxics(t *testing.T) {
t.Fatal("Latency toxic did not get updated with the correct settings:", latency)
}

latency, err = testProxy.UpdateToxic("latency_downstream", -1, tclient.Attributes{
"latency": 500,
})
if err != nil {
t.Fatal("Error setting toxic:", err)
}

if latency.Toxicity != 0.5 || latency.Attributes["latency"] != 500.0 || latency.Attributes["jitter"] != 10.0 {
t.Fatal("Latency toxic did not get updated with the correct settings:", latency)
}

toxics, err := testProxy.Toxics()
if err != nil {
t.Fatal("Error returning toxics:", err)
}

toxic := AssertToxicExists(t, toxics, "latency_downstream", "latency", "downstream", true)
if toxic.Toxicity != 0.5 || toxic.Attributes["latency"] != 1000.0 || toxic.Attributes["jitter"] != 10.0 {
if toxic.Toxicity != 0.5 || toxic.Attributes["latency"] != 500.0 || toxic.Attributes["jitter"] != 10.0 {
t.Fatal("Toxic was not read back correctly:", toxic)
}
})
Expand Down
11 changes: 10 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ func (proxy *Proxy) Toxics() (Toxics, error) {
// See https://github.com/Shopify/toxiproxy#toxics for a list of all Toxic types.
func (proxy *Proxy) AddToxic(name, typeName, stream string, toxicity float32, attrs Attributes) (*Toxic, error) {
toxic := Toxic{name, typeName, stream, toxicity, attrs}
if toxic.Toxicity == -1 {
toxic.Toxicity = 1 // Just to be consistent with a toxicity of -1 using the default
}

request, err := json.Marshal(&toxic)
if err != nil {
Expand All @@ -266,8 +269,14 @@ func (proxy *Proxy) AddToxic(name, typeName, stream string, toxicity float32, at
}

// UpdateToxic sets the parameters for an existing toxic with the given name.
// If toxicity is set to -1, the current value will be used.
func (proxy *Proxy) UpdateToxic(name string, toxicity float32, attrs Attributes) (*Toxic, error) {
toxic := Toxic{Toxicity: toxicity, Attributes: attrs}
toxic := map[string]interface{}{
"attributes": attrs,
}
if toxicity != -1 {
toxic["toxicity"] = toxicity
}
request, err := json.Marshal(&toxic)
if err != nil {
return nil, err
Expand Down
14 changes: 6 additions & 8 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ func (link *ToxicLink) Start(name string, source io.Reader, dest io.WriteCloser)
bytes, err := io.Copy(link.input, source)
if err != nil {
logrus.WithFields(logrus.Fields{
"name": link.proxy.Name,
"upstream": link.proxy.Upstream,
"bytes": bytes,
"err": err,
"name": link.proxy.Name,
"bytes": bytes,
"err": err,
}).Warn("Source terminated")
}
link.input.Close()
Expand All @@ -73,10 +72,9 @@ func (link *ToxicLink) Start(name string, source io.Reader, dest io.WriteCloser)
bytes, err := io.Copy(dest, link.output)
if err != nil {
logrus.WithFields(logrus.Fields{
"name": link.proxy.Name,
"upstream": link.proxy.Upstream,
"bytes": bytes,
"err": err,
"name": link.proxy.Name,
"bytes": bytes,
"err": err,
}).Warn("Destination terminated")
}
dest.Close()
Expand Down
4 changes: 1 addition & 3 deletions toxic_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ func (c *ToxicCollection) UpdateToxicJson(name string, data io.Reader) (*toxics.
if err != nil {
return nil, joinError(err, ErrBadRequestBody)
}
if attrs.Toxicity != -1 {
toxic.Toxicity = attrs.Toxicity
}
toxic.Toxicity = attrs.Toxicity

c.chainUpdateToxic(toxic)
return toxic, nil
Expand Down

0 comments on commit 6fa6fdd

Please sign in to comment.