Skip to content

Commit

Permalink
chore: test NodesManager Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Mar 3, 2024
1 parent e6f6239 commit 0747999
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 35 deletions.
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
}
25 changes: 25 additions & 0 deletions pkg/nodes_manager/queue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nodes_manager_test

import (
"github.com/stretchr/testify/require"

Check failure on line 4 in pkg/nodes_manager/queue_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
configTypes "main/pkg/config/types"
nodesManagerPkg "main/pkg/nodes_manager"
"main/pkg/types"
"testing"
)

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))
}

0 comments on commit 0747999

Please sign in to comment.