From 5efaf56650f33e28c45d5d6eca198a076ff963cb Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Mon, 18 Nov 2019 18:46:24 +0300 Subject: [PATCH] Fix RouteGroup bug --- pkg/router/route_group.go | 5 +- pkg/router/route_group_test.go | 510 +++++++++++++++++++++++++++++++++ 2 files changed, 513 insertions(+), 2 deletions(-) create mode 100644 pkg/router/route_group_test.go diff --git a/pkg/router/route_group.go b/pkg/router/route_group.go index 7094e0543..e3b93db60 100644 --- a/pkg/router/route_group.go +++ b/pkg/router/route_group.go @@ -68,12 +68,13 @@ func NewRouteGroup(rt routing.Table, desc routing.RouteDescriptor) *RouteGroup { rg := &RouteGroup{ logger: logging.MustGetLogger(fmt.Sprintf("RouteGroup %v", desc)), desc: desc, + rt: rt, + tps: make([]*transport.ManagedTransport, 0), fwd: make([]routing.Rule, 0), rvs: make([]routing.Rule, 0), - tps: make([]*transport.ManagedTransport, 0), readCh: make(chan []byte, readChBufSize), readBuf: bytes.Buffer{}, - rt: rt, + done: make(chan struct{}), } go rg.keepAliveLoop() diff --git a/pkg/router/route_group_test.go b/pkg/router/route_group_test.go new file mode 100644 index 000000000..1f7138baf --- /dev/null +++ b/pkg/router/route_group_test.go @@ -0,0 +1,510 @@ +package router + +import ( + "bytes" + "net" + "reflect" + "sync" + "testing" + "time" + + "github.com/SkycoinProject/dmsg/cipher" + "github.com/SkycoinProject/skycoin/src/util/logging" + "github.com/stretchr/testify/require" + + "github.com/SkycoinProject/skywire-mainnet/pkg/routing" + "github.com/SkycoinProject/skywire-mainnet/pkg/transport" +) + +func TestNewRouteGroup(t *testing.T) { + rt := routing.NewTable(routing.DefaultConfig()) + + pk1, _ := cipher.GenerateKeyPair() + pk2, _ := cipher.GenerateKeyPair() + port1 := routing.Port(1) + port2 := routing.Port(2) + desc := routing.NewRouteDescriptor(pk1, pk2, port1, port2) + + rg := NewRouteGroup(rt, desc) + require.NotNil(t, rg) + + require.NoError(t, rg.Close()) +} + +func TestRouteGroup_LocalAddr(t *testing.T) { + type fields struct { + mu sync.RWMutex + logger *logging.Logger + desc routing.RouteDescriptor + rt routing.Table + tps []*transport.ManagedTransport + fwd []routing.Rule + rvs []routing.Rule + lastSent int64 + readCh chan []byte + readBuf bytes.Buffer + done chan struct{} + once sync.Once + } + tests := []struct { + name string + fields fields + want net.Addr + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := &RouteGroup{ + mu: tt.fields.mu, + logger: tt.fields.logger, + desc: tt.fields.desc, + rt: tt.fields.rt, + tps: tt.fields.tps, + fwd: tt.fields.fwd, + rvs: tt.fields.rvs, + lastSent: tt.fields.lastSent, + readCh: tt.fields.readCh, + readBuf: tt.fields.readBuf, + done: tt.fields.done, + once: tt.fields.once, + } + if got := r.LocalAddr(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("LocalAddr() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestRouteGroup_Read(t *testing.T) { + type fields struct { + mu sync.RWMutex + logger *logging.Logger + desc routing.RouteDescriptor + rt routing.Table + tps []*transport.ManagedTransport + fwd []routing.Rule + rvs []routing.Rule + lastSent int64 + readCh chan []byte + readBuf bytes.Buffer + done chan struct{} + once sync.Once + } + type args struct { + p []byte + } + tests := []struct { + name string + fields fields + args args + wantN int + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := &RouteGroup{ + mu: tt.fields.mu, + logger: tt.fields.logger, + desc: tt.fields.desc, + rt: tt.fields.rt, + tps: tt.fields.tps, + fwd: tt.fields.fwd, + rvs: tt.fields.rvs, + lastSent: tt.fields.lastSent, + readCh: tt.fields.readCh, + readBuf: tt.fields.readBuf, + done: tt.fields.done, + once: tt.fields.once, + } + gotN, err := r.Read(tt.args.p) + if (err != nil) != tt.wantErr { + t.Errorf("Read() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotN != tt.wantN { + t.Errorf("Read() gotN = %v, want %v", gotN, tt.wantN) + } + }) + } +} + +func TestRouteGroup_RemoteAddr(t *testing.T) { + type fields struct { + mu sync.RWMutex + logger *logging.Logger + desc routing.RouteDescriptor + rt routing.Table + tps []*transport.ManagedTransport + fwd []routing.Rule + rvs []routing.Rule + lastSent int64 + readCh chan []byte + readBuf bytes.Buffer + done chan struct{} + once sync.Once + } + tests := []struct { + name string + fields fields + want net.Addr + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := &RouteGroup{ + mu: tt.fields.mu, + logger: tt.fields.logger, + desc: tt.fields.desc, + rt: tt.fields.rt, + tps: tt.fields.tps, + fwd: tt.fields.fwd, + rvs: tt.fields.rvs, + lastSent: tt.fields.lastSent, + readCh: tt.fields.readCh, + readBuf: tt.fields.readBuf, + done: tt.fields.done, + once: tt.fields.once, + } + if got := r.RemoteAddr(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("RemoteAddr() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestRouteGroup_SetDeadline(t *testing.T) { + type fields struct { + mu sync.RWMutex + logger *logging.Logger + desc routing.RouteDescriptor + rt routing.Table + tps []*transport.ManagedTransport + fwd []routing.Rule + rvs []routing.Rule + lastSent int64 + readCh chan []byte + readBuf bytes.Buffer + done chan struct{} + once sync.Once + } + type args struct { + t time.Time + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := &RouteGroup{ + mu: tt.fields.mu, + logger: tt.fields.logger, + desc: tt.fields.desc, + rt: tt.fields.rt, + tps: tt.fields.tps, + fwd: tt.fields.fwd, + rvs: tt.fields.rvs, + lastSent: tt.fields.lastSent, + readCh: tt.fields.readCh, + readBuf: tt.fields.readBuf, + done: tt.fields.done, + once: tt.fields.once, + } + if err := r.SetDeadline(tt.args.t); (err != nil) != tt.wantErr { + t.Errorf("SetDeadline() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestRouteGroup_SetReadDeadline(t *testing.T) { + type fields struct { + mu sync.RWMutex + logger *logging.Logger + desc routing.RouteDescriptor + rt routing.Table + tps []*transport.ManagedTransport + fwd []routing.Rule + rvs []routing.Rule + lastSent int64 + readCh chan []byte + readBuf bytes.Buffer + done chan struct{} + once sync.Once + } + type args struct { + t time.Time + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := &RouteGroup{ + mu: tt.fields.mu, + logger: tt.fields.logger, + desc: tt.fields.desc, + rt: tt.fields.rt, + tps: tt.fields.tps, + fwd: tt.fields.fwd, + rvs: tt.fields.rvs, + lastSent: tt.fields.lastSent, + readCh: tt.fields.readCh, + readBuf: tt.fields.readBuf, + done: tt.fields.done, + once: tt.fields.once, + } + if err := r.SetReadDeadline(tt.args.t); (err != nil) != tt.wantErr { + t.Errorf("SetReadDeadline() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestRouteGroup_SetWriteDeadline(t *testing.T) { + type fields struct { + mu sync.RWMutex + logger *logging.Logger + desc routing.RouteDescriptor + rt routing.Table + tps []*transport.ManagedTransport + fwd []routing.Rule + rvs []routing.Rule + lastSent int64 + readCh chan []byte + readBuf bytes.Buffer + done chan struct{} + once sync.Once + } + type args struct { + t time.Time + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := &RouteGroup{ + mu: tt.fields.mu, + logger: tt.fields.logger, + desc: tt.fields.desc, + rt: tt.fields.rt, + tps: tt.fields.tps, + fwd: tt.fields.fwd, + rvs: tt.fields.rvs, + lastSent: tt.fields.lastSent, + readCh: tt.fields.readCh, + readBuf: tt.fields.readBuf, + done: tt.fields.done, + once: tt.fields.once, + } + if err := r.SetWriteDeadline(tt.args.t); (err != nil) != tt.wantErr { + t.Errorf("SetWriteDeadline() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestRouteGroup_Write(t *testing.T) { + type fields struct { + mu sync.RWMutex + logger *logging.Logger + desc routing.RouteDescriptor + rt routing.Table + tps []*transport.ManagedTransport + fwd []routing.Rule + rvs []routing.Rule + lastSent int64 + readCh chan []byte + readBuf bytes.Buffer + done chan struct{} + once sync.Once + } + type args struct { + p []byte + } + tests := []struct { + name string + fields fields + args args + wantN int + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := &RouteGroup{ + mu: tt.fields.mu, + logger: tt.fields.logger, + desc: tt.fields.desc, + rt: tt.fields.rt, + tps: tt.fields.tps, + fwd: tt.fields.fwd, + rvs: tt.fields.rvs, + lastSent: tt.fields.lastSent, + readCh: tt.fields.readCh, + readBuf: tt.fields.readBuf, + done: tt.fields.done, + once: tt.fields.once, + } + gotN, err := r.Write(tt.args.p) + if (err != nil) != tt.wantErr { + t.Errorf("Write() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotN != tt.wantN { + t.Errorf("Write() gotN = %v, want %v", gotN, tt.wantN) + } + }) + } +} + +func TestRouteGroup_isClosing(t *testing.T) { + type fields struct { + mu sync.RWMutex + logger *logging.Logger + desc routing.RouteDescriptor + rt routing.Table + tps []*transport.ManagedTransport + fwd []routing.Rule + rvs []routing.Rule + lastSent int64 + readCh chan []byte + readBuf bytes.Buffer + done chan struct{} + once sync.Once + } + tests := []struct { + name string + fields fields + want bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := &RouteGroup{ + mu: tt.fields.mu, + logger: tt.fields.logger, + desc: tt.fields.desc, + rt: tt.fields.rt, + tps: tt.fields.tps, + fwd: tt.fields.fwd, + rvs: tt.fields.rvs, + lastSent: tt.fields.lastSent, + readCh: tt.fields.readCh, + readBuf: tt.fields.readBuf, + done: tt.fields.done, + once: tt.fields.once, + } + if got := r.isClosing(); got != tt.want { + t.Errorf("isClosing() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestRouteGroup_keepAliveLoop(t *testing.T) { + type fields struct { + mu sync.RWMutex + logger *logging.Logger + desc routing.RouteDescriptor + rt routing.Table + tps []*transport.ManagedTransport + fwd []routing.Rule + rvs []routing.Rule + lastSent int64 + readCh chan []byte + readBuf bytes.Buffer + done chan struct{} + once sync.Once + } + tests := []struct { + name string + fields fields + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _ = &RouteGroup{ + mu: tt.fields.mu, + logger: tt.fields.logger, + desc: tt.fields.desc, + rt: tt.fields.rt, + tps: tt.fields.tps, + fwd: tt.fields.fwd, + rvs: tt.fields.rvs, + lastSent: tt.fields.lastSent, + readCh: tt.fields.readCh, + readBuf: tt.fields.readBuf, + done: tt.fields.done, + once: tt.fields.once, + } + }) + } +} + +func TestRouteGroup_sendKeepAlive(t *testing.T) { + type fields struct { + mu sync.RWMutex + logger *logging.Logger + desc routing.RouteDescriptor + rt routing.Table + tps []*transport.ManagedTransport + fwd []routing.Rule + rvs []routing.Rule + lastSent int64 + readCh chan []byte + readBuf bytes.Buffer + done chan struct{} + once sync.Once + } + tests := []struct { + name string + fields fields + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := &RouteGroup{ + mu: tt.fields.mu, + logger: tt.fields.logger, + desc: tt.fields.desc, + rt: tt.fields.rt, + tps: tt.fields.tps, + fwd: tt.fields.fwd, + rvs: tt.fields.rvs, + lastSent: tt.fields.lastSent, + readCh: tt.fields.readCh, + readBuf: tt.fields.readBuf, + done: tt.fields.done, + once: tt.fields.once, + } + if err := r.sendKeepAlive(); (err != nil) != tt.wantErr { + t.Errorf("sendKeepAlive() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}