Best Toxiproxy code snippet using toxiproxy.CreateProxy
broker_test.go
Source:broker_test.go  
...24	)25	// setup proxy behavior26	{27		client := toxiproxy.NewClient("toxiproxy:8474")28		proxy, err := client.CreateProxy("kafka_pub", proxyAddr, kafkaAddr)29		if err != nil {30			t.Fatal(err)31		}32		defer proxy.Delete()33	}34	{35		pub, err := client.NewPublisher(36			client.PublisherBrokerAddress(proxyAddr),37			client.PublisherTopic(topic),38		)39		if err != nil {40			t.Fatal(err)41		}42		defer pub.Close()43		published := make(chan struct{}, msgs)44		for i := 0; i < msgs; i++ {45			go func() {46				payload := []byte("Hello")47				err := pub.Publish(topic, &payload)48				if err != nil {49					t.Errorf("error publishing message: %v", err)50				}51				published <- struct{}{}52			}()53		}54		for i := 0; i < msgs; i++ {55			<-published56		}57		close(published)58	}59}60func TestPacketPartitioning(t *testing.T) {61	var (62		proxyPubPort = 908163		proxySubPort = 908264		kafkaPubPort = 909165		kafkaSubPort = 909266		topic = randSeq(10)67		subs  = 368	)69	// setup proxy behavior70	{71		client := toxiproxy.NewClient("toxiproxy:8474")72		{73			proxy, err := client.CreateProxy("kafka_pub",74				fmt.Sprintf("toxiproxy:%d", proxyPubPort),75				fmt.Sprintf("minikafka:%d", kafkaPubPort),76			)77			if err != nil {78				t.Fatal(err)79			}80			defer proxy.Delete()81			proxy.AddToxic("small_packets", "slicer", "upstream", 1.0, toxiproxy.Attributes{82				"average_size":   1, // bytes83				"size_variation": 1,84				"delay":          100, // microseconds85			})86		}87		{88			proxy, err := client.CreateProxy("kafka_sub",89				fmt.Sprintf("toxiproxy:%d", proxySubPort),90				fmt.Sprintf("minikafka:%d", kafkaSubPort),91			)92			if err != nil {93				t.Fatal(err)94			}95			defer proxy.Delete()96			proxy.AddToxic("small_packets", "slicer", "upstream", 1.0, toxiproxy.Attributes{97				"average_size":   1, // bytes98				"size_variation": 1,99				"delay":          100, // microseconds100			})101		}102	}103	expected := make(map[string]struct{})104	for i := 0; i < 10; i++ {105		expected[fmt.Sprintf("Hello\n%d", i)] = struct{}{}106	}107	// publish messages108	{109		pub, err := client.NewPublisher(110			client.PublisherBrokerAddress(fmt.Sprintf("toxiproxy:%d", proxyPubPort)),111			client.PublisherTopic(topic),112		)113		if err != nil {114			t.Fatal(err)115		}116		defer pub.Close()117		published := make(chan struct{}, len(expected))118		for msg := range expected {119			go func(s string) {120				payload := []byte(s)121				err := pub.Publish(topic, &payload)122				if err != nil {123					t.Errorf("error publishing message: %v", err)124				}125				published <- struct{}{}126			}(msg)127		}128		for range expected {129			<-published130		}131		close(published)132	}133	// multiple subscribers on the same topic134	{135		subCh := make(chan struct{}, subs)136		for i := 0; i < subs; i++ {137			exp := make(map[string]struct{})138			for k, s := range expected {139				exp[k] = s140			}141			go func(expected map[string]struct{}) {142				sub, err := client.NewSubscriber(143					client.SubscriberBrokerAddress(fmt.Sprintf("toxiproxy:%d", proxySubPort)),144					client.SubscriberTopic(topic),145				)146				if err != nil {147					t.Error(err)148					return149				}150				defer sub.Close()151				for {152					bytes, err := sub.Read()153					if err != nil {154						if err == io.EOF {155							break156						}157						t.Error(err)158						return159					}160					msg := string(bytes)161					if _, ok := expected[msg]; !ok {162						t.Errorf("unexpected message received: %s", msg)163						continue164					}165					delete(expected, msg)166					if len(expected) == 0 {167						break168					}169				}170				for msg := range expected {171					t.Errorf("expected message not received: %s", msg)172				}173				subCh <- struct{}{}174			}(exp)175		}176		// subscribers exit when all expected messages are received177		for i := 0; i < subs; i++ {178			select {179			case <-subCh:180				continue181			case <-time.After(time.Second):182				t.Fatal("timeout waiting for subscriber to receive all expected messages")183			}184		}185	}186}187func TestAllPublished(t *testing.T) {188	var (189		proxyPubPort = 9081190		proxySubPort = 9082191		kafkaPubPort = 9091192		kafkaSubPort = 9092193		topic = randSeq(10)194		subs  = 3195	)196	// setup proxy behavior197	{198		client := toxiproxy.NewClient("toxiproxy:8474")199		{200			proxy, err := client.CreateProxy("kafka_pub",201				fmt.Sprintf("toxiproxy:%d", proxyPubPort),202				fmt.Sprintf("minikafka:%d", kafkaPubPort),203			)204			if err != nil {205				t.Fatal(err)206			}207			defer proxy.Delete()208		}209		{210			proxy, err := client.CreateProxy("kafka_sub",211				fmt.Sprintf("toxiproxy:%d", proxySubPort),212				fmt.Sprintf("minikafka:%d", kafkaSubPort),213			)214			if err != nil {215				t.Fatal(err)216			}217			defer proxy.Delete()218		}219	}220	expected := make(map[string]struct{})221	for i := 0; i < 10; i++ {222		expected[fmt.Sprintf("Hello\n%d", i)] = struct{}{}223	}224	// publish messages...toxiproxy.go
Source:toxiproxy.go  
...55}56func (toxiProxy *toxiProxyImpl) setup() error {57	toxiProxy.proxies = make(map[string]*toxiproxy.Proxy)58	var err error59	toxiProxy.proxies[smfProxyName], err = toxiProxy.client.CreateProxy(60		smfProxyName,61		fmt.Sprintf(":%d", toxiProxy.config.PlaintextPort),62		fmt.Sprintf("%s:%d", toxiProxy.config.Upstream, 55555),63	)64	if err != nil {65		return err66	}67	toxiProxy.proxies[compressedSmfProxyName], err = toxiProxy.client.CreateProxy(68		compressedSmfProxyName,69		fmt.Sprintf(":%d", toxiProxy.config.CompressedPort),70		fmt.Sprintf("%s:%d", toxiProxy.config.Upstream, 55003),71	)72	if err != nil {73		return err74	}75	toxiProxy.proxies[secureSmfProxyName], err = toxiProxy.client.CreateProxy(76		secureSmfProxyName,77		fmt.Sprintf(":%d", toxiProxy.config.SecurePort),78		fmt.Sprintf("%s:%d", toxiProxy.config.Upstream, 55443),79	)80	if err != nil {81		return err82	}83	return nil84}85func (toxiProxy *toxiProxyImpl) teardown() error {86	for _, proxy := range toxiProxy.proxies {87		err := proxy.Delete()88		if err != nil {89			return err...proxy.go
Source:proxy.go  
...9}10// AddPGProxy for postgresql11func AddPGProxy(listen string, upstream string) (*toxiproxy.Proxy, error) {12	// Alternatively, create the proxies manually with13	// return toxiClient.CreateProxy("pgsql", "[::]:32777", "localhost:5432")14	return toxiClient.CreateProxy("pgsql", listen, upstream)15}16// InjectLatency helper17func InjectLatency(name string, delay int) *toxiproxy.Proxy {18	proxy, err := toxiClient.Proxy(name)19	if err != nil {20		panic(err)21	}22	proxy.AddToxic("", "latency", "", 1, toxiproxy.Attributes{23		"latency": delay,24	})25	// defer proxies[name].RemoveToxic("latency_downstream")26	return proxy27}...CreateProxy
Using AI Code Generation
1import (2func main() {3	client, err := toxiproxy.NewClient("localhost:8474")4	if err != nil {5		fmt.Println("Error in creating client", err)6	}7	proxy, err := client.CreateProxy("redis", "localhost:6379", "localhost:6380")8	if err != nil {9		fmt.Println("Error in creating proxy", err)10	}11	fmt.Println(proxy.Name)12}13import (14func main() {15	client, err := toxiproxy.NewClient("localhost:8474")16	if err != nil {17		fmt.Println("Error in creating client", err)18	}19	toxic, err := client.CreateToxic("redis", "latency", "downstream", 1.0, toxiproxy.Attributes{"latency": 10000, "jitter": 1000})20	if err != nil {21		fmt.Println("Error in creating toxic", err)22	}23	fmt.Println(toxic.Name)24}25[{"name":"redis","listen":"localhost:6379","upstream":"localhost:6380","enabled":true,"toxics":[{"name":"latency","type":"latency","stream":"downstream","toxicity":1,"attributes":{"latency":10000,"jitter":1000}}]}]CreateProxy
Using AI Code Generation
1import (2func main() {3    toxiproxy, err := client.NewToxiproxy("localhost:8474")4    if err != nil {5        panic(err)6    }7    proxy, err := toxiproxy.CreateProxy("test", "localhost:3000", "localhost:3001")8    if err != nil {9        panic(err)10    }11    fmt.Println(proxy)12}13&{test localhost:3000 localhost:3001 0xc4200b4040 0xc4200b4060 0xc4200b4080 0xc4200b40a0 0xc4200b40c0 0xc4200b40e0 0xc4200b4100 0xc4200b4120 0xc4200b4140 0xc4200b4160 0xc4200b4180 0xc4200b41a0 0xc4200b41c0 0xc4200b41e0 0xc4200b4200 0xc4200b4220 0xc4200b4240 0xc4200b4260 0xc4200b4280 0xc4200b42a0 0xc4200b42c0 0xc4200b42e0 0xc4200b4300 0xc4200b4320 0xc4200b4340 0xc4200b4360 0xc4200b4380 0xc4200b43a0 0xc4200b43c0 0xc4200b43e0 0xc4200b4400 0xc4200b4420 0xc4200b4440 0xc4200b4460 0xc4200b4480 0xc4200b44a0 0xc4200b44c0 0xc4200b44e0 0xc4200b4500 0xc4200b4520 0xc4200b4540 0xc4200b4560 0xc4200b4580 0xc4200b45a0 0xc4200b45c0CreateProxy
Using AI Code Generation
1import "github.com/Shopify/toxiproxy"2func main() {3proxy, err := client.CreateProxy("redis", "localhost:6379", "localhost:6380")4if err != nil {5panic(err)6}7defer proxy.Delete()8}9import "github.com/Shopify/toxiproxy"10func main() {11proxy, err := client.CreateProxy("redis", "localhost:6379", "localhost:6380")12if err != nil {13panic(err)14}15defer proxy.Delete()16}17import "github.com/Shopify/toxiproxy"18func main() {19proxy, err := client.CreateProxy("redis", "localhost:6379", "localhost:6380")20if err != nil {21panic(err)22}23defer proxy.Delete()24}25import "github.com/Shopify/toxiproxy"26func main() {27proxy, err := client.CreateProxy("redis", "localhost:6379", "localhost:6380")28if err != nil {29panic(err)30}31defer proxy.Delete()32}33import "github.com/Shopify/toxiproxy"34func main() {35proxy, err := client.CreateProxy("redis", "CreateProxy
Using AI Code Generation
1import (2func main() {3	proxy, err := toxiproxyClient.CreateProxy("test", "localhost:5000", "localhost:5001")4	if err != nil {5		fmt.Println(err)6	}7	fmt.Println(proxy)8}9{test localhost:5000 localhost:5001 <nil> map[]}10import (11func main() {12	proxy, err := toxiproxyClient.CreateProxy("test", "localhost:5000", "localhost:5001")13	if err != nil {14		fmt.Println(err)15	}16	toxic := client.Toxic{17		Attributes: client.Attributes{18		},19	}20	err = proxy.AddToxic(toxic)21	if err != nil {22		fmt.Println(err)23	}24}CreateProxy
Using AI Code Generation
1import (2func main() {3	proxy := toxiproxy.NewProxy("proxy", "localhost:1234", "localhost:4321")4	toxic := toxiproxy.NewToxic("toxic", "latency", "downstream", 1.0)5	proxy.Toxics.Add(toxic)6	toxic = toxiproxy.NewToxic("toxic", "bandwidth", "downstream", 1.0)7	proxy.Toxics.Add(toxic)8	toxic = toxiproxy.NewToxic("toxic", "timeout", "downstream", 1.0)9	proxy.Toxics.Add(toxic)10	toxic = toxiproxy.NewToxic("toxic", "slow_close", "downstream", 1.0)11	proxy.Toxics.Add(toxic)12	toxic = toxiproxy.NewToxic("toxic", "slicer", "downstream", 1.0)13	proxy.Toxics.Add(toxic)14	toxic = toxiproxy.NewToxic("toxic", "limit_data", "downstream", 1.0)15	proxy.Toxics.Add(toxic)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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
