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

Eliminate a ton of new machinery #150

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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/stretchr/testify v1.4.0
github.com/uber/jaeger-client-go v2.21.1+incompatible
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
go.uber.org/atomic v1.5.1
go.uber.org/atomic v1.5.1 // indirect
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a // indirect
gonum.org/v1/gonum v0.6.2
google.golang.org/grpc v1.27.0
Expand Down
181 changes: 95 additions & 86 deletions pkg/networkservice/common/heal/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ import (
"testing"
"time"

. "github.com/networkservicemesh/sdk/pkg/test/fixtures/healclientfixture"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc"

"github.com/networkservicemesh/sdk/pkg/networkservice/common/heal"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/eventchannel"
"github.com/networkservicemesh/sdk/pkg/tools/addressof"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/stretchr/testify/require"
Expand All @@ -33,45 +39,65 @@ const (
tickTimeout = 10 * time.Millisecond
)

type testOnHeal struct {
RequestFunc func(ctx context.Context, in *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error)
CloseFunc func(ctx context.Context, in *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error)
}

func (t *testOnHeal) Request(ctx context.Context, in *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
return t.RequestFunc(ctx, in, opts...)
}

func (t *testOnHeal) Close(ctx context.Context, in *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
return t.CloseFunc(ctx, in, opts...)
}

func TestHealClient_Request(t *testing.T) {
f := &Fixture{
Request: &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "conn-1",
NetworkService: "ns-1",
},
eventCh := make(chan *networkservice.ConnectionEvent, 1)

onHealCh := make(chan struct{})
onHeal := &testOnHeal{
RequestFunc: func(ctx context.Context, in *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (connection *networkservice.Connection, e error) {
close(onHealCh)
return &networkservice.Connection{}, nil
},
}
err := SetupWithSingleRequest(f)
defer TearDown(f)

client := chain.NewNetworkServiceClient(
heal.NewClient(context.Background(),
eventchannel.NewMonitorConnectionClient(eventCh), addressof.NetworkServiceClient(onHeal)))

_, err := client.Request(context.Background(), &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "conn-1",
NetworkService: "ns-1",
},
})
require.Nil(t, err)
require.True(t, reflect.DeepEqual(f.Conn, f.Request.GetConnection()))

err = f.ServerStream.Send(&networkservice.ConnectionEvent{
eventCh <- &networkservice.ConnectionEvent{
Type: networkservice.ConnectionEventType_INITIAL_STATE_TRANSFER,
Connections: map[string]*networkservice.Connection{
"conn-1": {
Id: "conn-1",
NetworkService: "ns-1",
},
},
})
require.Nil(t, err)
}

err = f.ServerStream.Send(&networkservice.ConnectionEvent{
eventCh <- &networkservice.ConnectionEvent{
Type: networkservice.ConnectionEventType_DELETE,
Connections: map[string]*networkservice.Connection{
"conn-1": {
Id: "conn-1",
NetworkService: "ns-1",
},
},
})
require.Nil(t, err)
}

cond := func() bool {
select {
case <-f.OnHealNotifierCh:
case <-onHealCh:
return true
default:
return false
Expand All @@ -81,35 +107,43 @@ func TestHealClient_Request(t *testing.T) {
}

func TestHealClient_MonitorClose(t *testing.T) {
f := &Fixture{
Request: &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "conn-1",
NetworkService: "ns-1",
},
eventCh := make(chan *networkservice.ConnectionEvent, 1)

onHealCh := make(chan struct{})
onHeal := &testOnHeal{
RequestFunc: func(ctx context.Context, in *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (connection *networkservice.Connection, e error) {
close(onHealCh)
return &networkservice.Connection{}, nil
},
}
err := SetupWithSingleRequest(f)
defer TearDown(f)

ctx, cancelFunc := context.WithCancel(context.Background())
client := chain.NewNetworkServiceClient(
heal.NewClient(ctx, eventchannel.NewMonitorConnectionClient(eventCh), addressof.NetworkServiceClient(onHeal)))

_, err := client.Request(context.Background(), &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "conn-1",
NetworkService: "ns-1",
},
})
require.Nil(t, err)
require.True(t, reflect.DeepEqual(f.Conn, f.Request.GetConnection()))

err = f.ServerStream.Send(&networkservice.ConnectionEvent{
eventCh <- &networkservice.ConnectionEvent{
Type: networkservice.ConnectionEventType_INITIAL_STATE_TRANSFER,
Connections: map[string]*networkservice.Connection{
"conn-1": {
Id: "conn-1",
NetworkService: "ns-1",
},
},
})
require.Nil(t, err)
}

f.CloseStream()
cancelFunc()

cond := func() bool {
select {
case <-f.OnHealNotifierCh:
case <-onHealCh:
return true
default:
return false
Expand All @@ -119,103 +153,78 @@ func TestHealClient_MonitorClose(t *testing.T) {
}

func TestHealClient_EmptyInit(t *testing.T) {
f := &Fixture{
Request: &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "conn-1",
NetworkService: "ns-1",
},
eventCh := make(chan *networkservice.ConnectionEvent, 1)

client := chain.NewNetworkServiceClient(
heal.NewClient(context.Background(), eventchannel.NewMonitorConnectionClient(eventCh), nil))

_, err := client.Request(context.Background(), &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "conn-1",
NetworkService: "ns-1",
},
}
err := SetupWithSingleRequest(f)
defer TearDown(f)
})
require.Nil(t, err)
require.True(t, reflect.DeepEqual(f.Conn, f.Request.GetConnection()))

err = f.ServerStream.Send(&networkservice.ConnectionEvent{
eventCh <- &networkservice.ConnectionEvent{
Type: networkservice.ConnectionEventType_INITIAL_STATE_TRANSFER,
Connections: make(map[string]*networkservice.Connection),
})
require.Nil(t, err)
}

err = f.ServerStream.Send(&networkservice.ConnectionEvent{
eventCh <- &networkservice.ConnectionEvent{
Type: networkservice.ConnectionEventType_UPDATE,
Connections: map[string]*networkservice.Connection{
"conn-1": {
Id: "conn-1",
NetworkService: "ns-1",
},
},
})
require.Nil(t, err)
}
}

func TestHealClient_SeveralConnection(t *testing.T) {
f := &Fixture{
Request: &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "conn-1",
NetworkService: "ns-1",
},
},
}
err := SetupWithSingleRequest(f)
defer TearDown(f)
require.Nil(t, err)
require.True(t, reflect.DeepEqual(f.Conn, f.Request.GetConnection()))
func TestNewClient_MissingConnectionsInInit(t *testing.T) {
eventCh := make(chan *networkservice.ConnectionEvent, 1)

r := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "conn-2",
NetworkService: "ns-2",
requestCh := make(chan *networkservice.NetworkServiceRequest)
onHeal := &testOnHeal{
RequestFunc: func(ctx context.Context, in *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (connection *networkservice.Connection, e error) {
requestCh <- in
return &networkservice.Connection{}, nil
},
}

conn, err := f.Client.Request(context.Background(), r)
require.Nil(t, err)
require.True(t, reflect.DeepEqual(conn, r.GetConnection()))

ctx, cancelFunc := context.WithTimeout(context.Background(), 100*time.Millisecond)
_, _, err = f.MockMonitorServer.Stream(ctx)
require.NotNil(t, err) // we expect healClient called MonitorConnections method once
cancelFunc()
}

func TestNewClient_MissingConnectionsInInit(t *testing.T) {
f := &Fixture{}
err := Setup(f)
defer TearDown(f)
require.Nil(t, err)
ctx, cancelFunc := context.WithCancel(context.Background())
client := chain.NewNetworkServiceClient(
heal.NewClient(ctx, eventchannel.NewMonitorConnectionClient(eventCh), addressof.NetworkServiceClient(onHeal)))

conns := []*networkservice.Connection{
{Id: "conn-1", NetworkService: "ns-1"},
{Id: "conn-2", NetworkService: "ns-2"},
}

conn, err := f.Client.Request(context.Background(), &networkservice.NetworkServiceRequest{Connection: conns[0]})
conn, err := client.Request(context.Background(), &networkservice.NetworkServiceRequest{Connection: conns[0]})
require.Nil(t, err)
require.True(t, reflect.DeepEqual(conn, conns[0]))

conn, err = f.Client.Request(context.Background(), &networkservice.NetworkServiceRequest{Connection: conns[1]})
conn, err = client.Request(context.Background(), &networkservice.NetworkServiceRequest{Connection: conns[1]})
require.Nil(t, err)
require.True(t, reflect.DeepEqual(conn, conns[1]))

err = f.ServerStream.Send(&networkservice.ConnectionEvent{
eventCh <- &networkservice.ConnectionEvent{
Type: networkservice.ConnectionEventType_INITIAL_STATE_TRANSFER,
Connections: map[string]*networkservice.Connection{conns[0].GetId(): conns[0]},
})
require.Nil(t, err)
}

// we emulate situation that server managed to handle only the first connection
// second connection should came in the UPDATE event, but we emulate server's falling down
f.CloseStream()
cancelFunc()
// at that point we expect that 'healClient' start healing both 'conn-1' and 'conn-2'

healedIDs := map[string]bool{}

cond := func() bool {
select {
case r := <-f.OnHealNotifierCh:
case r := <-requestCh:
if _, ok := healedIDs[r.GetConnection().GetId()]; !ok {
healedIDs[r.GetConnection().GetId()] = true
return true
Expand Down
Loading