Skip to content

Commit 457de70

Browse files
idilhaqMuhammad Idil Haq Amir
andauthored
feat(notifier): sending notification concurrently (#136)
* feat: sending notification concurrently * test: update unit test * test: fix unit test * test: fix race data * chore: update ctx * test: update test * test: adding sleep for temporary solution * test: update test sleep --------- Co-authored-by: Muhammad Idil Haq Amir <[email protected]>
1 parent e54f816 commit 457de70

File tree

4 files changed

+167
-112
lines changed

4 files changed

+167
-112
lines changed

core/appeal/service.go

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -413,11 +413,14 @@ func (s *Service) Create(ctx context.Context, appeals []*domain.Appeal, opts ...
413413
}
414414

415415
if len(notifications) > 0 {
416-
if errs := s.notifier.Notify(ctx, notifications); errs != nil {
417-
for _, err1 := range errs {
418-
s.logger.Error(ctx, "failed to send notifications", "error", err1.Error())
416+
go func() {
417+
ctx := context.WithoutCancel(ctx)
418+
if errs := s.notifier.Notify(ctx, notifications); errs != nil {
419+
for _, err1 := range errs {
420+
s.logger.Error(ctx, "failed to send notifications", "error", err1.Error())
421+
}
419422
}
420-
}
423+
}()
421424
}
422425

423426
return nil
@@ -627,11 +630,14 @@ func (s *Service) UpdateApproval(ctx context.Context, approvalAction domain.Appr
627630
notifications = append(notifications, s.getApprovalNotifications(ctx, appeal)...)
628631
}
629632
if len(notifications) > 0 {
630-
if errs := s.notifier.Notify(ctx, notifications); errs != nil {
631-
for _, err1 := range errs {
632-
s.logger.Error(ctx, "failed to send notifications", "error", err1.Error())
633+
go func() {
634+
ctx := context.WithoutCancel(ctx)
635+
if errs := s.notifier.Notify(ctx, notifications); errs != nil {
636+
for _, err1 := range errs {
637+
s.logger.Error(ctx, "failed to send notifications", "error", err1.Error())
638+
}
633639
}
634-
}
640+
}()
635641
}
636642

637643
var auditKey string
@@ -744,37 +750,40 @@ func (s *Service) AddApprover(ctx context.Context, appealID, approvalID, email s
744750
}
745751
}
746752

747-
if errs := s.notifier.Notify(ctx, []domain.Notification{
748-
{
749-
User: email,
750-
Labels: map[string]string{
751-
"appeal_id": appeal.ID,
752-
},
753-
Message: domain.NotificationMessage{
754-
Type: domain.NotificationTypeApproverNotification,
755-
Variables: map[string]interface{}{
756-
"resource_name": fmt.Sprintf("%s (%s: %s)", appeal.Resource.Name, appeal.Resource.ProviderType, appeal.Resource.URN),
757-
"role": appeal.Role,
758-
"requestor": appeal.CreatedBy,
759-
"appeal_id": appeal.ID,
760-
"account_id": appeal.AccountID,
761-
"account_type": appeal.AccountType,
762-
"provider_type": appeal.Resource.ProviderType,
763-
"resource_type": appeal.Resource.Type,
764-
"created_at": appeal.CreatedAt,
765-
"approval_step": approval.Name,
766-
"actor": email,
767-
"details": appeal.Details,
768-
"duration": duration,
769-
"creator": appeal.Creator,
753+
go func() {
754+
ctx := context.WithoutCancel(ctx)
755+
if errs := s.notifier.Notify(ctx, []domain.Notification{
756+
{
757+
User: email,
758+
Labels: map[string]string{
759+
"appeal_id": appeal.ID,
760+
},
761+
Message: domain.NotificationMessage{
762+
Type: domain.NotificationTypeApproverNotification,
763+
Variables: map[string]interface{}{
764+
"resource_name": fmt.Sprintf("%s (%s: %s)", appeal.Resource.Name, appeal.Resource.ProviderType, appeal.Resource.URN),
765+
"role": appeal.Role,
766+
"requestor": appeal.CreatedBy,
767+
"appeal_id": appeal.ID,
768+
"account_id": appeal.AccountID,
769+
"account_type": appeal.AccountType,
770+
"provider_type": appeal.Resource.ProviderType,
771+
"resource_type": appeal.Resource.Type,
772+
"created_at": appeal.CreatedAt,
773+
"approval_step": approval.Name,
774+
"actor": email,
775+
"details": appeal.Details,
776+
"duration": duration,
777+
"creator": appeal.Creator,
778+
},
770779
},
771780
},
772-
},
773-
}); errs != nil {
774-
for _, err1 := range errs {
775-
s.logger.Error(ctx, "failed to send notifications", "error", err1.Error())
781+
}); errs != nil {
782+
for _, err1 := range errs {
783+
s.logger.Error(ctx, "failed to send notifications", "error", err1.Error())
784+
}
776785
}
777-
}
786+
}()
778787

779788
return appeal, nil
780789
}

core/appeal/service_test.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,6 @@ func (s *ServiceTestSuite) TestCreate() {
719719

720720
s.Run("should return appeals on success", func() {
721721
h := newServiceTestHelper()
722-
defer h.assertExpectations(s.T())
723722
resources := []*domain.Resource{
724723
{
725724
ID: "1",
@@ -1106,11 +1105,13 @@ func (s *ServiceTestSuite) TestCreate() {
11061105

11071106
s.Nil(actualError)
11081107
s.Equal(expectedResult, appeals)
1108+
1109+
time.Sleep(time.Millisecond)
1110+
h.assertExpectations(s.T())
11091111
})
11101112

11111113
s.Run("should return appeals on success with latest policy", func() {
11121114
h := newServiceTestHelper()
1113-
defer h.assertExpectations(s.T())
11141115
expDate := timeNow.Add(23 * time.Hour)
11151116

11161117
resources := []*domain.Resource{
@@ -1570,12 +1571,14 @@ func (s *ServiceTestSuite) TestCreate() {
15701571

15711572
s.Nil(actualError)
15721573
s.Equal(expectedResult, appeals)
1574+
1575+
time.Sleep(time.Millisecond)
1576+
h.assertExpectations(s.T())
15731577
})
15741578

15751579
s.Run("additional appeal creation", func() {
15761580
s.Run("should use the overridding policy", func() {
15771581
h := newServiceTestHelper()
1578-
defer h.assertExpectations(s.T())
15791582
input := &domain.Appeal{
15801583
ResourceID: uuid.New().String(),
15811584
AccountID: "[email protected]",
@@ -1668,13 +1671,15 @@ func (s *ServiceTestSuite) TestCreate() {
16681671
s.NoError(err)
16691672
s.Equal("test-approval", input.Approvals[0].Name)
16701673
s.Equal(expectedPermissions, input.Permissions)
1674+
1675+
time.Sleep(time.Millisecond)
1676+
h.assertExpectations(s.T())
16711677
})
16721678
})
16731679
}
16741680

16751681
func (s *ServiceTestSuite) TestCreateAppeal__WithExistingAppealAndWithAutoApprovalSteps() {
16761682
h := newServiceTestHelper()
1677-
defer h.assertExpectations(s.T())
16781683

16791684
appeal.TimeNow = func() time.Time {
16801685
return timeNow
@@ -1943,11 +1948,13 @@ func (s *ServiceTestSuite) TestCreateAppeal__WithExistingAppealAndWithAutoApprov
19431948

19441949
s.Nil(actualError)
19451950
s.Equal(expectedResult, appeals)
1951+
1952+
time.Sleep(time.Millisecond)
1953+
h.assertExpectations(s.T())
19461954
}
19471955

19481956
func (s *ServiceTestSuite) TestCreateAppeal__WithAdditionalAppeals() {
19491957
h := newServiceTestHelper()
1950-
defer h.assertExpectations(s.T())
19511958
providerType := "test-provider-type"
19521959
providerURN := "test-provider-urn"
19531960
resourceType := "test-resource-type"
@@ -2138,6 +2145,9 @@ func (s *ServiceTestSuite) TestCreateAppeal__WithAdditionalAppeals() {
21382145
err := h.service.Create(context.Background(), appealsPayload)
21392146

21402147
s.NoError(err)
2148+
2149+
time.Sleep(time.Millisecond)
2150+
h.assertExpectations(s.T())
21412151
}
21422152

21432153
func (s *ServiceTestSuite) TestCreate__WithAppealMetadata() {
@@ -2705,7 +2715,6 @@ func (s *ServiceTestSuite) TestUpdateApproval() {
27052715

27062716
s.Run("should terminate existing active grant if present", func() {
27072717
h := newServiceTestHelper()
2708-
defer h.assertExpectations(s.T())
27092718
action := domain.ApprovalAction{
27102719
AppealID: appealID,
27112720
ApprovalName: "test-approval-step",
@@ -2772,6 +2781,9 @@ func (s *ServiceTestSuite) TestUpdateApproval() {
27722781
_, actualError := h.service.UpdateApproval(context.Background(), action)
27732782

27742783
s.Nil(actualError)
2784+
2785+
time.Sleep(time.Millisecond)
2786+
h.assertExpectations(s.T())
27752787
})
27762788

27772789
s.Run("should return updated appeal on success", func() {
@@ -3128,7 +3140,6 @@ func (s *ServiceTestSuite) TestUpdateApproval() {
31283140
for _, tc := range testCases {
31293141
s.Run(tc.name, func() {
31303142
h := newServiceTestHelper()
3131-
defer h.assertExpectations(s.T())
31323143

31333144
h.mockRepository.EXPECT().
31343145
GetByID(h.ctxMatcher, validApprovalActionParam.AppealID).
@@ -3171,10 +3182,12 @@ func (s *ServiceTestSuite) TestUpdateApproval() {
31713182
Return(nil).Once()
31723183

31733184
actualResult, actualError := h.service.UpdateApproval(context.Background(), tc.expectedApprovalAction)
3174-
31753185
s.NoError(actualError)
31763186
tc.expectedResult.Policy = actualResult.Policy
31773187
s.Equal(tc.expectedResult, actualResult)
3188+
3189+
time.Sleep(time.Millisecond)
3190+
h.assertExpectations(s.T())
31783191
})
31793192
}
31803193
})
@@ -3305,8 +3318,6 @@ func (s *ServiceTestSuite) TestCancel() {
33053318

33063319
func (s *ServiceTestSuite) TestAddApprover() {
33073320
s.Run("should return appeal on success", func() {
3308-
h := newServiceTestHelper()
3309-
defer h.assertExpectations(s.T())
33103321
appealID := uuid.New().String()
33113322
approvalID := uuid.New().String()
33123323
approvalName := "test-approval-name"
@@ -3327,6 +3338,7 @@ func (s *ServiceTestSuite) TestAddApprover() {
33273338

33283339
for _, tc := range testCases {
33293340
s.Run(tc.name, func() {
3341+
h := newServiceTestHelper()
33303342
expectedAppeal := &domain.Appeal{
33313343
ID: appealID,
33323344
Status: domain.AppealStatusPending,
@@ -3369,10 +3381,10 @@ func (s *ServiceTestSuite) TestAddApprover() {
33693381
h.mockNotifier.EXPECT().
33703382
Notify(h.ctxMatcher, mock.Anything).
33713383
Run(func(ctx context.Context, notifications []domain.Notification) {
3372-
s.Len(notifications, 1)
3384+
assert.Equal(s.T(), len(notifications), 1)
33733385
n := notifications[0]
3374-
s.Equal(tc.newApprover, n.User)
3375-
s.Equal(domain.NotificationTypeApproverNotification, n.Message.Type)
3386+
assert.Equal(s.T(), tc.newApprover, n.User)
3387+
assert.Equal(s.T(), domain.NotificationTypeApproverNotification, n.Message.Type)
33763388
}).
33773389
Return(nil).Once()
33783390

@@ -3381,6 +3393,8 @@ func (s *ServiceTestSuite) TestAddApprover() {
33813393
s.NoError(actualError)
33823394
s.Equal(expectedApproval, actualAppeal.Approvals[0])
33833395

3396+
time.Sleep(time.Millisecond)
3397+
h.assertExpectations(s.T())
33843398
})
33853399
}
33863400
})

0 commit comments

Comments
 (0)