Skip to content

Commit c17e0c9

Browse files
committed
fix(notification): remove buffer size parameter from new question email worker for test
1 parent b70dda9 commit c17e0c9

2 files changed

Lines changed: 29 additions & 38 deletions

File tree

internal/service/notification/new_question_email_worker_test.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func TestNewQuestionEmailWorkerBuildsFreshRawDataPerAttempt(t *testing.T) {
346346
}
347347

348348
func TestNewQuestionEmailWorkerTryEnqueueCopiesTaskAndFailsFast(t *testing.T) {
349-
worker := newUnstartedNewQuestionEmailWorkerForTest(1)
349+
worker := newUnstartedNewQuestionEmailWorkerForTest()
350350
task := newQuestionEmailTask{
351351
UserIDs: []string{"user-1"},
352352
QuestionTitle: "Question",
@@ -380,7 +380,7 @@ func TestNewQuestionEmailWorkerTryEnqueueCopiesTaskAndFailsFast(t *testing.T) {
380380
t.Fatalf("TryEnqueue() after Close() = true, want false")
381381
}
382382

383-
canceledWorker := newUnstartedNewQuestionEmailWorkerForTest(1)
383+
canceledWorker := newUnstartedNewQuestionEmailWorkerForTest()
384384
canceledWorker.cancel()
385385
if canceledWorker.TryEnqueue(newQuestionEmailWorkerTask("question-5", "user-5")) {
386386
t.Fatalf("TryEnqueue() after cancel = true, want false")
@@ -393,8 +393,8 @@ func TestNewQuestionEmailWorkerTryEnqueueConcurrentClose(t *testing.T) {
393393
senders = 32
394394
)
395395

396-
for iteration := 0; iteration < iterations; iteration++ {
397-
worker := newUnstartedNewQuestionEmailWorkerForTest(1)
396+
for iteration := range iterations {
397+
worker := newUnstartedNewQuestionEmailWorkerForTest()
398398
if !worker.TryEnqueue(newQuestionEmailWorkerTask("already-queued", "queued-user")) {
399399
t.Fatalf("iteration %d: pre-fill TryEnqueue() = false, want true", iteration)
400400
}
@@ -406,10 +406,8 @@ func TestNewQuestionEmailWorkerTryEnqueueConcurrentClose(t *testing.T) {
406406
var acceptedAfterCloseObserved atomic.Int64
407407
var wg sync.WaitGroup
408408

409-
for sender := 0; sender < senders; sender++ {
410-
wg.Add(1)
411-
go func(sender int) {
412-
defer wg.Done()
409+
for range senders {
410+
wg.Go(func() {
413411
defer func() {
414412
if recovered := recover(); recovered != nil {
415413
panicCh <- recovered
@@ -428,9 +426,9 @@ func TestNewQuestionEmailWorkerTryEnqueueConcurrentClose(t *testing.T) {
428426
}
429427
runtime.Gosched()
430428
}
431-
}(sender)
429+
})
432430
}
433-
for sender := 0; sender < senders; sender++ {
431+
for range senders {
434432
<-ready
435433
}
436434

@@ -533,10 +531,10 @@ func newQuestionEmailWorkerTask(questionID string, userIDs ...string) newQuestio
533531
}
534532
}
535533

536-
func newUnstartedNewQuestionEmailWorkerForTest(bufferSize int) *newQuestionEmailWorker {
534+
func newUnstartedNewQuestionEmailWorkerForTest() *newQuestionEmailWorker {
537535
ctx, cancel := context.WithCancel(context.Background())
538536
return &newQuestionEmailWorker{
539-
tasks: make(chan newQuestionEmailTask, bufferSize),
537+
tasks: make(chan newQuestionEmailTask, 1),
540538
interval: func() time.Duration { return 0 },
541539
timerFactory: newRealNewQuestionEmailTimer,
542540
ctx: ctx,

internal/service/notification/new_question_notification_test.go

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func TestHandleNewQuestionNotificationEnqueuesEmailTask(t *testing.T) {
181181
},
182182
siteInfoService: siteInfoService,
183183
}
184-
service.newQuestionEmailWorker = newUnstartedNewQuestionEmailWorkerForTest(1)
184+
service.newQuestionEmailWorker = newUnstartedNewQuestionEmailWorkerForTest()
185185

186186
err = service.handleNewQuestionNotification(context.Background(), &schema.ExternalNotificationMsg{
187187
NewQuestionTemplateRawData: &schema.NewQuestionTemplateRawData{
@@ -243,7 +243,7 @@ func TestHandleNewQuestionNotificationSkipsEnqueueWithoutEnabledEmailAttempts(t
243243
"all-user": newQuestionNotificationTestUser("all-user"),
244244
},
245245
},
246-
newQuestionEmailWorker: newUnstartedNewQuestionEmailWorkerForTest(1),
246+
newQuestionEmailWorker: newUnstartedNewQuestionEmailWorkerForTest(),
247247
}
248248

249249
err = service.handleNewQuestionNotification(context.Background(), &schema.ExternalNotificationMsg{
@@ -271,7 +271,7 @@ func TestHandleNewQuestionNotificationReturnsWhenEmailWorkerQueueFull(t *testing
271271
}
272272
t.Cleanup(cleanup)
273273

274-
worker := newUnstartedNewQuestionEmailWorkerForTest(1)
274+
worker := newUnstartedNewQuestionEmailWorkerForTest()
275275
if !worker.TryEnqueue(newQuestionEmailWorkerTask("already-queued", "queued-user")) {
276276
t.Fatalf("pre-fill TryEnqueue() = false, want true")
277277
}
@@ -332,7 +332,7 @@ func TestHandleNewQuestionNotificationSyncsPluginBeforeEmailEnqueue(t *testing.T
332332
releaseNotify := make(chan struct{})
333333
enableNewQuestionNotificationTestPlugin(t, notifyStarted, releaseNotify)
334334

335-
worker := newUnstartedNewQuestionEmailWorkerForTest(1)
335+
worker := newUnstartedNewQuestionEmailWorkerForTest()
336336
service := &ExternalNotificationService{
337337
data: &basedata.Data{Cache: cache},
338338
userNotificationConfigRepo: &newQuestionNotificationTestUserNotificationConfigRepo{
@@ -428,27 +428,13 @@ func setNewQuestionNotificationEmailSendIntervalEnv(t *testing.T, value string,
428428
})
429429
}
430430

431-
func newQuestionSubscriber(userID string, channels ...*schema.NotificationChannelConfig) *NewQuestionSubscriber {
432-
return &NewQuestionSubscriber{
433-
UserID: userID,
434-
Channels: channels,
435-
}
436-
}
437-
438431
func newQuestionEmailChannel(enable bool) *schema.NotificationChannelConfig {
439432
return &schema.NotificationChannelConfig{
440433
Key: constant.EmailChannel,
441434
Enable: enable,
442435
}
443436
}
444437

445-
func newQuestionNonEmailChannel(enable bool) *schema.NotificationChannelConfig {
446-
return &schema.NotificationChannelConfig{
447-
Key: constant.NotificationChannelKey("inbox"),
448-
Enable: enable,
449-
}
450-
}
451-
452438
func newQuestionNotificationConfig(
453439
userID string, source constant.NotificationSource, emailEnabled bool) *entity.UserNotificationConfig {
454440
channels := schema.NotificationChannels{
@@ -702,14 +688,6 @@ func (r *newQuestionNotificationTestEmailRepo) VerifyCode(context.Context, strin
702688
return "", nil
703689
}
704690

705-
func (r *newQuestionNotificationTestEmailRepo) userIDs() []string {
706-
userIDs := make([]string, 0, len(r.codesByUserID))
707-
for userID := range r.codesByUserID {
708-
userIDs = append(userIDs, userID)
709-
}
710-
return userIDs
711-
}
712-
713691
var (
714692
newQuestionNotificationTestPluginOnce sync.Once
715693
newQuestionNotificationTestPluginInst = &newQuestionNotificationTestPlugin{}
@@ -820,3 +798,18 @@ func (newQuestionNotificationTestUserExternalLoginRepo) GetCacheUserExternalLogi
820798
context.Context, string) (*schema.ExternalLoginUserInfoCache, error) {
821799
return nil, nil
822800
}
801+
802+
func (newQuestionNotificationTestUserExternalLoginRepo) SetCacheOAuthState(
803+
context.Context, string, *schema.ExternalLoginOAuthState, time.Duration) error {
804+
return nil
805+
}
806+
807+
func (newQuestionNotificationTestUserExternalLoginRepo) GetCacheOAuthState(
808+
context.Context, string) (*schema.ExternalLoginOAuthState, error) {
809+
return nil, nil
810+
}
811+
812+
func (newQuestionNotificationTestUserExternalLoginRepo) DeleteCacheOAuthState(
813+
context.Context, string) error {
814+
return nil
815+
}

0 commit comments

Comments
 (0)