Skip to content

Commit

Permalink
Add test for case where channel accounts are not created and ctx is c…
Browse files Browse the repository at this point in the history
…ancelled
  • Loading branch information
aditya1702 committed Jan 15, 2025
1 parent 738c177 commit 2d54060
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
3 changes: 2 additions & 1 deletion internal/services/channel_account_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ func (s *channelAccountService) EnsureChannelAccounts(ctx context.Context, numbe
}

func (s *channelAccountService) submitCreateChannelAccountsOnChainTransaction(ctx context.Context, distributionAccountPublicKey string, ops []txnbuild.Operation) error {
rpcHeartbeatChannel := s.RPCService.GetHeartbeatChannel()
select {
case <-ctx.Done():
return fmt.Errorf("context cancelled while waiting for rpc service to become healthy: %w", ctx.Err())
// The channel account creation goroutine will wait in the background for the rpc service to become healthy on startup.
// This lets the API server startup so that users can start interacting with the API which does not depend on RPC, instead of waiting till it becomes healthy.
case <-s.RPCService.GetHeartbeatChannel():
case <-rpcHeartbeatChannel:
accountSeq, err := s.RPCService.GetAccountLedgerSequence(distributionAccountPublicKey)
if err != nil {
return fmt.Errorf("getting ledger sequence for distribution account public key: %s: %w", distributionAccountPublicKey, err)
Expand Down
34 changes: 28 additions & 6 deletions internal/services/channel_account_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"context"
"testing"
"time"

"github.com/stellar/go/keypair"
"github.com/stellar/go/network"
Expand Down Expand Up @@ -232,14 +233,9 @@ func TestChannelAccountServiceEnsureChannelAccounts(t *testing.T) {
Return(network.TestNetworkPassphrase).
Once()
defer signatureClient.AssertExpectations(t)

mockRPCService.
On("GetHealth").
Return(entities.RPCGetHealthResult{Status: "healthy"}, nil)

// Create and set up the heartbeat channel
health, _ := mockRPCService.GetHealth()
heartbeatChan <- health
heartbeatChan <- entities.RPCGetHealthResult{Status: "healthy"}
mockRPCService.On("GetHeartbeatChannel").Return(heartbeatChan)

mockRPCService.
Expand All @@ -265,6 +261,32 @@ func TestChannelAccountServiceEnsureChannelAccounts(t *testing.T) {
require.Error(t, err)
assert.Contains(t, err.Error(), "transaction failed")
})

t.Run("fails if rpc service is not healthy", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

channelAccountStore.
On("Count", ctx).
Return(2, nil).
Once()
defer channelAccountStore.AssertExpectations(t)

distributionAccount := keypair.MustRandom()
signatureClient.
On("GetAccountPublicKey", ctx).
Return(distributionAccount.Address(), nil).
Once()
defer signatureClient.AssertExpectations(t)

heartbeatChan := make(chan entities.RPCGetHealthResult, 1)
mockRPCService.On("GetHeartbeatChannel").Return(heartbeatChan)
defer mockRPCService.AssertExpectations(t)

err := s.EnsureChannelAccounts(ctx, 5)
require.Error(t, err)
assert.Contains(t, err.Error(), "context cancelled while waiting for rpc service to become healthy")
})
}

func TestSubmitTransaction(t *testing.T) {
Expand Down

0 comments on commit 2d54060

Please sign in to comment.