erigon-pulse/erigon-lib/gointerfaces/remote/sort_test.go
Jason Yellick 5654ba07c9
Upgrade libp2p (enables go 1.21 support) (#8288)
Closes #8078 

This change is primarily intended to support go 1.21, but as a
side-effect requires updating libp2p, which in turn triggers an update
of golang.org/x/exp which creates quite a bit of (simple) churn in the
slice sorting.

This change introduces a new `cmp.Compare` function which can be used to
return an integer satisfying the compare interface for slice sorting.

In order to continue to support mplex for libp2p, the change references
github.com/libp2p/go-libp2p-mplex instead. Please see the PR at
https://github.com/libp2p/go-libp2p/pull/2498 for the official usptream
comment that indicates official support for mplex being moved to this
location.

Co-authored-by: Jason Yellick <jason@enya.ai>
2023-09-29 22:11:13 +02:00

57 lines
1.1 KiB
Go

package remote_test
import (
"testing"
"github.com/ledgerwatch/erigon-lib/gointerfaces/remote"
"github.com/ledgerwatch/erigon-lib/gointerfaces/types"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/slices"
)
func TestSort(t *testing.T) {
tests := []struct {
name string
got *remote.NodesInfoReply
want *remote.NodesInfoReply
}{
{
name: "sort by name",
got: &remote.NodesInfoReply{
NodesInfo: []*types.NodeInfoReply{
{Name: "b", Enode: "c"},
{Name: "a", Enode: "d"},
},
},
want: &remote.NodesInfoReply{
NodesInfo: []*types.NodeInfoReply{
{Name: "a", Enode: "d"},
{Name: "b", Enode: "c"},
},
},
},
{
name: "sort by enode",
got: &remote.NodesInfoReply{
NodesInfo: []*types.NodeInfoReply{
{Name: "a", Enode: "d"},
{Name: "a", Enode: "c"},
},
},
want: &remote.NodesInfoReply{
NodesInfo: []*types.NodeInfoReply{
{Name: "a", Enode: "c"},
{Name: "a", Enode: "d"},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
slices.SortFunc(tt.got.NodesInfo, remote.NodeInfoReplyCmp)
assert.Equal(t, tt.want, tt.got)
})
}
}