Skip to content

Commit

Permalink
cancel context in tests to stop the goroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya1702 committed Nov 18, 2024
1 parent 9f27e10 commit b71df7b
Showing 1 changed file with 14 additions and 31 deletions.
45 changes: 14 additions & 31 deletions internal/services/ingest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ func TestIngest_LatestSyncedLedgerBehindRPC(t *testing.T) {
dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN)
require.NoError(t, err)

ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer func() {
ctx.Done()
cancel()
_ = dbConnectionPool.Close()
dbt.Close()
}()
Expand Down Expand Up @@ -309,9 +309,9 @@ func TestIngest_LatestSyncedLedgerAheadOfRPC(t *testing.T) {
dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN)
require.NoError(t, err)

ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer func() {
ctx.Done()
cancel()
_ = dbConnectionPool.Close()
dbt.Close()
log.DefaultLogger.SetOutput(os.Stderr)
Expand Down Expand Up @@ -347,7 +347,6 @@ func TestIngest_LatestSyncedLedgerAheadOfRPC(t *testing.T) {
log.DefaultLogger.SetOutput(&logBuffer)
log.SetLevel(log.DebugLevel)

// Mock transaction response for when RPC catches up
txEnvXDR := "AAAAAGL8HQvQkbK2HA3WVjRrKmjX00fG8sLI7m0ERwJW/AX3AAAACgAAAAAAAAABAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAArqN6LeOagjxMaUP96Bzfs9e0corNZXzBWJkFoK7kvkwAAAAAO5rKAAAAAAAAAAABVvwF9wAAAEAKZ7IPj/46PuWU6ZOtyMosctNAkXRNX9WCAI5RnfRk+AyxDLoDZP/9l3NvsxQtWj9juQOuoBlFLnWu8intgxQA"
mockResult := entities.RPCGetTransactionsResult{
Transactions: []entities.Transaction{{
Expand Down Expand Up @@ -397,7 +396,7 @@ func TestTrackRPCServiceHealth_HealthyService(t *testing.T) {
mockAppTracker := &apptracker.MockAppTracker{}
heartbeat := make(chan entities.RPCGetHealthResult, 1)

ctx, cancel := context.WithTimeout(context.Background(), 70*time.Second)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

healthResult := entities.RPCGetHealthResult{
Expand All @@ -406,24 +405,13 @@ func TestTrackRPCServiceHealth_HealthyService(t *testing.T) {
OldestLedger: 1,
LedgerRetentionWindow: 0,
}
mockRPCService.On("GetHealth").Return(healthResult, nil)

go func() {
select {
case <-ctx.Done():
return
default:
trackRPCServiceHealth(context.Background(), heartbeat, nil, mockRPCService)
}
}()
mockRPCService.On("GetHealth").Return(healthResult, nil).Once().Run(func(args mock.Arguments) {
cancel()
})

select {
case result := <-heartbeat:
assert.Equal(t, healthResult, result)
case <-ctx.Done():
t.Fatal("timeout waiting for heartbeat")
}
trackRPCServiceHealth(ctx, heartbeat, mockAppTracker, mockRPCService)

assert.Equal(t, healthResult, <-heartbeat)
mockRPCService.AssertExpectations(t)
mockAppTracker.AssertNotCalled(t, "CaptureMessage")
}
Expand All @@ -439,17 +427,12 @@ func TestTrackRPCServiceHealth_UnhealthyService(t *testing.T) {
)

mockAppTracker := &apptracker.MockAppTracker{}
mockAppTracker.On("CaptureMessage", "rpc service unhealthy for over 1m0s")
mockAppTracker.On("CaptureMessage", "rpc service unhealthy for over 1m0s").Run(func(args mock.Arguments) {
cancel()
})
heartbeat := make(chan entities.RPCGetHealthResult, 1)

go func() {
select {
case <-ctx.Done():
return
default:
trackRPCServiceHealth(context.Background(), heartbeat, mockAppTracker, mockRPCService)
}
}()
go trackRPCServiceHealth(ctx, heartbeat, mockAppTracker, mockRPCService)

// Wait long enough for both warnings to trigger
time.Sleep(65 * time.Second)
Expand Down

0 comments on commit b71df7b

Please sign in to comment.