Skip to content

Commit f2f3fe5

Browse files
jonfriesentylerflint
authored andcommitted
makes the http request async
1 parent 2aa3696 commit f2f3fe5

File tree

3 files changed

+55
-28
lines changed

3 files changed

+55
-28
lines changed

pkg/e2e/babel/babel.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package babel
22

33
import (
4+
"context"
45
"os"
56
"strings"
67

@@ -179,6 +180,20 @@ func AllImages() []HTTPRequestImage {
179180
return images
180181
}
181182

183+
type Container struct {
184+
testcontainers.Container
185+
resultCh chan ContainerResult
186+
}
187+
188+
func (c *Container) WaitForExit(ctx context.Context) (*ContainerResult, error) {
189+
select {
190+
case result := <-c.resultCh:
191+
return &result, nil
192+
case <-ctx.Done():
193+
return nil, ctx.Err()
194+
}
195+
}
196+
182197
type ContainerResult struct {
183198
ExitCode int
184199
Logs string

pkg/e2e/babel/request.go

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,11 @@ func BuildHTTPRequest() *HTTPRequestBuilder {
321321
}
322322
}
323323

324-
func (m *HTTPRequest) Run(ctx context.Context, l *zap.Logger) *ContainerResult {
325-
result := &ContainerResult{}
326-
var err error
324+
func (m *HTTPRequest) Run(ctx context.Context, l *zap.Logger) *Container {
325+
result := ContainerResult{}
326+
c := &Container{
327+
resultCh: make(chan ContainerResult),
328+
}
327329

328330
envVars := m.toEnvVars()
329331

@@ -337,7 +339,7 @@ func (m *HTTPRequest) Run(ctx context.Context, l *zap.Logger) *ContainerResult {
337339
},
338340
LogConsumerCfg: &testcontainers.LogConsumerConfig{
339341
Opts: []testcontainers.LogProductionOption{testcontainers.WithLogProductionTimeout(10 * time.Second)},
340-
Consumers: []testcontainers.LogConsumer{result},
342+
Consumers: []testcontainers.LogConsumer{&result},
341343
},
342344
WaitingFor: wait.ForExit().WithPollInterval(10 * time.Millisecond),
343345
AutoRemove: true, // Clean up container when it exits
@@ -355,29 +357,35 @@ func (m *HTTPRequest) Run(ctx context.Context, l *zap.Logger) *ContainerResult {
355357
// }
356358
// }
357359

358-
// Start the container
359-
l.Info("🕹️ starting container", zap.String("image", m.ImageURL))
360-
container, err := testcontainers.GenericContainer(ctx, req)
361-
if err != nil {
362-
l.Error("start container error", zap.Error(err))
363-
result.Error = fmt.Errorf("starting container %s: %w", m.ImageURL, err)
364-
result.ExitCode = -1
365-
return result
366-
}
360+
go func() {
361+
// Start the container
362+
l.Info("🕹️ starting container", zap.String("image", m.ImageURL))
363+
container, err := testcontainers.GenericContainer(ctx, req)
364+
if err != nil {
365+
l.Error("start container error", zap.Error(err))
366+
result.Error = fmt.Errorf("starting container %s: %w", m.ImageURL, err)
367+
result.ExitCode = -1
368+
c.resultCh <- result
369+
return
370+
}
367371

368-
state, err := container.State(ctx)
369-
if err != nil {
370-
l.Error("get container state error", zap.Error(err))
371-
result.Error = fmt.Errorf("getting container state: %w", err)
372-
result.ExitCode = -1
373-
return result
374-
}
372+
state, err := container.State(ctx)
373+
if err != nil {
374+
l.Error("get container state error", zap.Error(err))
375+
result.Error = fmt.Errorf("getting container state: %w", err)
376+
result.ExitCode = -1
377+
c.resultCh <- result
378+
return
379+
}
375380

376-
l.Debug("container state", zap.Any("state", state))
377-
result.ExitCode = state.ExitCode
378-
if state.Error != "" {
379-
result.Error = fmt.Errorf("container error: %s", state.Error)
380-
}
381+
l.Debug("container state", zap.Any("state", state))
382+
result.ExitCode = state.ExitCode
383+
if state.Error != "" {
384+
result.Error = fmt.Errorf("container error: %s", state.Error)
385+
}
386+
387+
c.resultCh <- result
388+
}()
381389

382-
return result
390+
return c
383391
}

pkg/e2e/e2e.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,13 @@ func (c *TestContext) Do(request *babel.HTTPRequest) ExecResult {
196196
request.WithExtraEnvVar("QPOINT_TAGS", fmt.Sprintf("ctxid:%s,ctxid:%s", c.ID, id))
197197
c.L.Info("🕹️ executing babel http request", zap.String("image", request.ImageURL))
198198

199-
result := request.Run(c.T.Context(), c.L)
199+
c.L.Debug("✅ babel http request starting", zap.String("image", request.ImageURL))
200+
container := request.Run(c.T.Context(), c.L)
200201

201-
c.L.Debug("✅ babel http request executed", zap.String("image", request.ImageURL), zap.String("result", result.Logs))
202+
c.L.Debug("⏱️ Waiting for babel http container to exit", zap.String("image", request.ImageURL))
203+
result, err := container.WaitForExit(c.T.Context())
204+
require.NoError(c.T, err)
205+
c.L.Debug("✅ babel http request finished", zap.String("image", request.ImageURL), zap.String("result", result.Logs))
202206

203207
return ExecResult{
204208
Output: result.Logs,

0 commit comments

Comments
 (0)