Best Toxiproxy code snippet using toxiproxy.Enable
server.go
Source:server.go
1package main2import (3 "flag"4 "fmt"5 "math/rand"6 "os"7 "os/signal"8 "strconv"9 "syscall"10 "time"11 "github.com/prometheus/client_golang/prometheus"12 "github.com/rs/zerolog"13 "github.com/rs/zerolog/log"14 "github.com/Shopify/toxiproxy/v2"15 "github.com/Shopify/toxiproxy/v2/collectors"16)17type cliArguments struct {18 host string19 port string20 config string21 seed int6422 printVersion bool23 proxyMetrics bool24 runtimeMetrics bool25}26func parseArguments() cliArguments {27 result := cliArguments{}28 flag.StringVar(&result.host, "host", "localhost",29 "Host for toxiproxy's API to listen on")30 flag.StringVar(&result.port, "port", "8474",31 "Port for toxiproxy's API to listen on")32 flag.StringVar(&result.config, "config", "",33 "JSON file containing proxies to create on startup")34 flag.Int64Var(&result.seed, "seed", time.Now().UTC().UnixNano(),35 "Seed for randomizing toxics with")36 flag.BoolVar(&result.runtimeMetrics, "runtime-metrics", false,37 `enable runtime-related prometheus metrics (default "false")`)38 flag.BoolVar(&result.proxyMetrics, "proxy-metrics", false,39 `enable toxiproxy-specific prometheus metrics (default "false")`)40 flag.BoolVar(&result.printVersion, "version", false,41 `print the version (default "false")`)42 flag.Parse()43 return result44}45func main() {46 err := run()47 if err != nil {48 fmt.Printf("error: %v", err)49 os.Exit(1)50 }51 os.Exit(0)52}53func run() error {54 cli := parseArguments()55 if cli.printVersion {56 fmt.Printf("toxiproxy-server version %s\n", toxiproxy.Version)57 return nil58 }59 rand.Seed(cli.seed)60 logger := setupLogger()61 log.Logger = logger62 logger.63 Info().64 Str("version", toxiproxy.Version).65 Msg("Starting Toxiproxy")66 metrics := toxiproxy.NewMetricsContainer(prometheus.NewRegistry())67 server := toxiproxy.NewServer(metrics, logger)68 if cli.proxyMetrics {69 server.Metrics.ProxyMetrics = collectors.NewProxyMetricCollectors()70 }71 if cli.runtimeMetrics {72 server.Metrics.RuntimeMetrics = collectors.NewRuntimeMetricCollectors()73 }74 if len(cli.config) > 0 {75 server.PopulateConfig(cli.config)76 }77 go func(server *toxiproxy.ApiServer, host, port string) {78 err := server.Listen(host, port)79 if err != nil {80 server.Logger.Err(err).Msg("Server finished with error")81 }82 }(server, cli.host, cli.port)83 signals := make(chan os.Signal, 1)84 signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)85 <-signals86 server.Logger.Info().Msg("Shutdown started")87 err := server.Shutdown()88 if err != nil {89 logger.Err(err).Msg("Shutdown finished with error")90 }91 return nil92}93func setupLogger() zerolog.Logger {94 zerolog.TimestampFunc = func() time.Time {95 return time.Now().UTC()96 }97 zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {98 short := file99 for i := len(file) - 1; i > 0; i-- {100 if file[i] == '/' {101 short = file[i+1:]102 break103 }104 }105 file = short106 return file + ":" + strconv.Itoa(line)107 }108 logger := zerolog.New(os.Stdout).With().Caller().Timestamp().Logger()109 val, ok := os.LookupEnv("LOG_LEVEL")110 if !ok {111 return logger112 }113 lvl, err := zerolog.ParseLevel(val)114 if err == nil {115 logger = logger.Level(lvl)116 } else {117 l := &logger118 l.Err(err).Msgf("unknown LOG_LEVEL value: \"%s\"", val)119 }120 return logger121}...
client.go
Source:client.go
...8)9func (S *Stub) RedisBackendDown(w http.ResponseWriter, r *http.Request) {10 // Test that redis is down11 S.RedisProxy.Disable()12 defer S.RedisProxy.Enable()13 _, err := redis.Dial("tcp", ":7379")14 if err != nil {15 S.RedisDownHits.Increment()16 fmt.Println("increm red down")17 }18}19func (S *Stub) RedisBackendUp(w http.ResponseWriter, r *http.Request) {20 // Test that redis is Up21 _, err := redis.Dial("tcp", ":7379")22 if err == nil {23 S.RedisUpHits.Increment()24 fmt.Println("increm red up")25 }26}27func (S *Stub) GitcheckUp(w http.ResponseWriter, r *http.Request) {28 // Test connection through proxy.Listen29 fmt.Println("gitup proxy listen", S.ToxiProxy.Listen)30 resp, err := http.Get("http://" + S.ToxiProxy.Listen)31 fmt.Println("response from gitup", resp)32 if err == nil {33 S.GitUpHits.Increment()34 } else {35 S.GitDownHits.Increment()36 }37}38func (S *Stub) GitcheckDown(w http.ResponseWriter, r *http.Request) {39 fmt.Println("Git checkdown proxy is ", S.ToxiProxy)40 S.ToxiProxy.Disable() //Disabling the proxy will simulate git down41 defer S.ToxiProxy.Enable()42 // Test connection through proxy.Listen43 resp, err := http.Get("http://" + S.ToxiProxy.Listen)44 fmt.Println("response from gitdown", resp)45 if err != nil || resp.StatusCode != 200 {46 S.GitDownHits.Increment()47 }48}49func (S *Stub) RedisBackendSlow(w http.ResponseWriter, r *http.Request) {50 S.RedisProxy.AddToxic("latency_downstream", "latency", "downstream", 1, toxiproxy.Attributes{51 "latency": 1000,52 }) //This adds latency to the Redis connect request53 defer S.RedisProxy.RemoveToxic("latency_downstream")54 // Test that redis is slow55 start := time.Now()...
Enable
Using AI Code Generation
1import (2func main() {3 toxiproxyClient := toxiproxy.NewClient("localhost:8474")4 toxiproxyClient.Enable("redis")5 fmt.Println("Toxiproxy enabled")6}7import (8func main() {9 toxiproxyClient := toxiproxy.NewClient("localhost:8474")10 toxiproxyClient.Disable("redis")11 fmt.Println("Toxiproxy disabled")12}
Enable
Using AI Code Generation
1import (2func main() {3 toxiproxyClient.Enable("redis")4}5import (6func main() {7 toxiproxyClient.Disable("redis")8}9import (10func main() {11 toxiproxyClient.Update("redis", 1234)12}13import (14func main() {15 toxiproxyClient.Delete("redis")16}17import (18func main() {19 toxiproxyClient.DeleteAll()20}21import (22func main() {23 proxy, err := toxiproxyClient.Create(&toxiproxy.Proxy{24 })25 if err != nil {
Enable
Using AI Code Generation
1import (2func main() {3 client := toxiproxy.NewClient("localhost:8474")4 err := client.Enable("proxy1")5 if err != nil {6 fmt.Println(err)7 }8}9import (10func main() {11 client := toxiproxy.NewClient("localhost:8474")12 err := client.Disable("proxy1")13 if err != nil {14 fmt.Println(err)15 }16}17import (18func main() {19 client := toxiproxy.NewClient("localhost:8474")20 err := client.Reset("proxy1")21 if err != nil {22 fmt.Println(err)23 }24}25import (26func main() {27 client := toxiproxy.NewClient("localhost:8474")28 proxy := toxiproxy.Proxy{
Enable
Using AI Code Generation
1import (2func main() {3 c, err := client.NewClient("localhost:8474")4 if err != nil {5 fmt.Println(err)6 }7 err = c.Proxy("test").Enable("latency")8 if err != nil {9 fmt.Println(err)10 }11}12import (13func main() {14 c, err := client.NewClient("localhost:8474")15 if err != nil {16 fmt.Println(err)17 }18 err = c.Proxy("test").Disable("latency")19 if err != nil {20 fmt.Println(err)21 }22}23import (24func main() {25 c, err := client.NewClient("localhost:8474")26 if err != nil {27 fmt.Println(err)28 }29 err = c.Proxy("test").Reset("latency")30 if err != nil {31 fmt.Println(err)32 }33}34import (35func main() {36 c, err := client.NewClient("localhost:8474")37 if err != nil {38 fmt.Println(err)39 }40 err = c.Proxy("test").Remove("latency")41 if err != nil {42 fmt.Println(err)43 }44}45import (46func main() {
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!!