2022-01-19 18:30:05 +01:00
|
|
|
package server_test
|
2021-10-25 03:39:37 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-11-15 14:52:32 +01:00
|
|
|
"net"
|
2021-10-25 03:39:37 +02:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-10-30 07:27:25 +02:00
|
|
|
"github.com/lbryio/hub/internal/metrics"
|
2021-10-25 03:39:37 +02:00
|
|
|
pb "github.com/lbryio/hub/protobuf/go"
|
2022-01-19 18:30:05 +01:00
|
|
|
server "github.com/lbryio/hub/server"
|
2021-10-30 07:27:25 +02:00
|
|
|
dto "github.com/prometheus/client_model/go"
|
|
|
|
"google.golang.org/grpc"
|
2021-10-25 03:39:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// lineCountFile takes a fileName and counts the number of lines in it.
|
|
|
|
func lineCountFile(fileName string) int {
|
|
|
|
f, err := os.Open(fileName)
|
|
|
|
defer f.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
var lineCount = 0
|
|
|
|
for scanner.Scan() {
|
|
|
|
scanner.Text()
|
|
|
|
lineCount = lineCount + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return lineCount
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeFile removes a file.
|
|
|
|
func removeFile(fileName string) {
|
|
|
|
err := os.Remove(fileName)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 18:30:05 +01:00
|
|
|
func makeDefaultArgs() *server.Args {
|
|
|
|
args := &server.Args{
|
2022-03-10 19:31:54 +01:00
|
|
|
CmdType: server.ServeCmd,
|
|
|
|
Host: server.DefaultHost,
|
|
|
|
Port: server.DefaultPort,
|
|
|
|
DBPath: server.DefaultDBPath,
|
|
|
|
EsHost: server.DefaultEsHost,
|
|
|
|
EsPort: server.DefaultEsPort,
|
|
|
|
PrometheusPort: server.DefaultPrometheusPort,
|
|
|
|
EsIndex: server.DefaultEsIndex,
|
|
|
|
RefreshDelta: server.DefaultRefreshDelta,
|
|
|
|
CacheTTL: server.DefaultCacheTTL,
|
|
|
|
PeerFile: server.DefaultPeerFile,
|
|
|
|
Country: server.DefaultCountry,
|
|
|
|
DisableEs: true,
|
|
|
|
Debug: true,
|
|
|
|
DisableLoadPeers: true,
|
|
|
|
DisableStartPrometheus: true,
|
|
|
|
DisableStartUDP: true,
|
|
|
|
DisableWritePeers: true,
|
|
|
|
DisableRocksDBRefresh: true,
|
|
|
|
DisableResolve: true,
|
|
|
|
DisableBlockingAndFiltering: true,
|
2021-10-25 03:39:37 +02:00
|
|
|
}
|
|
|
|
|
2021-10-30 07:27:25 +02:00
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAddPeer tests the ability to add peers
|
|
|
|
func TestAddPeer(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
args := makeDefaultArgs()
|
|
|
|
|
2021-10-25 03:39:37 +02:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
want int
|
2021-11-25 00:24:06 +01:00
|
|
|
}{
|
2021-10-25 03:39:37 +02:00
|
|
|
{
|
|
|
|
name: "Add 10 peers",
|
|
|
|
want: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Add 10 peers, 1 unique",
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2021-11-25 00:24:06 +01:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-01-19 18:30:05 +01:00
|
|
|
hubServer := server.MakeHubServer(ctx, args)
|
|
|
|
hubServer.ExternalIP = net.IPv4(0, 0, 0, 0)
|
2021-10-30 07:27:25 +02:00
|
|
|
metrics.PeersKnown.Set(0)
|
2021-10-25 03:39:37 +02:00
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2022-01-19 18:30:05 +01:00
|
|
|
var peer *server.Peer
|
2021-10-25 03:39:37 +02:00
|
|
|
if strings.Contains(tt.name, "1 unique") {
|
2022-01-19 18:30:05 +01:00
|
|
|
peer = &server.Peer{
|
2021-10-30 07:27:25 +02:00
|
|
|
Address: "1.1.1.1",
|
|
|
|
Port: "50051",
|
2021-10-25 03:39:37 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-30 07:27:25 +02:00
|
|
|
x := i + 1
|
2022-01-19 18:30:05 +01:00
|
|
|
peer = &server.Peer{
|
2021-10-30 07:27:25 +02:00
|
|
|
Address: fmt.Sprintf("%d.%d.%d.%d", x, x, x, x),
|
|
|
|
Port: "50051",
|
2021-10-25 03:39:37 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-30 07:27:25 +02:00
|
|
|
//log.Printf("Adding peer %+v\n", msg)
|
2022-01-19 18:30:05 +01:00
|
|
|
err := hubServer.AddPeerExported()(peer, false, false)
|
2021-10-30 07:27:25 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2021-10-25 03:39:37 +02:00
|
|
|
}
|
2021-10-30 07:27:25 +02:00
|
|
|
var m = &dto.Metric{}
|
|
|
|
if err := metrics.PeersKnown.Write(m); err != nil {
|
|
|
|
t.Errorf("Error getting metrics %+v\n", err)
|
|
|
|
}
|
|
|
|
got := int(*m.Gauge.Value)
|
2021-10-25 03:39:37 +02:00
|
|
|
if got != tt.want {
|
2021-10-30 07:27:25 +02:00
|
|
|
t.Errorf("len(server.PeerServers) = %d, want %d\n", got, tt.want)
|
2021-10-25 03:39:37 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-30 07:27:25 +02:00
|
|
|
// TestPeerWriter tests that peers get written properly
|
2021-10-25 03:39:37 +02:00
|
|
|
func TestPeerWriter(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2021-10-30 07:27:25 +02:00
|
|
|
args := makeDefaultArgs()
|
2021-11-25 00:56:34 +01:00
|
|
|
args.DisableWritePeers = false
|
2021-10-25 03:39:37 +02:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
want int
|
2021-11-25 00:24:06 +01:00
|
|
|
}{
|
2021-10-25 03:39:37 +02:00
|
|
|
{
|
|
|
|
name: "Add 10 peers",
|
|
|
|
want: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Add 10 peers, 1 unique",
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2021-11-25 00:24:06 +01:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-01-19 18:30:05 +01:00
|
|
|
hubServer := server.MakeHubServer(ctx, args)
|
|
|
|
hubServer.ExternalIP = net.IPv4(0, 0, 0, 0)
|
2021-10-25 03:39:37 +02:00
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2022-01-19 18:30:05 +01:00
|
|
|
var peer *server.Peer
|
2021-10-25 03:39:37 +02:00
|
|
|
if strings.Contains(tt.name, "1 unique") {
|
2022-01-19 18:30:05 +01:00
|
|
|
peer = &server.Peer{
|
2021-10-30 07:27:25 +02:00
|
|
|
Address: "1.1.1.1",
|
|
|
|
Port: "50051",
|
2021-10-25 03:39:37 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-30 07:36:24 +02:00
|
|
|
x := i + 1
|
2022-01-19 18:30:05 +01:00
|
|
|
peer = &server.Peer{
|
2021-10-30 07:36:24 +02:00
|
|
|
Address: fmt.Sprintf("%d.%d.%d.%d", x, x, x, x),
|
2021-10-30 07:27:25 +02:00
|
|
|
Port: "50051",
|
2021-10-25 03:39:37 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-02 01:32:23 +01:00
|
|
|
//log.Printf("Adding peer %+v\n", peer)
|
2022-01-19 18:30:05 +01:00
|
|
|
err := hubServer.AddPeerExported()(peer, false, false)
|
2021-10-30 07:27:25 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2021-10-25 03:39:37 +02:00
|
|
|
}
|
2021-10-30 07:27:25 +02:00
|
|
|
//log.Println("Counting lines...")
|
2022-01-19 18:30:05 +01:00
|
|
|
got := lineCountFile(hubServer.Args.PeerFile)
|
2021-10-25 03:39:37 +02:00
|
|
|
if got != tt.want {
|
2021-10-30 07:27:25 +02:00
|
|
|
t.Errorf("lineCountFile(peers.txt) = %d, want %d", got, tt.want)
|
2021-10-25 03:39:37 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
removeFile(args.PeerFile)
|
|
|
|
}
|
2021-10-30 07:27:25 +02:00
|
|
|
|
|
|
|
// TestAddPeerEndpoint tests the ability to add peers
|
|
|
|
func TestAddPeerEndpoint(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
args := makeDefaultArgs()
|
|
|
|
args2 := makeDefaultArgs()
|
|
|
|
args2.Port = "50052"
|
|
|
|
|
|
|
|
tests := []struct {
|
2021-11-25 00:24:06 +01:00
|
|
|
name string
|
2021-10-30 07:27:25 +02:00
|
|
|
wantServerOne int64
|
|
|
|
wantServerTwo int64
|
2021-11-25 00:24:06 +01:00
|
|
|
}{
|
2021-10-30 07:27:25 +02:00
|
|
|
{
|
|
|
|
// outside -> server1.AddPeer(server2, ping=true) : server1 = 1, server2 = 0
|
|
|
|
// server1 -> server2.Hello(server1) : server1 = 1, server2 = 0
|
|
|
|
// server2 -> server2.addPeer(server1, ping=false) : server1 = 1, server2 = 1
|
|
|
|
// server2 -> server1.PeerSubscribe(server2) : server1 = 1, server2 = 1
|
|
|
|
// server1 <- server2.makeHelloMessage() : server1 = 1, server2 = 1
|
|
|
|
// server1.notifyPeer() : server1 = 1, server2 = 1
|
|
|
|
// server1 -> server2.AddPeer(server2) : server1 = 1, server2 = 1
|
|
|
|
// server2 self peer, skipping : server1 = 1, server2 = 1
|
|
|
|
// server1 -> server2.PeerSubscribe(server1) : server1 = 1, server2 = 1
|
2021-11-25 00:24:06 +01:00
|
|
|
name: "Add 1 peer",
|
2021-10-30 07:27:25 +02:00
|
|
|
wantServerOne: 1,
|
|
|
|
wantServerTwo: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2021-11-25 00:24:06 +01:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-01-19 18:30:05 +01:00
|
|
|
hubServer := server.MakeHubServer(ctx, args)
|
|
|
|
hubServer2 := server.MakeHubServer(ctx, args2)
|
2021-10-30 07:27:25 +02:00
|
|
|
metrics.PeersKnown.Set(0)
|
2022-01-19 18:30:05 +01:00
|
|
|
go hubServer.Run()
|
|
|
|
go hubServer2.Run()
|
|
|
|
//go hubServer.Run()
|
2021-10-30 07:27:25 +02:00
|
|
|
conn, err := grpc.Dial("localhost:"+args.Port,
|
|
|
|
grpc.WithInsecure(),
|
|
|
|
grpc.WithBlock(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("did not connect: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := pb.NewHubClient(conn)
|
|
|
|
|
|
|
|
msg := &pb.ServerMessage{
|
|
|
|
Address: "0.0.0.0",
|
|
|
|
Port: "50052",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.AddPeer(context.Background(), msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
2022-01-19 18:30:05 +01:00
|
|
|
hubServer.GrpcServer.GracefulStop()
|
|
|
|
hubServer2.GrpcServer.GracefulStop()
|
|
|
|
got1 := hubServer.GetNumPeersExported()()
|
|
|
|
got2 := hubServer2.GetNumPeersExported()()
|
2021-10-30 07:27:25 +02:00
|
|
|
if got1 != tt.wantServerOne {
|
2022-01-19 18:30:05 +01:00
|
|
|
t.Errorf("len(hubServer.PeerServers) = %d, want %d\n", got1, tt.wantServerOne)
|
2021-10-30 07:27:25 +02:00
|
|
|
}
|
|
|
|
if got2 != tt.wantServerTwo {
|
2022-01-19 18:30:05 +01:00
|
|
|
t.Errorf("len(hubServer2.PeerServers) = %d, want %d\n", got2, tt.wantServerTwo)
|
2021-10-30 07:27:25 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAddPeerEndpoint2 tests the ability to add peers
|
|
|
|
func TestAddPeerEndpoint2(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
args := makeDefaultArgs()
|
|
|
|
args2 := makeDefaultArgs()
|
|
|
|
args3 := makeDefaultArgs()
|
|
|
|
args2.Port = "50052"
|
|
|
|
args3.Port = "50053"
|
|
|
|
|
|
|
|
tests := []struct {
|
2021-11-25 00:24:06 +01:00
|
|
|
name string
|
2021-10-30 07:27:25 +02:00
|
|
|
wantServerOne int64
|
|
|
|
wantServerTwo int64
|
|
|
|
wantServerThree int64
|
2021-11-25 00:24:06 +01:00
|
|
|
}{
|
2021-10-30 07:27:25 +02:00
|
|
|
{
|
2021-11-25 00:24:06 +01:00
|
|
|
name: "Add 2 peers",
|
2021-10-30 07:27:25 +02:00
|
|
|
wantServerOne: 2,
|
|
|
|
wantServerTwo: 2,
|
|
|
|
wantServerThree: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2021-11-25 00:24:06 +01:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-01-19 18:30:05 +01:00
|
|
|
hubServer := server.MakeHubServer(ctx, args)
|
|
|
|
hubServer2 := server.MakeHubServer(ctx, args2)
|
|
|
|
hubServer3 := server.MakeHubServer(ctx, args3)
|
2021-10-30 07:27:25 +02:00
|
|
|
metrics.PeersKnown.Set(0)
|
2022-01-19 18:30:05 +01:00
|
|
|
go hubServer.Run()
|
|
|
|
go hubServer2.Run()
|
|
|
|
go hubServer3.Run()
|
2021-10-30 07:27:25 +02:00
|
|
|
conn, err := grpc.Dial("localhost:"+args.Port,
|
|
|
|
grpc.WithInsecure(),
|
|
|
|
grpc.WithBlock(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("did not connect: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := pb.NewHubClient(conn)
|
|
|
|
|
|
|
|
msg := &pb.ServerMessage{
|
|
|
|
Address: "0.0.0.0",
|
|
|
|
Port: "50052",
|
|
|
|
}
|
|
|
|
|
|
|
|
msg2 := &pb.ServerMessage{
|
|
|
|
Address: "0.0.0.0",
|
|
|
|
Port: "50053",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.AddPeer(context.Background(), msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
_, err = c.AddPeer(context.Background(), msg2)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
2022-01-19 18:30:05 +01:00
|
|
|
hubServer.GrpcServer.GracefulStop()
|
|
|
|
hubServer2.GrpcServer.GracefulStop()
|
|
|
|
hubServer3.GrpcServer.GracefulStop()
|
|
|
|
got1 := hubServer.GetNumPeersExported()()
|
|
|
|
got2 := hubServer2.GetNumPeersExported()()
|
|
|
|
got3 := hubServer3.GetNumPeersExported()()
|
2021-10-30 07:27:25 +02:00
|
|
|
if got1 != tt.wantServerOne {
|
2022-01-19 18:30:05 +01:00
|
|
|
t.Errorf("len(hubServer.PeerServers) = %d, want %d\n", got1, tt.wantServerOne)
|
2021-10-30 07:27:25 +02:00
|
|
|
}
|
|
|
|
if got2 != tt.wantServerTwo {
|
2022-01-19 18:30:05 +01:00
|
|
|
t.Errorf("len(hubServer2.PeerServers) = %d, want %d\n", got2, tt.wantServerTwo)
|
2021-10-30 07:27:25 +02:00
|
|
|
}
|
|
|
|
if got3 != tt.wantServerThree {
|
2022-01-19 18:30:05 +01:00
|
|
|
t.Errorf("len(hubServer3.PeerServers) = %d, want %d\n", got3, tt.wantServerThree)
|
2021-10-30 07:27:25 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAddPeerEndpoint3 tests the ability to add peers
|
|
|
|
func TestAddPeerEndpoint3(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
args := makeDefaultArgs()
|
|
|
|
args2 := makeDefaultArgs()
|
|
|
|
args3 := makeDefaultArgs()
|
|
|
|
args2.Port = "50052"
|
|
|
|
args3.Port = "50053"
|
|
|
|
|
|
|
|
tests := []struct {
|
2021-11-25 00:24:06 +01:00
|
|
|
name string
|
2021-10-30 07:27:25 +02:00
|
|
|
wantServerOne int64
|
|
|
|
wantServerTwo int64
|
|
|
|
wantServerThree int64
|
2021-11-25 00:24:06 +01:00
|
|
|
}{
|
2021-10-30 07:27:25 +02:00
|
|
|
{
|
2021-11-25 00:24:06 +01:00
|
|
|
name: "Add 1 peer to each",
|
2021-10-30 07:27:25 +02:00
|
|
|
wantServerOne: 2,
|
|
|
|
wantServerTwo: 2,
|
|
|
|
wantServerThree: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2021-11-25 00:24:06 +01:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-01-19 18:30:05 +01:00
|
|
|
hubServer := server.MakeHubServer(ctx, args)
|
|
|
|
hubServer2 := server.MakeHubServer(ctx, args2)
|
|
|
|
hubServer3 := server.MakeHubServer(ctx, args3)
|
2021-10-30 07:27:25 +02:00
|
|
|
metrics.PeersKnown.Set(0)
|
2022-01-19 18:30:05 +01:00
|
|
|
go hubServer.Run()
|
|
|
|
go hubServer2.Run()
|
|
|
|
go hubServer3.Run()
|
2021-10-30 07:27:25 +02:00
|
|
|
conn, err := grpc.Dial("localhost:"+args.Port,
|
|
|
|
grpc.WithInsecure(),
|
|
|
|
grpc.WithBlock(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("did not connect: %v", err)
|
|
|
|
}
|
|
|
|
conn2, err := grpc.Dial("localhost:50052",
|
|
|
|
grpc.WithInsecure(),
|
|
|
|
grpc.WithBlock(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("did not connect: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := pb.NewHubClient(conn)
|
|
|
|
c2 := pb.NewHubClient(conn2)
|
|
|
|
|
|
|
|
msg := &pb.ServerMessage{
|
|
|
|
Address: "0.0.0.0",
|
|
|
|
Port: "50052",
|
|
|
|
}
|
|
|
|
|
|
|
|
msg2 := &pb.ServerMessage{
|
|
|
|
Address: "0.0.0.0",
|
|
|
|
Port: "50053",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.AddPeer(context.Background(), msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
_, err = c2.AddPeer(context.Background(), msg2)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
2022-01-19 18:30:05 +01:00
|
|
|
hubServer.GrpcServer.GracefulStop()
|
|
|
|
hubServer2.GrpcServer.GracefulStop()
|
|
|
|
hubServer3.GrpcServer.GracefulStop()
|
|
|
|
got1 := hubServer.GetNumPeersExported()()
|
|
|
|
got2 := hubServer2.GetNumPeersExported()()
|
|
|
|
got3 := hubServer3.GetNumPeersExported()()
|
2021-10-30 07:27:25 +02:00
|
|
|
if got1 != tt.wantServerOne {
|
2022-01-19 18:30:05 +01:00
|
|
|
t.Errorf("len(hubServer.PeerServers) = %d, want %d\n", got1, tt.wantServerOne)
|
2021-10-30 07:27:25 +02:00
|
|
|
}
|
|
|
|
if got2 != tt.wantServerTwo {
|
2022-01-19 18:30:05 +01:00
|
|
|
t.Errorf("len(hubServer2.PeerServers) = %d, want %d\n", got2, tt.wantServerTwo)
|
2021-10-30 07:27:25 +02:00
|
|
|
}
|
|
|
|
if got3 != tt.wantServerThree {
|
2022-01-19 18:30:05 +01:00
|
|
|
t.Errorf("len(hubServer3.PeerServers) = %d, want %d\n", got3, tt.wantServerThree)
|
2021-10-30 07:27:25 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-11-10 01:39:13 +01:00
|
|
|
|
|
|
|
// TestAddPeer tests the ability to add peers
|
|
|
|
func TestUDPServer(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
args := makeDefaultArgs()
|
2021-11-25 00:56:34 +01:00
|
|
|
args.DisableStartUDP = false
|
2021-11-10 01:39:13 +01:00
|
|
|
args2 := makeDefaultArgs()
|
|
|
|
args2.Port = "50052"
|
2021-11-25 00:56:34 +01:00
|
|
|
args2.DisableStartUDP = false
|
2021-11-10 01:39:13 +01:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
want string
|
2021-11-25 00:24:06 +01:00
|
|
|
}{
|
2021-11-10 01:39:13 +01:00
|
|
|
{
|
2022-01-19 18:30:05 +01:00
|
|
|
name: "hubs hubServer external ip",
|
2021-11-10 01:39:13 +01:00
|
|
|
want: "127.0.0.1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2021-11-25 00:24:06 +01:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-01-19 18:30:05 +01:00
|
|
|
hubServer := server.MakeHubServer(ctx, args)
|
|
|
|
hubServer2 := server.MakeHubServer(ctx, args2)
|
|
|
|
go hubServer.Run()
|
|
|
|
go hubServer2.Run()
|
2021-11-10 01:39:13 +01:00
|
|
|
metrics.PeersKnown.Set(0)
|
|
|
|
|
2022-01-19 18:30:05 +01:00
|
|
|
peer := &server.Peer{
|
2021-11-10 01:39:13 +01:00
|
|
|
Address: "0.0.0.0",
|
|
|
|
Port: "50052",
|
|
|
|
}
|
|
|
|
|
2022-01-19 18:30:05 +01:00
|
|
|
err := hubServer.AddPeerExported()(peer, true, true)
|
2021-11-10 01:39:13 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
2022-01-19 18:30:05 +01:00
|
|
|
hubServer.GrpcServer.GracefulStop()
|
|
|
|
hubServer2.GrpcServer.GracefulStop()
|
2021-11-10 01:39:13 +01:00
|
|
|
|
2022-01-19 18:30:05 +01:00
|
|
|
got1 := hubServer.ExternalIP.String()
|
2021-11-10 01:39:13 +01:00
|
|
|
if got1 != tt.want {
|
2022-01-19 18:30:05 +01:00
|
|
|
t.Errorf("hubServer.ExternalIP = %s, want %s\n", got1, tt.want)
|
|
|
|
t.Errorf("hubServer.Args.Port = %s\n", hubServer.Args.Port)
|
2021-11-10 01:39:13 +01:00
|
|
|
}
|
2022-01-19 18:30:05 +01:00
|
|
|
got2 := hubServer2.ExternalIP.String()
|
2021-11-10 01:39:13 +01:00
|
|
|
if got2 != tt.want {
|
2022-01-19 18:30:05 +01:00
|
|
|
t.Errorf("hubServer2.ExternalIP = %s, want %s\n", got2, tt.want)
|
|
|
|
t.Errorf("hubServer2.Args.Port = %s\n", hubServer2.Args.Port)
|
2021-11-10 01:39:13 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|