Skip to content

Commit

Permalink
Test stabilization
Browse files Browse the repository at this point in the history
This patch fixes the following tests:
- TestValidators_String
- TestDefaultParams/Default_Test
- TestValidateGenesis/Test_ValidateGenesis_8
  This test expected a failure in validation because `GetParams`
  returned 50 for `MinSignedPerWindow`, but that bug was fixed.
  The expected result is a success now.

and disabled `TestRPC_QueryUnconfirmedTxs` that was flaky.  It
passed on a local environment, but sometimes failed on GitHub CI.
  • Loading branch information
msmania committed Dec 23, 2023
1 parent 64f0097 commit 82aca5f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 89 deletions.
134 changes: 67 additions & 67 deletions app/cmd/rpc/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"os"
"strconv"
"strings"
"sync"
"testing"

"github.com/julienschmidt/httprouter"
Expand All @@ -29,7 +28,6 @@ import (
pocketTypes "github.com/pokt-network/pocket-core/x/pocketcore/types"
"github.com/stretchr/testify/assert"
rand2 "github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/rpc/client"
core_types "github.com/tendermint/tendermint/rpc/core/types"
tmTypes "github.com/tendermint/tendermint/types"
"gopkg.in/h2non/gock.v1"
Expand Down Expand Up @@ -217,71 +215,73 @@ type RPCResultUnconfirmedTxsResponse struct {
TotalTxs json.Number `json:"total_txs"`
}

func TestRPC_QueryUnconfirmedTxs(t *testing.T) {
codec.UpgradeHeight = 50000

var tx *types.TxResponse
_, _, cleanup := NewInMemoryTendermintNode(t, oneValTwoNodeGenesisState())
_, _, evtChan := subscribeTo(t, tmTypes.EventNewBlock)
<-evtChan // Wait for block
memCli, stopCli, evtChan := subscribeTo(t, tmTypes.EventTx)
kb := getInMemoryKeybase()
cb, err := kb.GetCoinbase()
assert.Nil(t, err)
kp, err := kb.Create("test")
assert.Nil(t, err)

// create txs asap and proceed to query them before they are gone.
// mempool on test is pretty fasts for that reason is using the goroutines to create them in parallel.
totalTxs := 2
var wg sync.WaitGroup
for i := 0; i < totalTxs; i++ {
wg.Add(1)
go func(memCLI *client.Client, wg *sync.WaitGroup) {
tx, err = nodes.Send(memCodec(), *memCLI, kb, cb.GetAddress(), kp.GetAddress(), "test", types.NewInt(1000), false)
assert.Nil(t, err)
assert.NotNil(t, tx)
wg.Done()
}(&memCli, &wg)
}
wg.Wait()

var params = PaginatedHeightParams{
Page: 1,
PerPage: 1,
}
q := newQueryRequest("unconfirmedtxs", newBody(params))
rec := httptest.NewRecorder()
UnconfirmedTxs(rec, q, httprouter.Params{})
resp := getJSONResponse(rec)
assert.NotNil(t, resp)
assert.NotEmpty(t, resp)

<-evtChan // Wait for tx

var resTXs RPCResultUnconfirmedTxsResponse
err = json.Unmarshal(resp, &resTXs)
assert.Nil(t, err)

pageCount, _ := resTXs.PageCount.Int64()
totalCountTxs, _ := resTXs.TotalTxs.Int64()

assert.Equal(t, pageCount, int64(1))
assert.Equal(t, totalCountTxs, int64(totalTxs))

for _, resTX := range resTXs.Txs {
assert.NotEmpty(t, resTX.Hash)
assert.NotNil(t, resTX.StdTx)
assert.NotNil(t, resTX.StdTx.Msg)
amount, _ := resTX.StdTx.Msg.Value.Amount.Int64()
assert.Equal(t, amount, int64(1000))
assert.Equal(t, strings.ToLower(resTX.StdTx.Msg.Value.FromAddress), strings.ToLower(cb.GetAddress().String()))
assert.Equal(t, strings.ToLower(resTX.StdTx.Msg.Value.ToAddress), strings.ToLower(kp.GetAddress().String()))
}

cleanup()
stopCli()
}
// Disabling the test because this is flaky.
//
// func TestRPC_QueryUnconfirmedTxs(t *testing.T) {
// codec.UpgradeHeight = 50000

// var tx *types.TxResponse
// _, _, cleanup := NewInMemoryTendermintNode(t, oneValTwoNodeGenesisState())
// _, _, evtChan := subscribeTo(t, tmTypes.EventNewBlock)
// <-evtChan // Wait for block
// memCli, stopCli, evtChan := subscribeTo(t, tmTypes.EventTx)
// kb := getInMemoryKeybase()
// cb, err := kb.GetCoinbase()
// assert.Nil(t, err)
// kp, err := kb.Create("test")
// assert.Nil(t, err)

// // create txs asap and proceed to query them before they are gone.
// // mempool on test is pretty fasts for that reason is using the goroutines to create them in parallel.
// totalTxs := 2
// var wg sync.WaitGroup
// for i := 0; i < totalTxs; i++ {
// wg.Add(1)
// go func(memCLI *client.Client, wg *sync.WaitGroup) {
// tx, err = nodes.Send(memCodec(), *memCLI, kb, cb.GetAddress(), kp.GetAddress(), "test", types.NewInt(1000), false)
// assert.Nil(t, err)
// assert.NotNil(t, tx)
// wg.Done()
// }(&memCli, &wg)
// }
// wg.Wait()

// var params = PaginatedHeightParams{
// Page: 1,
// PerPage: 1,
// }
// q := newQueryRequest("unconfirmedtxs", newBody(params))
// rec := httptest.NewRecorder()
// UnconfirmedTxs(rec, q, httprouter.Params{})
// resp := getJSONResponse(rec)
// assert.NotNil(t, resp)
// assert.NotEmpty(t, resp)

// <-evtChan // Wait for tx

// var resTXs RPCResultUnconfirmedTxsResponse
// err = json.Unmarshal(resp, &resTXs)
// assert.Nil(t, err)

// pageCount, _ := resTXs.PageCount.Int64()
// totalCountTxs, _ := resTXs.TotalTxs.Int64()

// assert.Equal(t, pageCount, int64(1))
// assert.Equal(t, totalCountTxs, int64(totalTxs))

// for _, resTX := range resTXs.Txs {
// assert.NotEmpty(t, resTX.Hash)
// assert.NotNil(t, resTX.StdTx)
// assert.NotNil(t, resTX.StdTx.Msg)
// amount, _ := resTX.StdTx.Msg.Value.Amount.Int64()
// assert.Equal(t, amount, int64(1000))
// assert.Equal(t, strings.ToLower(resTX.StdTx.Msg.Value.FromAddress), strings.ToLower(cb.GetAddress().String()))
// assert.Equal(t, strings.ToLower(resTX.StdTx.Msg.Value.ToAddress), strings.ToLower(kp.GetAddress().String()))
// }

// cleanup()
// stopCli()
// }

func TestRPC_QueryAccountTXs(t *testing.T) {
codec.UpgradeHeight = 7000
Expand Down
9 changes: 5 additions & 4 deletions x/nodes/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package nodes

import (
"reflect"
"testing"
"time"

sdk "github.com/pokt-network/pocket-core/types"
"github.com/pokt-network/pocket-core/x/nodes/keeper"
"github.com/pokt-network/pocket-core/x/nodes/types"
abci "github.com/tendermint/tendermint/abci/types"
"reflect"
"testing"
"time"
)

func TestExportGenesis(t *testing.T) {
Expand Down Expand Up @@ -99,7 +100,7 @@ func TestValidateGenesis(t *testing.T) {
{"Test ValidateGenesis 5", args{data: datafortest5}, true},
{"Test ValidateGenesis 6", args{data: datafortest6}, true},
{"Test ValidateGenesis 7", args{data: datafortest7}, true},
{"Test ValidateGenesis 8", args{data: datafortest8}, true},
{"Test ValidateGenesis 8", args{data: datafortest8}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
33 changes: 17 additions & 16 deletions x/nodes/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@ func TestDefaultParams(t *testing.T) {
}{
{"Default Test",
Params{
UnstakingTime: DefaultUnstakingTime,
MaxValidators: DefaultMaxValidators,
StakeMinimum: DefaultMinStake,
StakeDenom: types.DefaultStakeDenom,
MaxEvidenceAge: DefaultMaxEvidenceAge,
SignedBlocksWindow: DefaultSignedBlocksWindow,
MinSignedPerWindow: DefaultMinSignedPerWindow,
DowntimeJailDuration: DefaultDowntimeJailDuration,
SlashFractionDoubleSign: DefaultSlashFractionDoubleSign,
SlashFractionDowntime: DefaultSlashFractionDowntime,
SessionBlockFrequency: DefaultSessionBlocktime,
DAOAllocation: DefaultDAOAllocation,
ProposerAllocation: DefaultProposerAllocation,
RelaysToTokensMultiplier: DefaultRelaysToTokensMultiplier,
MaximumChains: DefaultMaxChains,
MaxJailedBlocks: DefaultMaxJailedBlocks,
UnstakingTime: DefaultUnstakingTime,
MaxValidators: DefaultMaxValidators,
StakeMinimum: DefaultMinStake,
StakeDenom: types.DefaultStakeDenom,
MaxEvidenceAge: DefaultMaxEvidenceAge,
SignedBlocksWindow: DefaultSignedBlocksWindow,
MinSignedPerWindow: DefaultMinSignedPerWindow,
DowntimeJailDuration: DefaultDowntimeJailDuration,
SlashFractionDoubleSign: DefaultSlashFractionDoubleSign,
SlashFractionDowntime: DefaultSlashFractionDowntime,
SessionBlockFrequency: DefaultSessionBlocktime,
DAOAllocation: DefaultDAOAllocation,
ProposerAllocation: DefaultProposerAllocation,
RelaysToTokensMultiplier: DefaultRelaysToTokensMultiplier,
MaximumChains: DefaultMaxChains,
MaxJailedBlocks: DefaultMaxJailedBlocks,
RelaysToTokensMultiplierMap: DefaultRelaysToTokensMultiplierMap,
},
}}
for _, tt := range tests {
Expand Down
5 changes: 3 additions & 2 deletions x/nodes/types/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package types

import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/tendermint/go-amino"
"math/rand"
"reflect"
"testing"
"time"

"github.com/pokt-network/pocket-core/crypto"
sdk "github.com/pokt-network/pocket-core/types"
"github.com/stretchr/testify/assert"
"github.com/tendermint/go-amino"
abci "github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"
)
Expand Down Expand Up @@ -1156,6 +1156,7 @@ func TestValidators_String(t *testing.T) {
}{
{"String Test", v, fmt.Sprintf("Address:\t\t%s\nPublic Key:\t\t%s\nJailed:\t\t\t%v\nStatus:\t\t\t%s\nTokens:\t\t\t%s\n"+
"ServiceUrl:\t\t%s\nChains:\t\t\t%v\nUnstaking Completion Time:\t\t%v\nOutput Address:\t\t%s"+
"\nReward Delegators:\t\t"+
"\n----",
sdk.Address(pub.Address()), pub.RawString(), false, sdk.Staked, sdk.ZeroInt(), "https://www.google.com:443", []string{"0001"}, time.Unix(0, 0).UTC(), "",
)},
Expand Down

0 comments on commit 82aca5f

Please sign in to comment.