fix(m2-pr-d): thread ctx through Job/Notification/Audit services

Collapse CancelJobWithContext into CancelJob; eliminate 10 context.Background()
hits across the Job+Notification+Audit service cluster by threading ctx
through their handler-facing service interfaces.

Services (ctx-first):
- service/job.go: ListJobs, GetJob, CancelJob, ApproveJob, RejectJob now
  accept ctx; the CancelJobWithContext wrapper is removed (handler callers
  continue to invoke CancelJob, now ctx-aware).
- service/notification.go: ListNotifications, GetNotification, MarkAsRead
  accept ctx.
- service/audit.go: ListAuditEvents, GetAuditEvent accept ctx.

Handlers (interface + callsites):
- handler/jobs.go, handler/notifications.go, handler/audit.go: local
  service interfaces updated, r.Context() threaded at every callsite.

Tests:
- Mock services updated to match the new interfaces (ctx accepted and
  ignored via '_ context.Context' first parameter; Fn closure fields
  unchanged).
- job_test.go / notification_test.go callsites thread context.Background()
  to match production shape.

Verification:
  go build ./...                 ok
  go vet ./...                   ok
  go test -short ./...           ok
  go test -race -short ./...     ok
  golangci-lint run ./...        0 issues

Locked decisions from the M-2 plan:
  D-1 ctx-only signatures (no dual forms)
  D-4 preserve handler method names facing the router
  D-5 domain types stay ctx-free

Audit complete. Commit: 1f6cf0eafa. Sections: 12. Findings: 2/7/10/4/6.
This commit is contained in:
shankar0123
2026-04-18 01:20:46 +00:00
parent 478a141498
commit ccd89c348f
11 changed files with 67 additions and 63 deletions
+11 -5
View File
@@ -99,7 +99,7 @@ func TestCancelJob(t *testing.T) {
jobService := newTestJobService(jobRepo)
err := jobService.CancelJobWithContext(ctx, "job-001")
err := jobService.CancelJob(ctx, "job-001")
if err != nil {
t.Fatalf("CancelJob failed: %v", err)
}
@@ -129,13 +129,15 @@ func TestCancelJob_AlreadyCompleted(t *testing.T) {
jobService := newTestJobService(jobRepo)
err := jobService.CancelJobWithContext(ctx, "job-001")
err := jobService.CancelJob(ctx, "job-001")
if err == nil {
t.Fatal("expected error for completed job")
}
}
func TestGetJob(t *testing.T) {
ctx := context.Background()
now := time.Now()
job := &domain.Job{
ID: "job-001",
@@ -153,7 +155,7 @@ func TestGetJob(t *testing.T) {
jobService := newTestJobService(jobRepo)
retrieved, err := jobService.GetJob("job-001")
retrieved, err := jobService.GetJob(ctx, "job-001")
if err != nil {
t.Fatalf("GetJob failed: %v", err)
}
@@ -167,6 +169,8 @@ func TestGetJob(t *testing.T) {
}
func TestListJobs(t *testing.T) {
ctx := context.Background()
now := time.Now()
job1 := &domain.Job{
ID: "job-001",
@@ -192,7 +196,7 @@ func TestListJobs(t *testing.T) {
jobService := newTestJobService(jobRepo)
jobs, total, err := jobService.ListJobs("", "", 1, 50)
jobs, total, err := jobService.ListJobs(ctx, "", "", 1, 50)
if err != nil {
t.Fatalf("ListJobs failed: %v", err)
}
@@ -206,6 +210,8 @@ func TestListJobs(t *testing.T) {
}
func TestListJobs_FilterByStatus(t *testing.T) {
ctx := context.Background()
now := time.Now()
job1 := &domain.Job{
ID: "job-001",
@@ -231,7 +237,7 @@ func TestListJobs_FilterByStatus(t *testing.T) {
jobService := newTestJobService(jobRepo)
jobs, total, err := jobService.ListJobs(string(domain.JobStatusPending), "", 1, 50)
jobs, total, err := jobService.ListJobs(ctx, string(domain.JobStatusPending), "", 1, 50)
if err != nil {
t.Fatalf("ListJobs failed: %v", err)
}