Shopify / toxiproxy

:alarm_clock: :fire: A TCP proxy to simulate network and system conditions for chaos and resiliency testing
https://github.com/shopify/toxiproxy
MIT License
10.88k stars 452 forks source link

Better Go api #56

Open sirupsen opened 9 years ago

sirupsen commented 9 years ago

The current Go API is pretty low-level, something with closures or a context object would be great.

ihsw commented 9 years ago

I'm interested in having a crack at this, but I'm only minimally familiar with Go.

sirupsen commented 9 years ago

What I'm doing in my current project is something like this:

func WithProxy(t *testing.T, proxy *toxiproxy.Proxy, f func(proxy *toxiproxy.Proxy)) {                                                                                                                                                                                                     
  proxy.Enabled = true                                                                                                                                                                                                                                                                     

  client := toxiproxy.NewClient("http://localhost:8474")                                                                                                                                                                                                                                   
  proxy = client.NewProxy(proxy)                                                                                                                                                                                                                                                           

  err := proxy.Create()                                                                                                                                                                                                                                                                    
  if err != nil {                                                                                                                                                                                                                                                                          
    t.Error("Failed to create proxy: ", err)                                                                                                                                                                                                                                               
  }                                                                                                                                                                                                                                                                                        
  defer proxy.Delete()                                                                                                                                                                                                                                                                     

  f(proxy)                                                                                                                                                                                                                                                                                 
}                                                                                                                                                                                                                                                                                          

func WithProxyDown(t *testing.T, proxy *toxiproxy.Proxy, f func()) {                                                                                                                                                                                                                       
  err := proxy.Delete()                                                                                                                                                                                                                                                                    
  if err != nil {                                                                                                                                                                                                                                                                          
    t.Error("Failed to delete proxy: ", err)                                                                                                                                                                                                                                               
  }                                                                                                                                                                                                                                                                                        
  defer proxy.Create()                                                                                                                                                                                                                                                                     

  f()                                                                                                                                                                                                                                                                                      
}