Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context propagation: explicit ctx parameter in unit tests #3229

Merged
merged 4 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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