Skip to content

Commit

Permalink
Update client readme example to compile with previous API changes (#607)
Browse files Browse the repository at this point in the history
* Update client readme example to compile with previous API changes

* More cleanup
  • Loading branch information
abecevello authored Dec 16, 2024
1 parent 85cd59a commit 754b6b6
Showing 1 changed file with 9 additions and 22 deletions.
31 changes: 9 additions & 22 deletions client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ client api has changed:
down a proxy.
- `proxy.ToxicsUpstream` and `proxy.ToxicsDownstream` have been merged into a
single `ActiveToxics` list.
- `proxy.Toxics()`` no longer requires a direction to be specified, and will
- `proxy.Toxics()` no longer requires a direction to be specified, and will
return toxics for both directions.
- `proxy.SetToxic()` has been replaced by `proxy.AddToxic()`,
`proxy.UpdateToxic()`, and `proxy.RemoveToxic()`.
Expand Down Expand Up @@ -91,21 +91,19 @@ proxy.Delete()

```go
import (
"net/http"
"testing"
"time"

toxiproxy "github.com/Shopify/toxiproxy/v2/client"
"github.com/garyburd/redigo/redis"
"github.com/gomodule/redigo/redis"
)

var toxiClient *toxiproxy.Client
var proxies map[string]*toxiproxy.Proxy

func init() {
var err error
toxiClient = toxiproxy.NewClient("localhost:8474")
proxies, err = toxiClient.Populate([]toxiproxy.Proxy{{
_, err = toxiClient.Populate([]toxiproxy.Proxy{{
Name: "redis",
Listen: "localhost:26379",
Upstream: "localhost:6379",
Expand All @@ -119,8 +117,9 @@ func init() {
}

func TestRedisBackendDown(t *testing.T) {
proxies["redis"].Disable()
defer proxies["redis"].Enable()
var proxy, _ = toxiClient.Proxy("redis")
proxy.Disable()
defer proxy.Enable()

// Test that redis is down
_, err := redis.Dial("tcp", ":26379")
Expand All @@ -130,10 +129,11 @@ func TestRedisBackendDown(t *testing.T) {
}

func TestRedisBackendSlow(t *testing.T) {
proxies["redis"].AddToxic("", "latency", "", 1, toxiproxy.Attributes{
var proxy, _ = toxiClient.Proxy("redis")
proxy.AddToxic("", "latency", "", 1, toxiproxy.Attributes{
"latency": 1000,
})
defer proxies["redis"].RemoveToxic("latency_downstream")
defer proxy.RemoveToxic("latency_downstream")

// Test that redis is slow
start := time.Now()
Expand All @@ -149,17 +149,4 @@ func TestRedisBackendSlow(t *testing.T) {
t.Fatal("Redis command did not take long enough:", time.Since(start))
}
}

func TestEphemeralProxy(t *testing.T) {
proxy, _ := toxiClient.CreateProxy("test", "", "google.com:80")
defer proxy.Delete()

// Test connection through proxy.Listen
resp, err := http.Get("http://" + proxy.Listen)
if err != nil {
t.Fatal(err)
} else if resp.StatusCode != 200 {
t.Fatal("Proxy to google failed:", resp.StatusCode)
}
}
```

0 comments on commit 754b6b6

Please sign in to comment.