From 6fa6fddae07f7e21dbae02e0f0ec4ea21e0919d6 Mon Sep 17 00:00:00 2001 From: Jacob Wirth Date: Mon, 21 Mar 2016 02:34:21 -0400 Subject: [PATCH] Handle toxicity of -1 in go client Fix data race on go1.6 --- api_test.go | 15 +++++++++++++-- client/client.go | 11 ++++++++++- link.go | 14 ++++++-------- toxic_collection.go | 4 +--- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/api_test.go b/api_test.go index 4cb53288..3770c04b 100644 --- a/api_test.go +++ b/api_test.go @@ -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, }) @@ -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) } }) diff --git a/client/client.go b/client/client.go index 84cef07d..664e599d 100644 --- a/client/client.go +++ b/client/client.go @@ -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 { @@ -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 diff --git a/link.go b/link.go index ca7e713d..2c89bf21 100644 --- a/link.go +++ b/link.go @@ -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() @@ -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() diff --git a/toxic_collection.go b/toxic_collection.go index 4ac9e667..c80c4e3d 100644 --- a/toxic_collection.go +++ b/toxic_collection.go @@ -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