Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Dec 29, 2023
1 parent ff3b08d commit 7da5da0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
6 changes: 3 additions & 3 deletions x/dex/contract/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func EndBlockerAtomic(ctx sdk.Context, keeper *keeper.Keeper, validContractsInfo
handleDeposits(spanCtx, cachedCtx, env, keeper, tracer)

runner := NewParallelRunner(func(contract types.ContractInfoV2) {
orderMatchingRunnable(spanCtx, cachedCtx, env, keeper, contract, tracer)
OrderMatchingRunnable(spanCtx, cachedCtx, env, keeper, contract, tracer)

Check warning on line 61 in x/dex/contract/abci.go

View check run for this annotation

Codecov / codecov/patch

x/dex/contract/abci.go#L61

Added line #L61 was not covered by tests
}, validContractsInfo, cachedCtx)

_, err := logging.LogIfNotDoneAfter(ctx.Logger(), func() (struct{}, error) {
Expand Down Expand Up @@ -227,7 +227,7 @@ func handleUnfulfilledMarketOrders(ctx context.Context, sdkCtx sdk.Context, env
}
}

func orderMatchingRunnable(ctx context.Context, sdkContext sdk.Context, env *environment, keeper *keeper.Keeper, contractInfo types.ContractInfoV2, tracer *otrace.Tracer) {
func OrderMatchingRunnable(ctx context.Context, sdkContext sdk.Context, env *environment, keeper *keeper.Keeper, contractInfo types.ContractInfoV2, tracer *otrace.Tracer) {
defer func() {
if err := recover(); err != nil {
msg := fmt.Sprintf("PANIC RECOVERED during order matching: %s", err)
Expand All @@ -237,7 +237,7 @@ func orderMatchingRunnable(ctx context.Context, sdkContext sdk.Context, env *env
}

Check warning on line 237 in x/dex/contract/abci.go

View check run for this annotation

Codecov / codecov/patch

x/dex/contract/abci.go#L236-L237

Added lines #L236 - L237 were not covered by tests
}
}()
_, span := (*tracer).Start(ctx, "orderMatchingRunnable")
_, span := (*tracer).Start(ctx, "OrderMatchingRunnable")
defer span.End()
defer telemetry.MeasureSince(time.Now(), "dex", "order_matching_runnable")
defer func() {
Expand Down
9 changes: 9 additions & 0 deletions x/dex/contract/abci_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package contract_test

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -32,3 +33,11 @@ func TestTransferRentFromDexToCollector(t *testing.T) {
collectorBalance := bankkeeper.GetBalance(ctx, testApp.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName), "usei")
require.Equal(t, int64(80), collectorBalance.Amount.Int64())
}

func TestOrderMatchingRunnablePanicHandler(t *testing.T) {
testApp := keepertest.TestApp()
ctx := testApp.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()})
require.NotPanics(t, func() {
contract.OrderMatchingRunnable(context.Background(), ctx, nil, nil, types.ContractInfoV2{}, nil)
})
}
16 changes: 8 additions & 8 deletions x/dex/contract/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ func (r *ParallelRunner) wrapRunnable(contractAddr types.ContractAddress) {
if err := recover(); err != nil {
r.sdkCtx.Logger().Error(fmt.Sprintf("panic in parallel runner recovered: %s", err))
}

atomic.AddInt64(&r.inProgressCnt, -1) // this has to happen after any potential increment to readyCnt
select {
case r.someContractFinished <- struct{}{}:
case <-r.done:
// make sure other goroutines can also receive from 'done'
r.done <- struct{}{}
}
}()

contractInfo, _ := r.contractAddrToInfo.Load(contractAddr)
Expand Down Expand Up @@ -165,12 +173,4 @@ func (r *ParallelRunner) wrapRunnable(contractAddr types.ContractAddress) {
}
}
}

atomic.AddInt64(&r.inProgressCnt, -1) // this has to happen after any potential increment to readyCnt
select {
case r.someContractFinished <- struct{}{}:
case <-r.done:
// make sure other goroutines can also receive from 'done'
r.done <- struct{}{}
}
}
13 changes: 13 additions & 0 deletions x/dex/contract/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func idleRunnable(_ types.ContractInfoV2) {
atomic.AddInt64(&counter, 1)
}

func panicRunnable(_ types.ContractInfoV2) {
panic("")
}

func dependencyCheckRunnable(contractInfo types.ContractInfoV2) {
if contractInfo.ContractAddr == "C" {
_, hasA := dependencyCheck.Load("A")
Expand Down Expand Up @@ -126,3 +130,12 @@ func TestRunnerParallelContractWithInvalidDependency(t *testing.T) {
_, hasC := dependencyCheck.Load("C")
require.False(t, hasC)
}

func TestRunnerPanicContract(t *testing.T) {
contractInfo := types.ContractInfoV2{
ContractAddr: "A",
NumIncomingDependencies: 0,
}
runner := contract.NewParallelRunner(panicRunnable, []types.ContractInfoV2{contractInfo}, sdkCtx)
require.NotPanics(t, runner.Run)
}

0 comments on commit 7da5da0

Please sign in to comment.