How to use AssertDeltaTime method of toxics_test Package

Best Toxiproxy code snippet using toxics_test.AssertDeltaTime

latency_test.go

Source:latency_test.go Github

copy

Full Screen

...10 "time"11 "github.com/Shopify/toxiproxy"12 "github.com/Shopify/toxiproxy/toxics"13)14func AssertDeltaTime(t *testing.T, message string, actual, expected, delta time.Duration) {15 diff := actual - expected16 if diff < 0 {17 diff *= -118 }19 if diff > delta {20 t.Errorf("[%s] Time was more than %v off: got %v expected %v", message, delta, actual, expected)21 } else {22 t.Logf("[%s] Time was correct: %v (expected %v)", message, actual, expected)23 }24}25func DoLatencyTest(t *testing.T, upLatency, downLatency *toxics.LatencyToxic) {26 WithEchoProxy(t, func(conn net.Conn, response chan []byte, proxy *toxiproxy.Proxy) {27 if upLatency == nil {28 upLatency = &toxics.LatencyToxic{}29 } else {30 _, err := proxy.Toxics.AddToxicJson(ToxicToJson(t, "latency_up", "latency", "upstream", upLatency))31 if err != nil {32 t.Error("AddToxicJson returned error:", err)33 }34 }35 if downLatency == nil {36 downLatency = &toxics.LatencyToxic{}37 } else {38 _, err := proxy.Toxics.AddToxicJson(ToxicToJson(t, "latency_down", "latency", "downstream", downLatency))39 if err != nil {40 t.Error("AddToxicJson returned error:", err)41 }42 }43 t.Logf("Using latency: Up: %dms +/- %dms, Down: %dms +/- %dms", upLatency.Latency, upLatency.Jitter, downLatency.Latency, downLatency.Jitter)44 msg := []byte("hello world " + strings.Repeat("a", 32*1024) + "\n")45 timer := time.Now()46 _, err := conn.Write(msg)47 if err != nil {48 t.Error("Failed writing to TCP server", err)49 }50 resp := <-response51 if !bytes.Equal(resp, msg) {52 t.Error("Server didn't read correct bytes from client:", string(resp))53 }54 AssertDeltaTime(t,55 "Server read",56 time.Since(timer),57 time.Duration(upLatency.Latency)*time.Millisecond,58 time.Duration(upLatency.Jitter+10)*time.Millisecond,59 )60 timer2 := time.Now()61 scan := bufio.NewScanner(conn)62 if scan.Scan() {63 resp = append(scan.Bytes(), '\n')64 if !bytes.Equal(resp, msg) {65 t.Error("Client didn't read correct bytes from server:", string(resp))66 }67 }68 AssertDeltaTime(t,69 "Client read",70 time.Since(timer2),71 time.Duration(downLatency.Latency)*time.Millisecond,72 time.Duration(downLatency.Jitter+10)*time.Millisecond,73 )74 AssertDeltaTime(t,75 "Round trip",76 time.Since(timer),77 time.Duration(upLatency.Latency+downLatency.Latency)*time.Millisecond,78 time.Duration(upLatency.Jitter+downLatency.Jitter+20)*time.Millisecond,79 )80 proxy.Toxics.RemoveToxic("latency_up")81 proxy.Toxics.RemoveToxic("latency_down")82 err = conn.Close()83 if err != nil {84 t.Error("Failed to close TCP connection", err)85 }86 })87}88func TestUpstreamLatency(t *testing.T) {89 DoLatencyTest(t, &toxics.LatencyToxic{Latency: 100}, nil)90}91func TestDownstreamLatency(t *testing.T) {92 DoLatencyTest(t, nil, &toxics.LatencyToxic{Latency: 100})93}94func TestFullstreamLatencyEven(t *testing.T) {95 DoLatencyTest(t, &toxics.LatencyToxic{Latency: 100}, &toxics.LatencyToxic{Latency: 100})96}97func TestFullstreamLatencyBiasUp(t *testing.T) {98 DoLatencyTest(t, &toxics.LatencyToxic{Latency: 1000}, &toxics.LatencyToxic{Latency: 100})99}100func TestFullstreamLatencyBiasDown(t *testing.T) {101 DoLatencyTest(t, &toxics.LatencyToxic{Latency: 100}, &toxics.LatencyToxic{Latency: 1000})102}103func TestZeroLatency(t *testing.T) {104 DoLatencyTest(t, &toxics.LatencyToxic{Latency: 0}, &toxics.LatencyToxic{Latency: 0})105}106func TestLatencyToxicCloseRace(t *testing.T) {107 ln, err := net.Listen("tcp", "localhost:0")108 if err != nil {109 t.Fatal("Failed to create TCP server", err)110 }111 defer ln.Close()112 proxy := NewTestProxy("test", ln.Addr().String())113 proxy.Start()114 defer proxy.Stop()115 go func() {116 for {117 _, err := ln.Accept()118 if err != nil {119 return120 }121 }122 }()123 // Check for potential race conditions when interrupting toxics124 for i := 0; i < 1000; i++ {125 proxy.Toxics.AddToxicJson(ToxicToJson(t, "", "latency", "upstream", &toxics.LatencyToxic{Latency: 10}))126 conn, err := net.Dial("tcp", proxy.Listen)127 if err != nil {128 t.Error("Unable to dial TCP server", err)129 }130 conn.Write([]byte("hello"))131 conn.Close()132 proxy.Toxics.RemoveToxic("latency")133 }134}135func TestTwoLatencyToxics(t *testing.T) {136 WithEchoProxy(t, func(conn net.Conn, response chan []byte, proxy *toxiproxy.Proxy) {137 toxics := []*toxics.LatencyToxic{&toxics.LatencyToxic{Latency: 500}, &toxics.LatencyToxic{Latency: 500}}138 for i, toxic := range toxics {139 _, err := proxy.Toxics.AddToxicJson(ToxicToJson(t, "latency_"+strconv.Itoa(i), "latency", "upstream", toxic))140 if err != nil {141 t.Error("AddToxicJson returned error:", err)142 }143 }144 msg := []byte("hello world " + strings.Repeat("a", 32*1024) + "\n")145 timer := time.Now()146 _, err := conn.Write(msg)147 if err != nil {148 t.Error("Failed writing to TCP server", err)149 }150 resp := <-response151 if !bytes.Equal(resp, msg) {152 t.Error("Server didn't read correct bytes from client:", string(resp))153 }154 AssertDeltaTime(t,155 "Upstream two latency toxics",156 time.Since(timer),157 time.Duration(1000)*time.Millisecond,158 time.Duration(10)*time.Millisecond,159 )160 for i := range toxics {161 proxy.Toxics.RemoveToxic("latency_" + strconv.Itoa(i))162 }163 err = conn.Close()164 if err != nil {165 t.Error("Failed to close TCP connection", err)166 }167 })168}169func TestLatencyToxicBandwidth(t *testing.T) {170 ln, err := net.Listen("tcp", "localhost:0")171 if err != nil {172 t.Fatal("Failed to create TCP server", err)173 }174 defer ln.Close()175 proxy := NewTestProxy("test", ln.Addr().String())176 proxy.Start()177 defer proxy.Stop()178 buf := []byte(strings.Repeat("hello world ", 1000))179 go func() {180 conn, err := ln.Accept()181 if err != nil {182 t.Error("Unable to accept TCP connection", err)183 }184 for err == nil {185 _, err = conn.Write(buf)186 }187 }()188 conn, err := net.Dial("tcp", proxy.Listen)189 if err != nil {190 t.Error("Unable to dial TCP server", err)191 }192 proxy.Toxics.AddToxicJson(ToxicToJson(t, "", "latency", "", &toxics.LatencyToxic{Latency: 100}))193 time.Sleep(150 * time.Millisecond) // Wait for latency toxic194 buf2 := make([]byte, len(buf))195 start := time.Now()196 count := 0197 for i := 0; i < 100; i++ {198 n, err := io.ReadFull(conn, buf2)199 count += n200 if err != nil {201 t.Error(err)202 break203 }204 }205 // Assert the transfer was at least 100MB/s206 AssertDeltaTime(t, "Latency toxic bandwidth", time.Since(start), 0, time.Duration(count/100000)*time.Millisecond)207 err = conn.Close()208 if err != nil {209 t.Error("Failed to close TCP connection", err)210 }211}...

Full Screen

Full Screen

AssertDeltaTime

Using AI Code Generation

copy

Full Screen

1import (2func TestToxic(t *testing.T) {3 proxy := toxiproxy.NewProxy()4 proxy.Toxics = toxiproxy.Toxics{}5 err := proxy.Save()6 if err != nil {7 t.Fatal(err)8 }9 client, err := client.NewClient("localhost:8474")10 if err != nil {11 t.Fatal(err)12 }13 toxic := &toxics.DeltaDelayToxic{14 Toxic: toxics.Toxic{15 Stream: toxics.ToxicStream("downstream"),

Full Screen

Full Screen

AssertDeltaTime

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 toxic := &toxics.LatencyToxic{4 }5 toxicTest := toxics_test.NewToxicTest(toxic, nil)6 toxicTest.AssertDeltaTime(1000 * time.Millisecond, 100 * time.Millisecond)7}8import (9type ToxicTest struct {10}11func NewToxicTest(toxic *toxics.ToxicStub, proxy *toxics.ProxyStub) *ToxicTest {12 return &ToxicTest{toxic}13}14func (t *ToxicTest) AssertDeltaTime(expected time.Duration, jitter time.Duration) {15 start := time.Now()16 t.toxic.SetDelay(expected, jitter)17 end := time.Now()18 actual := end.Sub(start)19 if actual < expected - jitter || actual > expected + jitter {20 fmt.Println("Error")21 }22}23import (24type ToxicStub struct {25}26func (t *ToxicStub) SetDelay(delay time.Duration, jitter time.Duration) {27}

Full Screen

Full Screen

AssertDeltaTime

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client := toxiproxy.NewClient("localhost:8474")4 proxy, err := client.CreateProxy("my_redis", "localhost:6379", "localhost:9736")5 if err != nil {6 log.Fatal(err)7 }8 defer client.DeleteProxy(proxy.Name)9 toxic, err := proxy.CreateToxic("latency", "latency", "downstream", 1.0, toxiproxy.Attributes{10 })11 if err != nil {12 log.Fatal(err)13 }14 defer proxy.DeleteToxic(toxic.Name)15 time.Sleep(1 * time.Second)16 toxics, err := proxy.Toxics()17 if err != nil {18 log.Fatal(err)19 }20 for _, t := range toxics {21 if t.Name == toxic.Name {22 fmt.Println("Toxic is still there!")23 }24 }25}26import (27func main() {28 client := toxiproxy.NewClient("localhost:8474")29 proxy, err := client.CreateProxy("my_redis", "localhost:6379", "localhost:9736")30 if err != nil {31 log.Fatal(err)32 }33 defer client.DeleteProxy(proxy.Name)34 toxic, err := proxy.CreateToxic("latency", "latency", "downstream", 1.0, toxiproxy.Attributes{35 })36 if err != nil {37 log.Fatal(err)38 }39 defer proxy.DeleteToxic(toxic.Name)40 toxics, err := proxy.Toxics()41 if err != nil {42 log.Fatal(err)43 }44 for _, t := range toxics {45 if t.Name == toxic.Name {46 fmt.Println("

Full Screen

Full Screen

AssertDeltaTime

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 toxics_test := toxiproxy.NewClient("localhost:8474")4 toxics_test.CreateProxy("test", "localhost:8080", "localhost:8081")5 toxics_test.AddToxic("test", "test", "latency", "downstream", 1, stream.ToxicAttributes{6 })7 start := time.Now()8 toxics_test.AssertDeltaTime("test", "test", 1000, 100)9 fmt.Printf("Delta time: %v\n", time.Since(start))10}11import (12func main() {13 toxics_test := toxiproxy.NewClient("localhost:8474")14 toxics_test.CreateProxy("test", "localhost:8080", "localhost:8081")15 toxics_test.AddToxic("test", "test", "latency", "downstream", 1, stream.ToxicAttributes{16 })17 start := time.Now()18 toxics_test.AssertDelta("test", "test", 1000, 100)19 fmt.Printf("Delta time: %v\n", time.Since(start))20}21import (22func main() {23 toxics_test := toxiproxy.NewClient("localhost:8474")24 toxics_test.CreateProxy("test", "localhost:8080", "localhost:8081")25 toxics_test.AddToxic("test", "test", "latency", "downstream", 1, stream.ToxicAttributes{

Full Screen

Full Screen

AssertDeltaTime

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 toxics_test := toxics.NewToxics()4 toxics_test.AssertDeltaTime(time.Second, time.Second, 0)5 fmt.Println("success")6}7import (8func main() {9 toxics_test := toxics.NewToxics()10 toxics_test.AssertDeltaTime(time.Second, time.Second, 0)11 fmt.Println("success")12}13import (14func main() {15 toxics_test := toxics.NewToxics()16 toxics_test.AssertDeltaTime(time.Second, time.Second, 0)17 fmt.Println("success")18}19import (20func main() {21 toxics_test := toxics.NewToxics()22 toxics_test.AssertDeltaTime(time.Second, time.Second, 0)23 fmt.Println("success")24}25import (26func main() {27 toxics_test := toxics.NewToxics()28 toxics_test.AssertDeltaTime(time.Second, time.Second, 0)29 fmt.Println("success")30}

Full Screen

Full Screen

AssertDeltaTime

Using AI Code Generation

copy

Full Screen

1import (2func TestToxics(t *testing.T) {3 toxics := ToxicsTest{t}4 toxics.AssertDeltaTime(time.Second, func() {5 time.Sleep(time.Second)6 })7}8import (9func TestToxics(t *testing.T) {10 toxics := ToxicsTest{t}11 toxics.AssertDeltaTime(time.Second, func() {12 time.Sleep(time.Second)13 })14}15import (16func TestToxics(t *testing.T) {17 toxics := ToxicsTest{t}18 toxics.AssertDeltaTime(time.Second, func() {19 time.Sleep(time.Second)20 })21}22import (23func TestToxics(t *testing.T) {24 toxics := ToxicsTest{t}25 toxics.AssertDeltaTime(time.Second, func() {26 time.Sleep(time.Second)27 })28}29import (30func TestToxics(t *testing.T) {31 toxics := ToxicsTest{t}32 toxics.AssertDeltaTime(time.Second, func() {33 time.Sleep(time.Second)34 })35}36import (37func TestToxics(t *testing.T) {38 toxics := ToxicsTest{t}39 toxics.AssertDeltaTime(time.Second, func() {40 time.Sleep(time.Second)41 })42}43import (44func TestToxics(t *testing.T) {45 toxics := ToxicsTest{t}46 toxics.AssertDeltaTime(time

Full Screen

Full Screen

AssertDeltaTime

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 toxics_test := NewToxicsTest()4 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 1")5 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 2")6 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 3")7 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 4")8 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 5")9 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 6")10 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 7")11 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 8")12 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 9")13 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 10")14 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 11")15 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 12")16 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 13")17 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 14")18 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 15")19 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 16")20 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 17")21 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 18")22 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 19")23 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 20")24 toxics_test.AssertDeltaTime(1000, 1000, 50, "Test 21")25 toxics_test.AssertDeltaTime(

Full Screen

Full Screen

AssertDeltaTime

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 fmt.Println("test")5 t.AssertDeltaTime(time.Now(), time.Now(), 1)6}7import (8type toxics_test struct {9}10func (t *toxics_test) AssertDeltaTime(a, b time.Time, delta time.Duration) {11 if a.Sub(b) > delta || b.Sub(a) > delta {12 t.Errorf("Expected %v to be within %v of %v", a, delta, b)13 }14}15import (16type toxics_test struct {17}18func (t *toxics_test) AssertDeltaTime(a, b time.Time, delta time.Duration) {19 if a.Sub(b) > delta || b.Sub(a) > delta {20 t.Errorf("Expected %v to be within %v of %v", a, delta, b)21 }22}

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