Best Testcontainers-go code snippet using wait_test.State
wait_test.go
Source:wait_test.go
...47 t.Parallel()48 ctx, cancel := context.WithTimeout(context.Background(), time.Second)49 defer cancel()50 f := &mock.Fetcher{51 StatusFunc: func(ctx context.Context) (*types.ContainerState, error) {52 status := "unhealthy"53 d, _ := ctx.Deadline()54 remain := d.Sub(time.Now())55 if remain < 500*time.Millisecond {56 status = "healthy"57 }58 return &types.ContainerState{59 Health: &types.Health{60 Status: status,61 },62 }, nil63 },64 }65 time.Sleep(300 * time.Millisecond)66 ok, err := wait.CheckHealthy(ctx, f)67 if err != nil {68 t.Fatal(err)69 } else if ok {70 t.Fatal("unexpected complete")71 }72 time.Sleep(300 * time.Millisecond)73 ok, err = wait.CheckHealthy(ctx, f)74 if err != nil {75 t.Fatal(err)76 } else if !ok {77 t.Fatal("expected to be completed")78 }79}80func TestWaiter_Wait(t *testing.T) {81 t.Parallel()82 ctx := context.Background()83 w := wait.New(func(ctx context.Context, f wait.Fetcher) (bool, error) {84 status, err := f.Status(ctx)85 if err != nil {86 return false, err87 }88 return status.Status == "running", nil89 }, wait.WithInterval(100*time.Millisecond), wait.WithTimeout(700*time.Millisecond))90 t.Run("success", func(t *testing.T) {91 t.Parallel()92 var count int93 f := &mock.Fetcher{94 StatusFunc: func(ctx context.Context) (*types.ContainerState, error) {95 status := "created"96 count++97 d, _ := ctx.Deadline()98 remain := d.Sub(time.Now())99 if remain < 200*time.Millisecond {100 status = "running"101 }102 return &types.ContainerState{103 Status: status,104 }, nil105 },106 }107 err := w.Wait(ctx, f)108 if err != nil {109 t.Fatal(err)110 }111 if count < 4 {112 t.Fatal("unexpected count of try to check status")113 }114 })115 t.Run("timeout", func(t *testing.T) {116 t.Parallel()117 var count int118 f := &mock.Fetcher{119 StatusFunc: func(ctx context.Context) (*types.ContainerState, error) {120 count++121 return &types.ContainerState{122 Status: "created",123 }, nil124 },125 }126 err := w.Wait(ctx, f)127 if err == nil {128 t.Fatal("unexpected success")129 }130 if count < 6 {131 t.Fatal("unexpected count of try to check status")132 }133 })134}...
exec_test.go
Source:exec_test.go
...50 return 0, st.failure51 }52 return st.exitCode, st.failure53}54func (st mockExecTarget) State(_ context.Context) (*types.ContainerState, error) {55 return nil, errors.New("not implemented")56}57func TestExecStrategyWaitUntilReady(t *testing.T) {58 target := mockExecTarget{}59 wg := wait.NewExecStrategy([]string{"true"}).60 WithStartupTimeout(30 * time.Second)61 err := wg.WaitUntilReady(context.Background(), target)62 if err != nil {63 t.Fatal(err)64 }65}66func TestExecStrategyWaitUntilReadyForExec(t *testing.T) {67 target := mockExecTarget{}68 wg := wait.ForExec([]string{"true"})...
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!!