Skip to content

Commit

Permalink
fix_: simplify performStorenodeTask
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Oct 23, 2024
1 parent 777aefb commit c17f9ef
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
40 changes: 15 additions & 25 deletions protocol/messenger_mailserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,35 +90,25 @@ func (m *Messenger) scheduleSyncChat(chat *Chat) (bool, error) {
}

func (m *Messenger) performStorenodeTask(task func() (*MessengerResponse, error), opts ...history.StorenodeTaskOption) (*MessengerResponse, error) {
responseCh := make(chan *MessengerResponse)
errCh := make(chan error)

go func() {
defer gocommon.LogOnPanic()
err := m.transport.PerformStorenodeTask(func() error {
r, err := task()
if err != nil {
return err
}

select {
case <-m.ctx.Done():
return m.ctx.Err()
case responseCh <- r:
default:
//
}
responseCh := make(chan *MessengerResponse, 1)
err := m.transport.PerformStorenodeTask(func() error {
r, err := task()
if err != nil {
return err

Check warning on line 97 in protocol/messenger_mailserver.go

View check run for this annotation

Codecov / codecov/patch

protocol/messenger_mailserver.go#L92-L97

Added lines #L92 - L97 were not covered by tests
}

select {
case <-m.ctx.Done():
return m.ctx.Err()
case responseCh <- r:
return nil

Check warning on line 104 in protocol/messenger_mailserver.go

View check run for this annotation

Codecov / codecov/patch

protocol/messenger_mailserver.go#L100-L104

Added lines #L100 - L104 were not covered by tests
}, opts...)
if err != nil {
errCh <- err
}
}()
}, opts...)
if err != nil {
return nil, err

Check warning on line 108 in protocol/messenger_mailserver.go

View check run for this annotation

Codecov / codecov/patch

protocol/messenger_mailserver.go#L107-L108

Added lines #L107 - L108 were not covered by tests
}

select {
case err := <-errCh:
return nil, err
case r := <-responseCh:
if r != nil {
return r, nil

Check warning on line 114 in protocol/messenger_mailserver.go

View check run for this annotation

Codecov / codecov/patch

protocol/messenger_mailserver.go#L111-L114

Added lines #L111 - L114 were not covered by tests
Expand Down Expand Up @@ -784,7 +774,7 @@ func (m *Messenger) SyncChatFromSyncedFrom(chatID string) (uint32, error) {
chat.SyncedFrom = uint32(batch.From.Unix())

Check warning on line 774 in protocol/messenger_mailserver.go

View check run for this annotation

Codecov / codecov/patch

protocol/messenger_mailserver.go#L773-L774

Added lines #L773 - L774 were not covered by tests
}

m.logger.Debug("setting sync timestamps", zap.Int64("from", int64(batch.From.Unix())), zap.Int64("to", int64(chat.SyncedTo)), zap.String("chatID", chatID))
m.logger.Debug("setting sync timestamps", zap.Int64("from", batch.From.Unix()), zap.Int64("to", int64(chat.SyncedTo)), zap.String("chatID", chatID))

Check warning on line 777 in protocol/messenger_mailserver.go

View check run for this annotation

Codecov / codecov/patch

protocol/messenger_mailserver.go#L777

Added line #L777 was not covered by tests

err = m.persistence.SetSyncTimestamps(uint32(batch.From.Unix()), chat.SyncedTo, chat.ID)
from = uint32(batch.From.Unix())

Check warning on line 780 in protocol/messenger_mailserver.go

View check run for this annotation

Codecov / codecov/patch

protocol/messenger_mailserver.go#L779-L780

Added lines #L779 - L780 were not covered by tests
Expand Down
4 changes: 2 additions & 2 deletions protocol/messenger_storenode_comunity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (s *MessengerStoreNodeCommunitySuite) newMessenger(name string, storenodeAd
}

func (s *MessengerStoreNodeCommunitySuite) createCommunityWithChat(m *Messenger) (*communities.Community, *Chat) {
WaitForAvailableStoreNode(&s.Suite, m, 500*time.Millisecond)
WaitForAvailableStoreNode(&s.Suite, m, context.TODO())

storeNodeSubscription := s.setupStoreNodeEnvelopesWatcher(nil)

Expand Down Expand Up @@ -197,7 +197,7 @@ func (s *MessengerStoreNodeCommunitySuite) fetchCommunity(m *Messenger, communit
WithWaitForResponseOption(true),
}

fetchedCommunity, stats, err := m.storeNodeRequestsManager.FetchCommunity(communityShard, options)
fetchedCommunity, stats, err := m.storeNodeRequestsManager.FetchCommunity(context.TODO(), communityShard, options)

s.Require().NoError(err)
s.requireCommunitiesEqual(fetchedCommunity, expectedCommunity)
Expand Down
6 changes: 3 additions & 3 deletions protocol/messenger_storenode_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (s *MessengerStoreNodeRequestSuite) fetchCommunity(m *Messenger, communityS
WithWaitForResponseOption(true),
}

fetchedCommunity, stats, err := m.storeNodeRequestsManager.FetchCommunity(communityShard, options)
fetchedCommunity, stats, err := m.storeNodeRequestsManager.FetchCommunity(context.TODO(), communityShard, options)

s.Require().NoError(err)
s.requireCommunitiesEqual(fetchedCommunity, expectedCommunity)
Expand Down Expand Up @@ -808,7 +808,7 @@ func (s *MessengerStoreNodeRequestSuite) TestRequestCommunityEnvelopesOrder() {
}

// Fetch the community
fetchedCommunity, _, err := s.bob.storeNodeRequestsManager.FetchCommunity(community.CommunityShard(), options)
fetchedCommunity, _, err := s.bob.storeNodeRequestsManager.FetchCommunity(context.TODO(), community.CommunityShard(), options)
s.Require().NoError(err)
s.requireCommunitiesEqual(fetchedCommunity, community)

Expand Down Expand Up @@ -1162,7 +1162,7 @@ func (s *MessengerStoreNodeRequestSuite) TestFetchRealCommunity() {
}
storeNodeRequestOptions = append(storeNodeRequestOptions, exampleToRun.CustomOptions...)

fetchedCommunity, stats, err := user.storeNodeRequestsManager.FetchCommunity(communityAddress, storeNodeRequestOptions)
fetchedCommunity, stats, err := user.storeNodeRequestsManager.FetchCommunity(context.TODO(), communityAddress, storeNodeRequestOptions)

result.EnvelopesCount = stats.FetchedEnvelopesCount
result.FetchedCommunity = fetchedCommunity
Expand Down
2 changes: 2 additions & 0 deletions protocol/messenger_testing_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ func SetIdentityImagesAndWaitForChange(s *suite.Suite, messenger *Messenger, tim
}

func WaitForAvailableStoreNode(s *suite.Suite, m *Messenger, ctx context.Context) {
ctx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
defer cancel()
available := m.transport.WaitForAvailableStoreNode(ctx)

Check warning on line 370 in protocol/messenger_testing_utils.go

View check run for this annotation

Codecov / codecov/patch

protocol/messenger_testing_utils.go#L367-L370

Added lines #L367 - L370 were not covered by tests
s.Require().True(available)
}
Expand Down

0 comments on commit c17f9ef

Please sign in to comment.