Skip to content

Commit

Permalink
context propagation: explicit ctx parameter in unit tests (#3229)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Sep 19, 2024
1 parent b14201a commit 7c5d4d8
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 87 deletions.
19 changes: 11 additions & 8 deletions pkg/acquisition/modules/loki/loki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ query: >
delayFor: 1 * time.Second,
},
{

config: `
mode: tail
source: loki
Expand All @@ -111,7 +110,6 @@ query: >
testName: "Correct config with password",
},
{

config: `
mode: tail
source: loki
Expand Down Expand Up @@ -261,7 +259,7 @@ func TestConfigureDSN(t *testing.T) {
}
}

func feedLoki(logger *log.Entry, n int, title string) error {
func feedLoki(ctx context.Context, logger *log.Entry, n int, title string) error {
streams := LogStreams{
Streams: []LogStream{
{
Expand All @@ -286,7 +284,7 @@ func feedLoki(logger *log.Entry, n int, title string) error {
return err
}

req, err := http.NewRequest(http.MethodPost, "http://127.0.0.1:3100/loki/api/v1/push", bytes.NewBuffer(buff))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://127.0.0.1:3100/loki/api/v1/push", bytes.NewBuffer(buff))
if err != nil {
return err
}
Expand Down Expand Up @@ -344,12 +342,13 @@ since: 1h
subLogger := logger.WithField("type", "loki")
lokiSource := loki.LokiSource{}
err := lokiSource.Configure([]byte(ts.config), subLogger, configuration.METRICS_NONE)

if err != nil {
t.Fatalf("Unexpected error : %s", err)
}

err = feedLoki(subLogger, 20, title)
ctx := context.Background()

err = feedLoki(ctx, subLogger, 20, title)
if err != nil {
t.Fatalf("Unexpected error : %s", err)
}
Expand Down Expand Up @@ -421,6 +420,8 @@ query: >
},
}

ctx := context.Background()

for _, ts := range tests {
t.Run(ts.name, func(t *testing.T) {
logger := log.New()
Expand Down Expand Up @@ -472,7 +473,7 @@ query: >
}
})

err = feedLoki(subLogger, ts.expectedLines, title)
err = feedLoki(ctx, subLogger, ts.expectedLines, title)
if err != nil {
t.Fatalf("Unexpected error : %s", err)
}
Expand Down Expand Up @@ -525,7 +526,9 @@ query: >

time.Sleep(time.Second * 2)

err = feedLoki(subLogger, 1, title)
ctx := context.Background()

err = feedLoki(ctx, subLogger, 1, title)
if err != nil {
t.Fatalf("Unexpected error : %s", err)
}
Expand Down
Loading

0 comments on commit 7c5d4d8

Please sign in to comment.