Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: test NodesManager Queue #68

Merged
merged 5 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions pkg/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ func TestConverterUnparsedMessage(t *testing.T) {
chain := &configTypes.Chain{Name: "chain"}
converter := converterPkg.NewConverter(logger, chain)

// msgSend := &cosmosBankTypes.MsgSend{}
// bytes, err := msgSend.Marshal()
// require.NoError(t, err)

message := &codecTypes.Any{
TypeUrl: "/cosmos.bank.v1beta1.MsgSend",
Value: []byte("unparsed"),
Expand Down
31 changes: 0 additions & 31 deletions pkg/nodes_manager/nodes_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,6 @@ import (
"github.com/rs/zerolog"
)

type ReportQueue struct {
Data []types.Report
Size int
Mutes sync.Mutex
}

func NewReportQueue(size int) ReportQueue {
return ReportQueue{Data: make([]types.Report, 0), Size: size}
}

func (q *ReportQueue) Add(report types.Report) {
q.Mutes.Lock()

if len(q.Data) >= q.Size {
_, q.Data = q.Data[0], q.Data[1:]
}

q.Data = append(q.Data, report)
q.Mutes.Unlock()
}

func (q *ReportQueue) Has(msg types.Report) bool {
for _, elem := range q.Data {
if elem.Reportable.GetHash() == msg.Reportable.GetHash() {
return true
}
}

return false
}

type NodesManager struct {
Logger zerolog.Logger
Nodes map[string][]*ws.TendermintWebsocketClient
Expand Down
37 changes: 37 additions & 0 deletions pkg/nodes_manager/queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package nodes_manager

import (
"main/pkg/types"
"sync"
)

type ReportQueue struct {
Data []types.Report
Size int
Mutes sync.Mutex
}

func NewReportQueue(size int) ReportQueue {
return ReportQueue{Data: make([]types.Report, 0), Size: size}
}

func (q *ReportQueue) Add(report types.Report) {
q.Mutes.Lock()

if len(q.Data) >= q.Size {
_, q.Data = q.Data[0], q.Data[1:]
}

q.Data = append(q.Data, report)
q.Mutes.Unlock()
}

func (q *ReportQueue) Has(msg types.Report) bool {
for _, elem := range q.Data {
if elem.Reportable.GetHash() == msg.Reportable.GetHash() {
return true
}
}

return false
}
26 changes: 26 additions & 0 deletions pkg/nodes_manager/queue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package nodes_manager_test

import (
configTypes "main/pkg/config/types"
nodesManagerPkg "main/pkg/nodes_manager"
"main/pkg/types"
"testing"

"github.com/stretchr/testify/require"
)

func TestQueueAdd(t *testing.T) {
t.Parallel()

queue := nodesManagerPkg.NewReportQueue(1)
report1 := types.Report{Reportable: &types.Tx{Hash: configTypes.Link{Value: "123"}}}
report2 := types.Report{Reportable: &types.Tx{Hash: configTypes.Link{Value: "456"}}}

queue.Add(report1)
require.True(t, queue.Has(report1))
require.False(t, queue.Has(report2))

queue.Add(report2)
require.True(t, queue.Has(report2))
require.False(t, queue.Has(report1))
}
84 changes: 84 additions & 0 deletions pkg/types/responses/cosmos_directory_chains_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package responses_test

import (
"main/pkg/types/responses"
"testing"

"github.com/stretchr/testify/require"
)

func TestCosmosDirectoryChainsFind(t *testing.T) {
t.Parallel()

chains := responses.CosmosDirectoryChains{
{ChainID: "chain"},
}

chain1, found1 := chains.FindByChainID("chain")
require.NotNil(t, chain1)
require.True(t, found1)

_, found2 := chains.FindByChainID("chain2")
require.False(t, found2)
}

func TestMalformedResponse(t *testing.T) {
t.Parallel()

chain := responses.CosmosDirectoryChain{
ChainID: "chain",
Assets: []responses.CosmosDirectoryAsset{
{
Denom: "denom",
Base: responses.CosmosDirectoryAssetDenomInfo{},
Display: responses.CosmosDirectoryAssetDenomInfo{},
},
},
}

denom, err := chain.GetDenomInfo("denom")
require.Error(t, err)
require.Nil(t, denom)
}

func TestDenomNotFound(t *testing.T) {
t.Parallel()

chain := responses.CosmosDirectoryChain{
ChainID: "chain",
Assets: []responses.CosmosDirectoryAsset{
{
Base: responses.CosmosDirectoryAssetDenomInfo{},
Display: responses.CosmosDirectoryAssetDenomInfo{},
},
},
}

denom, err := chain.GetDenomInfo("denom")
require.Error(t, err)
require.Nil(t, denom)
}

func TestDenomFound(t *testing.T) {
t.Parallel()

chain := responses.CosmosDirectoryChain{
ChainID: "chain",
Assets: []responses.CosmosDirectoryAsset{
{
Denom: "udenom",
Base: responses.CosmosDirectoryAssetDenomInfo{Denom: "udenom", Exponent: 3},
Display: responses.CosmosDirectoryAssetDenomInfo{Denom: "denom", Exponent: 9},
CoingeckoID: "coingecko",
},
},
}

denom, err := chain.GetDenomInfo("udenom")
require.NoError(t, err)
require.NotNil(t, denom)
require.Equal(t, "udenom", denom.Denom)
require.Equal(t, "denom", denom.DisplayDenom)
require.Equal(t, "coingecko", denom.CoingeckoCurrency)
require.Equal(t, 6, denom.DenomExponent)
}
40 changes: 40 additions & 0 deletions pkg/types/responses/responses_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package responses_test

import (
"encoding/json"
"main/pkg/types/responses"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestMarshalDurationInvalidString(t *testing.T) {
t.Parallel()

var duration responses.Duration
err := json.Unmarshal([]byte("\"invalid\""), &duration)

require.Error(t, err)
require.Zero(t, duration.Duration)
}

func TestMarshalDurationNotString(t *testing.T) {
t.Parallel()

var duration responses.Duration
err := json.Unmarshal([]byte("invalid"), &duration)

require.Error(t, err)
require.Zero(t, duration.Duration)
}

func TestMarshalDurationValid(t *testing.T) {
t.Parallel()

var duration responses.Duration
err := json.Unmarshal([]byte("\"20s\""), &duration)

require.NoError(t, err)
require.Equal(t, 20*time.Second, duration.Duration)
}
Loading