How to use Get method of toxiproxy Package

Best Toxiproxy code snippet using toxiproxy.Get

resilience_integration_test.go

Source:resilience_integration_test.go Github

copy

Full Screen

...84func Validate_Sword(sword entities.Sword) bool{85 return (sword.Type != "" && sword.Weight != 0)86}87// Integration Tests88// Single request GetIron from Smelterservice89// Success: Valid Iron Response90// Fails if enot valid Iron response91func Test_Integration_GetIronFromSmelterservice(t *testing.T){92 if testing.Short() {93 t.Skip("skipping Integration Test: TestGetIronFromSmelterservice")94 }95 toxiClient, _ = InitToxiProxy()96 iron, err := application.ForgeService.GetIronFromSmelterservice()97 if err != nil {98 t.Fail()99 }100 assert.Equal(t, reflect.TypeOf(entities.Iron{}), reflect.TypeOf(iron))101}102// Single request GetSword103// Success: Valid Sword Response104// Fails if enot valid sword response105func Test_Integration_GetSword_Success(t *testing.T) {106 if testing.Short() {107 t.Skip("skipping Integration Test: TestGetSwords")108 }109 toxiClient, _ = InitToxiProxy()110 sword, err := application.ForgeService.GetSword()111 if err != nil {112 t.Fatal(err)113 }114 assert.True(t,Validate_Sword(sword))115}116// Resilience Test117// Single request GetSword with timeout of the smelterservice118// Success: When timeout, error should be the Circuit Breaker Opening to save resources119// Fails if error is not ErrCircuitBreakerOpen120func Test_Integration_CircuitBreakerOpens_Timeout_GetSword(t *testing.T) {121 if testing.Short() {122 t.Skip("skipping Resilience Test: Test_CircuitBreakerOpens_GetIron")123 }124 var err error125 toxiClient, err = InitToxiProxy()126 if err != nil {127 logrus.Error("Could not create proxy listen on smelter connection")128 t.Fatal(err)129 }130 _, err = proxySmelter.AddToxic("timeout_smelter", "timeout", "", 1, toxiproxy.Attributes{})131 if err != nil{132 logrus.Error("Could not add toxic to smelter proxy")133 t.Fatal(err)134 }135 r, err := application.ForgeService.GetSword()136 defer proxySmelter.RemoveToxic("timeout_smelter")137 if err != nil && err.Error() == ErrCircuitBreakerOpen.Error() {138 logrus.Debug(err.Error(), r)139 return140 } else {141 t.Fail()142 }143}144// Single request GetSword with latency of the smelterservice145// Success: When latency is too high, error should be the Circuit Breaker Opening to save resources146// Fails if error is not ErrCircuitBreakerOpen147func Test_Integration_CircuitBreakerOpens_Latency_GetSword(t *testing.T) {148 if testing.Short() {149 t.Skip("skipping Resilience Test: Test_CircuitBreakerOpens_GetIron")150 }151 var err error152 toxiClient, err = InitToxiProxy()153 if err != nil {154 logrus.Error("Could not create proxy listen on smelter connection")155 t.Fatal(err)156 }157 _, err = proxySmelter.AddToxic("latency_downstream", "latency", "", 1, toxiproxy.Attributes{158 "latency": 3000,159 })160 if err != nil{161 logrus.Error("Could not add toxic to smelter proxy")162 t.Fatal(err)163 }164 r, err := application.ForgeService.GetSword()165 defer proxySmelter.RemoveToxic("latency_downstream")166 if err != nil && err.Error() == ErrCircuitBreakerOpen.Error() {167 logrus.Debug(err.Error(), r)168 return169 } else {170 t.Fail()171 }172}173// Bulk requests with a temporary timeout of the smelterservice174// Success: When timeout, error should be the Circuit Breaker Opening to save resources175// Fails if error is not ErrCircuitBreakerOpen176func Test_Integration_BulkGetSword_TemporarySmelterTimeout(t *testing.T) {177 if testing.Short() {178 t.Skip("skipping Resilience Test: TestSmelterConnectionSlow")179 }180 var err error181 toxiClient, err = InitToxiProxy()182 if err != nil {183 logrus.Error("Could not create proxy listen on smelter connection")184 t.Fatal(err)185 }186 _, err = proxySmelter.AddToxic("timeout_smelter", "timeout", "", 0, toxiproxy.Attributes{})187 if err != nil{188 logrus.Error("Could not add toxic to smelter proxy")189 t.Fatal(err)190 }191 // simulate a temporary timeout192 // make a bulk of requests to to forgeservice including internal requests to smelterservice which has occasional timeouts,193 //first 19 success, 20 to 179 failure, rest success194 for i := 0; i < 10; i++ {195 if i > 2 && i < 7 {196 if i == 3 {197 proxySmelter.UpdateToxic("timeout_smelter", 1, toxiproxy.Attributes{})198 }199 r, err := application.ForgeService.GetSword()200 if err != nil {201 logrus.Debug("Service Timeout HTTP Status: "+err.Error())202 if err.Error() == ErrCircuitBreakerOpen.Error() {203 logrus.Debug(err.Error(), r)204 return205 } else {206 t.Fail()207 }208 }209 }210 if i < 3 || i > 6 {211 if i == 7{212 proxySmelter.UpdateToxic("timeout_smelter", 0, toxiproxy.Attributes{})213 }214 r, err := application.ForgeService.GetSword()215 if err != nil {216 t.Fatal(err)217 }218 logrus.Debug("Response from Smelterconnection: ",r)219 }220 }221 // clean up toxic later222 defer proxySmelter.RemoveToxic("timeout_smelter")223}224// Bulk requests with a temporary latency of the smelterservice225// Success: When latency is too high, error should be the Circuit Breaker Opening to save resources226// Fails if error is not ErrCircuitBreakerOpen227func Test_Integration_BulkGetSword_TemporarySmelterLatency(t *testing.T) {228 if testing.Short() {229 t.Skip("skipping Resilience Test: TestSmelterConnectionSlow")230 }231 var err error232 toxiClient, err = InitToxiProxy()233 if err != nil {234 logrus.Error("Could not create proxy listen on smelter connection")235 t.Fatal(err)236 }237 _, err = proxySmelter.AddToxic("latency_downstream", "latency", "", 0, toxiproxy.Attributes{238 "latency": 3000,239 })240 if err != nil{241 logrus.Error("Could not add toxic to smelter proxy")242 t.Fatal(err)243 }244 // simulate a temporary latency245 // make a bulk of requests to to forgeservice including internal requests to smelterservice which has occasional timeouts,246 //first 19 success, 20 to 179 failure, rest success247 for i := 0; i < 5; i++ {248 if i == 4 {249 logrus.Debug("NOW TOXIC ACTIVE: latency_downstream")250 proxySmelter.UpdateToxic("latency_downstream", 1, toxiproxy.Attributes{})251 r, err := application.ForgeService.GetSword()252 if err != nil {253 logrus.Debug("Service Timeout HTTP Status: "+err.Error())254 }255 logrus.Debug("Response from timed out service "+"http://localhost:8080/sword",r)256 } else {257 r, err := application.ForgeService.GetSword()258 if err != nil {259 t.Fail()260 }261 logrus.Debug("Response from smelterservice: ",r)262 }263 }264 // clean up toxic later265 defer proxySmelter.RemoveToxic("latency_downstream")266}267// Bulk requests with a temporary latency and jittering of the smelterservice connection268// Success: When latency is too high, error should be the Circuit Breaker Opening to save resources269// Fails if error is not ErrCircuitBreakerOpen270func Test_Integration_BulkGetSword_SmelterLatencyJittering(t *testing.T) {271 if testing.Short() {272 t.Skip("skipping Resilience Test: TestSmelterConnectionSlow")273 }274 var err error275 toxiClient, err = InitToxiProxy()276 if err != nil {277 logrus.Error("Could not create proxy listen on smelter connection")278 t.Fatal(err)279 }280 _, err = proxySmelter.AddToxic("latency_downstream", "latency", "", 1, toxiproxy.Attributes{281 "latency": 2500,282 "jittering": 1000,283 })284 if err != nil{285 logrus.Error("Could not add toxic to smelter proxy")286 t.Fatal(err)287 }288 logrus.Debug("NOW TOXIC ACTIVE: latency_downstream")289 // simulate a temporary latency with high jittering290 for i := 0; i < 5; i++ {291 r, err := application.ForgeService.GetSword()292 if err != nil {293 logrus.Debug("Service Timeout HTTP Status: "+err.Error())294 }295 logrus.Debug("Response from timed out service "+"http://localhost:8080/sword",r)296 }297 // clean up toxic later298 defer proxySmelter.RemoveToxic("latency_downstream")299}300func Test_Integration_BulkGetSword_SmelterBandwidth(t *testing.T) {301 if testing.Short() {302 t.Skip("skipping Resilience Test: TestSmelterConnectionSlow")303 }304 var err error305 toxiClient, err = InitToxiProxy()306 if err != nil {307 logrus.Error("Could not create proxy listen on smelter connection")308 t.Fatal(err)309 }310 _, err = proxySmelter.AddToxic("low_bandwidth_smelter", "bandwidth", "", 1, toxiproxy.Attributes{311 "rate": 0,312 })313 if err != nil{314 logrus.Error("Could not add toxic to smelter proxy")315 t.Fatal(err)316 }317 logrus.Debug("NOW TOXIC ACTIVE: low_bandwidth_smelter")318 // simulate a temporary latency with high jittering319 for i := 0; i < 2; i++ {320 r, err := application.ForgeService.GetSword()321 if err != nil {322 logrus.Debug("Service Timeout HTTP Status: "+err.Error())323 }324 logrus.Debug("Response from slow service "+"http://localhost:8080/sword",r)325 }326 // clean up toxic later327 defer proxySmelter.RemoveToxic("low_bandwidth_smelter")328}329func Test_Integration_BulkGetSword_Smelter_limit_data(t *testing.T) {330 if testing.Short() {331 t.Skip("skipping Resilience Test: TestSmelterConnectionSlow")332 }333 var err error334 toxiClient, err = InitToxiProxy()335 if err != nil {336 logrus.Error("Could not create proxy listen on smelter connection")337 t.Fatal(err)338 }339 _, err = proxySmelter.AddToxic("limit_data_smelter", "limit_data", "", 1, toxiproxy.Attributes{340 "bytes": 100,341 })342 if err != nil{343 logrus.Error("Could not add toxic to smelter proxy")344 t.Fatal(err)345 }346 logrus.Debug("NOW TOXIC ACTIVE: limit_data_smelter")347 for i := 0; i < 2; i++ {348 r, err := application.ForgeService.GetSword()349 if err != nil {350 logrus.Debug("Service Timeout HTTP Status: "+err.Error())351 }352 logrus.Debug("Response from limited service "+"http://localhost:8080/sword",r)353 }354 // clean up toxic later355 defer proxySmelter.RemoveToxic("limit_data_smelter")356}357func workerGetIron(id int, wg *sync.WaitGroup) {358 defer wg.Done()359 fmt.Printf("Worker %d starting\n", id)360 sword, err := application.ForgeService.GetSword()361 logrus.Debugln(sword, err)362 fmt.Printf("Worker %d done\n", id)363}364// makes a bunch of concurrent requests to GetIron that wait for each other to finish365// Success: No errors366func Test_Integration_ConcurrentRequests_GetIron(t *testing.T) {367 if testing.Short() {368 t.Skip("skipping Resilience Test: Test_Integration_ConcurrentRequests_GetIron")369 }370 var err error371 toxiClient, err = InitToxiProxy()372 if err != nil {373 logrus.Error("Could not create proxy listen on smelter connection")374 t.Fatal(err)375 }376 var wg sync.WaitGroup377 for i := 1; i <= 100; i++ {378 wg.Add(1)379 go workerGetIron(i, &wg)380 }381 wg.Wait()382}...

Full Screen

Full Screen

toxiproxy_docker.go

Source:toxiproxy_docker.go Github

copy

Full Screen

...44 // FIXME(joey): Use a random, free port. Even better, reserve or45 // pre-acquire the port so there is no race condition to acquire it.46 // This is probably a hard problem though (transferring port to47 // another process, with Go).48 openPort, err := freeport.GetFreePort()49 if err != nil {50 return nil, err51 }52 bindings["8474/tcp"] = []nat.PortBinding{nat.PortBinding{HostPort: strconv.Itoa(openPort)}}53 d.apiPort = openPort54 endpoints := make(map[string]*network.EndpointSettings, 1)55 endpoints[d.config.NetworkName] = &network.EndpointSettings{}56 log.Printf("creating toxiproxy container %q bound to localhost:%d", d.config.Name, openPort)57 resp, err := d.c.ContainerCreate(58 ctx,59 &container.Config{60 Image: "shopify/toxiproxy:latest",61 Hostname: d.config.Name,62 },63 &container.HostConfig{64 // FIXME(joey): Might not want to set this. Not sure about the65 // impact yet.66 NetworkMode: container.NetworkMode(d.config.NetworkName),67 PublishAllPorts: true,68 PortBindings: bindings,69 },70 &network.NetworkingConfig{71 EndpointsConfig: endpoints,72 },73 d.config.Name,74 )75 if err != nil {76 return nil, err77 }78 for _, warning := range resp.Warnings {79 log.Printf("warning: %s", warning)80 }81 d.containerID = resp.ID82 return d, nil83}84func (d *DockerToxiproxy) Start(ctx context.Context) error {85 log.Printf("starting toxiproxy container %q", d.containerID)86 if err := d.c.ContainerStart(ctx, d.containerID, types.ContainerStartOptions{}); err != nil {87 return err88 }89 return nil90}91func (d *DockerToxiproxy) Cleanup(ctx context.Context) error {92 if err := d.c.ContainerRemove(93 ctx,94 d.containerID,95 types.ContainerRemoveOptions{96 RemoveVolumes: true,97 Force: true,98 },99 ); err != nil {100 log.Printf("FATAL: leaded resources, got: %+v", err)101 return err102 }103 return nil104}105// If the listen address is empty, a random port will be assigned and106// returned.107func (d *DockerToxiproxy) AddProxy(name string, listen string, upstream string) (*toxiproxy.Proxy, error) {108 if listen == "" {109 openPort, err := freeport.GetFreePort()110 if err != nil {111 return nil, err112 }113 listen = fmt.Sprintf("%s:%d", d.config.Name, openPort)114 }115 proxy, err := d.GetClient().CreateProxy(name, listen, upstream)116 if err != nil {117 return nil, err118 }119 log.Printf("created toxiproxy name=%q listen=%q upstream=%q", proxy.Name, proxy.Listen, proxy.Upstream)120 return proxy, nil121}122func (d *DockerToxiproxy) GetClient() *toxiproxy.Client {123 if d.toxiproxyClient != nil {124 return d.toxiproxyClient125 }126 client := toxiproxy.NewClient(fmt.Sprintf("localhost:%d", d.apiPort))127 d.toxiproxyClient = client128 return client129}...

Full Screen

Full Screen

factory_ddb.go

Source:factory_ddb.go Github

copy

Full Screen

...35 return fmt.Errorf("can not add default ddb component: %w", err)36 }37 return nil38}39func (f *ddbFactory) GetSettingsSchema() ComponentBaseSettingsAware {40 return &ddbSettings{}41}42func (f *ddbFactory) DescribeContainers(settings interface{}) componentContainerDescriptions {43 s := settings.(*ddbSettings)44 descriptions := componentContainerDescriptions{45 "main": {46 containerConfig: f.configureContainer(settings),47 healthCheck: f.healthCheck(),48 },49 }50 if s.ToxiproxyEnabled {51 descriptions["toxiproxy"] = f.toxiproxyFactory.describeContainer(s.ExpireAfter)52 }53 return descriptions54}55func (f *ddbFactory) configureContainer(settings interface{}) *containerConfig {56 s := settings.(*ddbSettings)57 return &containerConfig{58 Repository: "amazon/dynamodb-local",59 Tag: "1.18.0",60 PortBindings: portBindings{61 "8000/tcp": s.Port,62 },63 ExpireAfter: s.ExpireAfter,64 }65}66func (f *ddbFactory) healthCheck() ComponentHealthCheck {67 return func(container *container) error {68 var err error69 var client *dynamodb.Client70 if client, err = f.client(container); err != nil {71 return fmt.Errorf("can not build client: %w", err)72 }73 _, err = client.ListTables(context.Background(), &dynamodb.ListTablesInput{})74 return err75 }76}77func (f *ddbFactory) Component(_ cfg.Config, logger log.Logger, containers map[string]*container, settings interface{}) (Component, error) {78 s := settings.(*ddbSettings)79 var err error80 var proxy *toxiproxy.Proxy81 ddbAddress := containers["main"].bindings["8000/tcp"].getAddress()82 if s.ToxiproxyEnabled {83 toxiproxyClient := f.toxiproxyFactory.client(containers["toxiproxy"])84 if proxy, err = toxiproxyClient.CreateProxy("ddb", ":56248", ddbAddress); err != nil {85 return nil, fmt.Errorf("can not create toxiproxy proxy for ddb component: %w", err)86 }87 ddbAddress = containers["toxiproxy"].bindings["56248/tcp"].getAddress()88 }89 component := &DdbComponent{90 baseComponent: baseComponent{91 name: s.Name,92 },93 logger: logger,94 ddbAddress: ddbAddress,95 toxiproxy: proxy,96 }97 return component, nil98}99func (f *ddbFactory) client(container *container) (*dynamodb.Client, error) {100 binding := container.bindings["8000/tcp"]101 address := fmt.Sprintf("http://%s:%s", binding.host, binding.port)102 var err error103 var cfg aws.Config104 if cfg, err = GetDefaultAwsSdkConfig(address); err != nil {105 return nil, fmt.Errorf("can't get default aws sdk config: %w", err)106 }107 return dynamodb.NewFromConfig(cfg), nil108}...

Full Screen

Full Screen

Get

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 toxiproxy := client.NewClient("localhost:8474")4 proxy, err := toxiproxy.Get("proxy1")5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(proxy)9}10import (11func main() {12 toxiproxy := client.NewClient("localhost:8474")13 proxies, err := toxiproxy.List()14 if err != nil {15 fmt.Println(err)16 }17 fmt.Println(proxies)18}19import (20func main() {21 toxiproxy := client.NewClient("localhost:8474")22 proxy, err := toxiproxy.Create(&client.Proxy{23 Toxics: []*client.Toxic{},24 })25 if err != nil {26 fmt.Println(err)27 }28 fmt.Println(proxy)29}30import (31func main() {32 toxiproxy := client.NewClient("localhost:8474")33 err := toxiproxy.Delete("proxy1")34 if err != nil {35 fmt.Println(err)36 }37}38import (39func main() {40 toxiproxy := client.NewClient("localhost:8474")41 err := toxiproxy.Enable("proxy1")42 if err != nil {43 fmt.Println(err)44 }45}

Full Screen

Full Screen

Get

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 toxiproxy := client.NewClient("localhost:8474")4 proxy, err := toxiproxy.Get("test")5 if err != nil {6 panic(err)7 }8 fmt.Println(proxy.Name)9}10import (11func main() {12 toxiproxy := client.NewClient("localhost:8474")13 proxy := client.Proxy{

Full Screen

Full Screen

Get

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 toxiproxyClient := toxiproxy.NewClient("localhost:8474")4 proxy, err := toxiproxyClient.Get("redis")5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(proxy.Name)9 fmt.Println(proxy.Listen)10 fmt.Println(proxy.Upstream)11}

Full Screen

Full Screen

Get

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 resp, err := http.Get(url)4 if err != nil {5 }6 defer resp.Body.Close()7 body, err := ioutil.ReadAll(resp.Body)8 fmt.Println(string(body))9}10import (11func main() {12 fmt.Println("URL:>", url)13 var jsonStr = []byte(`{"name":"test_toxiproxy","listen":"

Full Screen

Full Screen

Get

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 toxiproxyClient.Reset()4 toxiproxyClient.CreateProxy("my_proxy", "localhost:9000", "localhost:3000")5 toxiproxyClient.CreateProxy("my_proxy1", "localhost:9001", "localhost:3001")6 toxiproxyClient.CreateProxy("my_proxy2", "localhost:9002", "localhost:3002")7 toxiproxyClient.CreateProxy("my_proxy3", "localhost:9003", "localhost:3003")8 toxiproxyClient.CreateProxy("my_proxy4", "localhost:9004", "localhost:3004")9 toxiproxyClient.CreateProxy("my_proxy5", "localhost:9005", "localhost:3005")10 toxiproxyClient.CreateProxy("my_proxy6", "localhost:9006", "localhost:3006")11 toxiproxyClient.CreateProxy("my_proxy7", "localhost:9007", "localhost:3007")12 toxiproxyClient.CreateProxy("my_proxy8", "localhost:9008", "localhost:3008")13 toxiproxyClient.CreateProxy("my_proxy9", "localhost:9009", "localhost:3009")14 toxiproxyClient.CreateProxy("my_proxy10", "localhost:9010", "localhost:3010")15 toxiproxyClient.CreateProxy("my_proxy11", "localhost:9011", "localhost:3011")16 toxiproxyClient.CreateProxy("my_proxy12", "localhost:9012", "localhost:3012")17 toxiproxyClient.CreateProxy("my_proxy13", "localhost:9013", "localhost:3013")18 toxiproxyClient.CreateProxy("my_proxy14", "localhost:9014", "localhost:3014")19 toxiproxyClient.CreateProxy("my_proxy15", "localhost:9015", "localhost:3015")20 toxiproxyClient.CreateProxy("my_proxy16", "localhost:9016", "localhost:3016")21 toxiproxyClient.CreateProxy("my_proxy17", "localhost:9017", "localhost:

Full Screen

Full Screen

Get

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 proxy, err := proxyClient.Get("myproxy")4 if err != nil {5 fmt.Println(err)6 }7 fmt.Println(proxy.Name)8}9import (10func main() {11 proxy := client.Proxy{12 }13 err := proxyClient.Set(proxy)14 if err != nil {15 fmt.Println(err)16 }17}18import (19func main() {20 proxy := client.Proxy{21 }22 err := proxyClient.Delete(proxy)23 if err != nil {24 fmt.Println(err)25 }26}27import (28func main() {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful