How to use Run method of toxics Package

Best Toxiproxy code snippet using toxics.Run

link_test.go

Source:link_test.go Github

copy

Full Screen

...57}58func TestAddRemoveStubs(t *testing.T) {59 collection := NewToxicCollection(nil)60 link := NewToxicLink(nil, collection, stream.Downstream)61 go link.stubs[0].Run(collection.chain[stream.Downstream][0])62 collection.links["test"] = link63 // Add stubs64 collection.chainAddToxic(&toxics.ToxicWrapper{65 Toxic: new(toxics.LatencyToxic),66 Type: "latency",67 Direction: stream.Downstream,68 BufferSize: 1024,69 Toxicity: 1,70 })71 toxic := &toxics.ToxicWrapper{72 Toxic: new(toxics.BandwidthToxic),73 Type: "bandwidth",74 Direction: stream.Downstream,75 BufferSize: 2048,76 Toxicity: 1,77 }78 collection.chainAddToxic(toxic)79 if cap(link.stubs[len(link.stubs)-1].Output) != 0 {80 t.Fatalf("Link output buffer was not initialized as 0: %d", cap(link.stubs[0].Output))81 }82 for i, toxic := range collection.chain[stream.Downstream] {83 if cap(link.stubs[i].Input) != toxic.BufferSize {84 t.Fatalf("%s buffer was not initialized as %d: %d", toxic.Type, toxic.BufferSize, cap(link.stubs[i].Input))85 }86 }87 // Remove stubs88 collection.chainRemoveToxic(toxic)89 if cap(link.stubs[len(link.stubs)-1].Output) != 0 {90 t.Fatalf("Link output buffer was not initialized as 0: %d", cap(link.stubs[0].Output))91 }92 for i, toxic := range collection.chain[stream.Downstream] {93 if cap(link.stubs[i].Input) != toxic.BufferSize {94 t.Fatalf("%s buffer was not initialized as %d: %d", toxic.Type, toxic.BufferSize, cap(link.stubs[i].Input))95 }96 }97}98func TestNoDataDropped(t *testing.T) {99 collection := NewToxicCollection(nil)100 link := NewToxicLink(nil, collection, stream.Downstream)101 go link.stubs[0].Run(collection.chain[stream.Downstream][0])102 collection.links["test"] = link103 toxic := &toxics.ToxicWrapper{104 Toxic: &toxics.LatencyToxic{105 Latency: 1000,106 },107 Type: "latency",108 Direction: stream.Downstream,109 BufferSize: 1024,110 Toxicity: 1,111 }112 done := make(chan struct{})113 defer close(done)114 go func() {115 for i := 0; i < 64*1024; i++ {116 buf := make([]byte, 2)117 binary.BigEndian.PutUint16(buf, uint16(i))118 link.input.Write(buf)119 }120 link.input.Close()121 }()122 go func() {123 for {124 select {125 case <-done:126 return127 default:128 collection.chainAddToxic(toxic)129 collection.chainRemoveToxic(toxic)130 }131 }132 }()133 buf := make([]byte, 2)134 for i := 0; i < 64*1024; i++ {135 n, err := link.output.Read(buf)136 if n != 2 || err != nil {137 t.Fatalf("Read failed: %d %v", n, err)138 } else {139 val := binary.BigEndian.Uint16(buf)140 if val != uint16(i) {141 t.Fatalf("Read incorrect bytes: %v != %d", val, i)142 }143 }144 }145 n, err := link.output.Read(buf)146 if n != 0 || err != io.EOF {147 t.Fatalf("Expected EOF: %d %v", n, err)148 }149}150func TestToxicity(t *testing.T) {151 collection := NewToxicCollection(nil)152 link := NewToxicLink(nil, collection, stream.Downstream)153 go link.stubs[0].Run(collection.chain[stream.Downstream][0])154 collection.links["test"] = link155 toxic := &toxics.ToxicWrapper{156 Toxic: new(toxics.TimeoutToxic),157 Name: "timeout1",158 Type: "timeout",159 Direction: stream.Downstream,160 Toxicity: 0,161 }162 collection.chainAddToxic(toxic)163 // Toxic should be a Noop because of toxicity164 n, err := link.input.Write([]byte{42})165 if n != 1 || err != nil {166 t.Fatalf("Write failed: %d %v", n, err)167 }168 buf := make([]byte, 2)169 n, err = link.output.Read(buf)170 if n != 1 || err != nil {171 t.Fatalf("Read failed: %d %v", n, err)172 } else if buf[0] != 42 {173 t.Fatalf("Read wrong byte: %x", buf[0])174 }175 toxic.Toxicity = 1176 toxic.Toxic.(*toxics.TimeoutToxic).Timeout = 100177 collection.chainUpdateToxic(toxic)178 err = testhelper.TimeoutAfter(150*time.Millisecond, func() {179 n, err = link.input.Write([]byte{42})180 if n != 1 || err != nil {181 t.Fatalf("Write failed: %d %v", n, err)182 }183 n, err = link.output.Read(buf)184 if n != 0 || err != io.EOF {185 t.Fatalf("Read did not get EOF: %d %v", n, err)186 }187 })188 if err != nil {189 t.Fatal(err)190 }191}192func TestStateCreated(t *testing.T) {193 collection := NewToxicCollection(nil)194 link := NewToxicLink(nil, collection, stream.Downstream)195 go link.stubs[0].Run(collection.chain[stream.Downstream][0])196 collection.links["test"] = link197 collection.chainAddToxic(&toxics.ToxicWrapper{198 Toxic: new(toxics.LimitDataToxic),199 Type: "limit_data",200 Direction: stream.Downstream,201 Toxicity: 1,202 })203 if link.stubs[len(link.stubs)-1].State == nil {204 t.Fatalf("New toxic did not have state object created.")205 }206}...

Full Screen

Full Screen

link.go

Source:link.go Github

copy

Full Screen

...61 for i, toxic := range link.toxics.chain[link.direction] {62 if stateful, ok := toxic.Toxic.(toxics.StatefulToxic); ok {63 link.stubs[i].State = stateful.NewState()64 }65 go link.stubs[i].Run(toxic)66 }67 go func() {68 bytes, err := io.Copy(dest, link.output)69 if err != nil {70 logrus.WithFields(logrus.Fields{71 "name": link.proxy.Name,72 "bytes": bytes,73 "err": err,74 }).Warn("Destination terminated")75 }76 dest.Close()77 link.toxics.RemoveLink(name)78 link.proxy.RemoveConnection(name)79 }()80}81// Add a toxic to the end of the chain.82func (link *ToxicLink) AddToxic(toxic *toxics.ToxicWrapper) {83 i := len(link.stubs)84 newin := make(chan *stream.StreamChunk, toxic.BufferSize)85 link.stubs = append(link.stubs, toxics.NewToxicStub(newin, link.stubs[i-1].Output))86 // Interrupt the last toxic so that we don't have a race when moving channels87 if link.stubs[i-1].InterruptToxic() {88 link.stubs[i-1].Output = newin89 if stateful, ok := toxic.Toxic.(toxics.StatefulToxic); ok {90 link.stubs[i].State = stateful.NewState()91 }92 go link.stubs[i].Run(toxic)93 go link.stubs[i-1].Run(link.toxics.chain[link.direction][i-1])94 } else {95 // This link is already closed, make sure the new toxic matches96 link.stubs[i].Output = newin // The real output is already closed, close this instead97 link.stubs[i].Close()98 }99}100// Update an existing toxic in the chain.101func (link *ToxicLink) UpdateToxic(toxic *toxics.ToxicWrapper) {102 if link.stubs[toxic.Index].InterruptToxic() {103 go link.stubs[toxic.Index].Run(toxic)104 }105}106// Remove an existing toxic from the chain.107func (link *ToxicLink) RemoveToxic(toxic *toxics.ToxicWrapper) {108 i := toxic.Index109 if link.stubs[i].InterruptToxic() {110 cleanup, ok := toxic.Toxic.(toxics.CleanupToxic)111 if ok {112 cleanup.Cleanup(link.stubs[i])113 // Cleanup could have closed the stub.114 if link.stubs[i].Closed() {115 return116 }117 }118 stop := make(chan bool)119 // Interrupt the previous toxic to update its output120 go func() {121 stop <- link.stubs[i-1].InterruptToxic()122 }()123 // Unblock the previous toxic if it is trying to flush124 // If the previous toxic is closed, continue flusing until we reach the end.125 interrupted := false126 stopped := false127 for !interrupted {128 select {129 case interrupted = <-stop:130 stopped = true131 case tmp := <-link.stubs[i].Input:132 if tmp == nil {133 link.stubs[i].Close()134 if !stopped {135 <-stop136 }137 return138 }139 link.stubs[i].Output <- tmp140 }141 }142 // Empty the toxic's buffer if necessary143 for len(link.stubs[i].Input) > 0 {144 tmp := <-link.stubs[i].Input145 if tmp == nil {146 link.stubs[i].Close()147 return148 }149 link.stubs[i].Output <- tmp150 }151 link.stubs[i-1].Output = link.stubs[i].Output152 link.stubs = append(link.stubs[:i], link.stubs[i+1:]...)153 go link.stubs[i-1].Run(link.toxics.chain[link.direction][i-1])154 }155}...

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client, err := toxiproxy.NewClient("localhost:8474")4 if err != nil {5 log.Fatal(err)6 }7 toxic := &toxiproxy.Toxic{8 Attributes: map[string]interface{}{9 },10 }11 if err := client.CreateToxic("redis", toxic); err != nil {12 log.Fatal(err)13 }14}15import (16func main() {17 client, err := toxiproxy.NewClient("localhost:8474")18 if err != nil {19 log.Fatal(err)20 }21 toxics, err := client.Toxics("redis")22 if err != nil {23 log.Fatal(err)24 }25 for _, toxic := range toxics {26 log.Println(toxic.Name)27 }28}29import (30func main() {31 client, err := toxiproxy.NewClient("localhost:8474")32 if err != nil {33 log.Fatal(err)34 }35 toxic := &toxiproxy.Toxic{36 Attributes: map[string]interface{}{37 },38 }39 if err := client.UpdateToxic("redis", toxic); err != nil

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cli, err := toxiproxy.NewClient("localhost:8474")4 if err != nil {5 log.Fatal(err)6 }7 toxics, err := cli.Toxics("redis")8 if err != nil {9 log.Fatal(err)10 }11 err = toxics.Latency("latency", "downstream", 1000, 0)12 if err != nil {13 log.Fatal(err)14 }15 err = toxics.Run("latency")16 if err != nil {17 log.Fatal(err)18 }19 err = toxics.Stop("latency")20 if err != nil {21 log.Fatal(err)22 }23}24import (25func main() {26 cli, err := toxiproxy.NewClient("localhost:8474")27 if err != nil {28 log.Fatal(err)29 }30 toxic, err := cli.Toxic("redis", "latency")31 if err != nil {32 log.Fatal(err)33 }34 err = toxic.Run()35 if err != nil {36 log.Fatal(err)37 }38}39import (40func main() {41 cli, err := toxiproxy.NewClient("localhost:8474")42 if err != nil {43 log.Fatal(err)44 }45 proxy, err := cli.Proxy("redis")46 if err != nil {47 log.Fatal(err)48 }49 err = proxy.Run()50 if err != nil {51 log.Fatal(err)52 }53}54import (55func main() {56 cli, err := toxiproxy.NewClient("localhost:8474")57 if err != nil {58 log.Fatal(err)59 }60 err = cli.Run("redis", "latency")61 if err != nil {62 log.Fatal(err)

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 toxics := client.NewClient("localhost:8474").Toxics("redis-master")4 toxics.Latency("latency", "downstream", 1000)5 toxics.Timeout("timeout", "downstream", 5000)6}7import (8func main() {9 proxy := client.NewClient("localhost:8474").Proxy("redis-master")10 proxy.Enable()11 proxy.Disable()12 proxy.SimulateTimeout()13 proxy.SimulateLatency()14}15import (16func main() {17 client := client.NewClient("localhost:8474")18 client.CreateProxy("redis-master", "

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 toxics := toxiproxy.NewClient("localhost:8474")4 proxy, err := toxics.CreateProxy("localhost:8080", "localhost:8081")5 if err != nil {6 log.Fatal(err)7 }8 toxic, err := proxy.CreateToxic("latency", "latency", "downstream", 1.0)9 if err != nil {10 log.Fatal(err)11 }12 toxic.SetAttribute("latency", 500)13 time.Sleep(time.Second)14 toxic.Remove()15 proxy.Remove()16}17import (18func main() {19 toxics := toxiproxy.NewClient("localhost:8474")20 proxy, err := toxics.CreateProxy("localhost:8080", "localhost:8081")21 if err != nil {22 log.Fatal(err)23 }24 toxic, err := proxy.CreateToxic("latency", "latency", "downstream", 1.0)25 if err != nil {26 log.Fatal(err)27 }28 toxic.SetAttribute("latency", 500)29 time.Sleep(time.Second)30 toxic.Remove()31 proxy.Remove()32}33import (34func main() {35 toxics := toxiproxy.NewClient("localhost:8474")36 proxy, err := toxics.CreateProxy("localhost:8080", "localhost:8081")37 if err != nil {38 log.Fatal(err)39 }

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 toxic := toxics.Create("latency", toxiproxy.Toxic{4 Attributes: map[string]interface{}{5 },6 })7 fmt.Println(toxic)8}9{latency 0 downstream 1 2017-03-16 18:06:58.196 +0530 IST map[latency:1000 jitter:200]}10import (11func main() {12 toxic := toxics.Create("latency", toxiproxy.Toxic{13 Attributes: map[string]interface{}{14 },15 })16 toxic.Run()17 fmt.Println(toxic)18}19{latency 0 downstream 1 2017-03-16 18:06:58.196 +0530 IST map[latency:1000 jitter:200]}20import (21func main() {22 toxic := toxics.Create("latency", toxiproxy.Toxic{23 Attributes: map[string]interface{}{

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 proxy := httputil.NewSingleHostReverseProxy(proxyUrl)7 server := &http.Server{8 }9 log.Println("Starting HTTP server on :8000")10 log.Fatal(server.ListenAndServe())11}

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.

Run Toxiproxy automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful