Skip to content

Commit 322171f

Browse files
CROSSLINK-287 Fix copilot comments
1 parent d8d07b5 commit 322171f

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

broker/app/app.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ func Init(ctx context.Context) (Context, error) {
199199
return Context{}, err
200200
}
201201

202-
schedRepoRepo := sched_db.CreateSchedRepo(pool)
203-
if err = StartScheduler(ctx, schedRepoRepo, eventBus); err != nil {
202+
schedRepo := sched_db.CreateSchedRepo(pool)
203+
if err = StartScheduler(ctx, schedRepo, eventBus); err != nil {
204204
return Context{}, err
205205
}
206206

broker/scheduler/service/scheduler.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ func NewSchedulerService(schedRepo sched_db.SchedRepo, eventBus events.EventBus,
4545
}
4646
}
4747

48-
// Listen opens a dedicated Postgres connection and listens on sched_db.SchedulerChannel.
49-
// Each notification wakes the scheduler loop. Reconnects with exponential
50-
// backoff on connection loss. Blocks until ctx is cancelled.
48+
// Listen opens a dedicated Postgres connection and listens on
49+
// sched_db.SchedulerChannel. Each notification wakes the scheduler loop.
50+
// After the initial connection and LISTEN registration succeed, it starts a
51+
// background goroutine to receive notifications and returns. The listener
52+
// reconnects with exponential backoff on connection loss and runs until ctx is
53+
// cancelled.
5154
func (s *SchedulerService) Listen(ctx common.ExtendedContext) error {
5255
// openConn establishes a fresh connection and registers the LISTEN.
5356
// The caller is responsible for closing the returned connection.

broker/test/scheduler/service/scheduler_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ type countingEventBus struct {
7373
claims []string
7474
}
7575

76-
func (b *countingEventBus) CreateTask(_ string, _ events.EventName, _ events.EventData, _ events.EventDomain, _ *string, _ events.SignalTarget) (string, error) {
76+
func (b *countingEventBus) CreateTask(_ string, _ events.EventName, data events.EventData, _ events.EventDomain, _ *string, _ events.SignalTarget) (string, error) {
7777
b.mu.Lock()
7878
defer b.mu.Unlock()
79-
b.claims = append(b.claims, uuid.NewString())
80-
return uuid.NewString(), nil
79+
taskId := uuid.New().String()
80+
b.claims = append(b.claims, data.Note)
81+
return taskId, nil
8182
}
8283

8384
func (b *countingEventBus) totalClaims() int {
@@ -86,11 +87,19 @@ func (b *countingEventBus) totalClaims() int {
8687
return len(b.claims)
8788
}
8889

90+
func (b *countingEventBus) getClaims() []string {
91+
b.mu.Lock()
92+
defer b.mu.Unlock()
93+
return b.claims
94+
}
95+
8996
func overdueTask() sched_db.SaveScheduledTaskParams {
97+
id := uuid.New().String()
9098
return sched_db.SaveScheduledTaskParams{
91-
ID: uuid.NewString(),
99+
ID: id,
92100
EventName: events.EventNameSendNotification,
93101
CronExpr: "",
102+
Payload: events.EventData{CommonEventData: events.CommonEventData{Note: id}},
94103
RunAt: pgtype.Timestamptz{Time: time.Now().Add(-1 * time.Second), Valid: true},
95104
Status: sched_db.ScheduledTaskStatusPending,
96105
CreatedAt: pgtype.Timestamptz{Time: time.Now(), Valid: true},
@@ -101,6 +110,7 @@ func startScheduler(t *testing.T, ctx context.Context, bus events.EventBus) {
101110
t.Helper()
102111
pool, err := app.InitDbPool()
103112
assert.NoError(t, err)
113+
t.Cleanup(pool.Close)
104114
repo := sched_db.CreateSchedRepo(pool)
105115
svc := sched_service.NewSchedulerService(repo, bus, connString)
106116
extCtx := common.CreateExtCtxWithArgs(ctx, nil)
@@ -145,9 +155,11 @@ func TestMultipleInstances_EachTaskClaimedOnce(t *testing.T) {
145155
bus := &countingEventBus{}
146156
ctx, cancel := context.WithCancel(context.Background())
147157
defer cancel()
158+
ids := []string{}
148159

149160
for i := 0; i < taskCount; i++ {
150-
_, err := schedRepo.SaveScheduledTask(appCtx, overdueTask())
161+
task, err := schedRepo.SaveScheduledTask(appCtx, overdueTask())
162+
ids = append(ids, task.ID)
151163
assert.NoError(t, err)
152164
}
153165

@@ -162,6 +174,7 @@ func TestMultipleInstances_EachTaskClaimedOnce(t *testing.T) {
162174
time.Sleep(150 * time.Millisecond)
163175

164176
assert.Equal(t, taskCount, bus.totalClaims(), "each task must be dispatched exactly once")
177+
assert.ElementsMatch(t, ids, bus.getClaims(), "each task must be claimed exactly once")
165178
}
166179

167180
// TestMultipleInstances_HighConcurrency runs 10 tasks across 5 instances and
@@ -242,6 +255,7 @@ func TestListen_ReconnectsAfterConnectionLoss(t *testing.T) {
242255
// Kill all LISTEN connections to simulate a network interruption.
243256
adminPool, err := app.InitDbPool()
244257
assert.NoError(t, err)
258+
t.Cleanup(adminPool.Close)
245259
killCtx := common.CreateExtCtxWithArgs(context.Background(), nil)
246260
_, err = adminPool.Exec(killCtx,
247261
`SELECT pg_terminate_backend(pid)
@@ -275,6 +289,7 @@ func TestScheduler_StopsOnContextCancel(t *testing.T) {
275289

276290
pool, err := app.InitDbPool()
277291
assert.NoError(t, err)
292+
t.Cleanup(pool.Close)
278293
repo := sched_db.CreateSchedRepo(pool)
279294
svc := sched_service.NewSchedulerService(repo, bus, connString)
280295
extCtx := common.CreateExtCtxWithArgs(ctx, nil)

0 commit comments

Comments
 (0)