toxiproxy icon indicating copy to clipboard operation
toxiproxy copied to clipboard

Better Go api

Open sirupsen opened this issue 9 years ago • 2 comments

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

sirupsen avatar May 14 '15 13:05 sirupsen

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

ihsw avatar May 14 '15 19:05 ihsw

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()                                                                                                                                                                                                                                                                                      
}

sirupsen avatar May 14 '15 19:05 sirupsen