Skip to content

Commit

Permalink
fix unstable tests
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Glazychev <[email protected]>
  • Loading branch information
glazychev-art committed Sep 25, 2023
1 parent dbbea17 commit ba512e4
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 149 deletions.
5 changes: 5 additions & 0 deletions pkg/networkservice/chains/nsmgr/select_forwarder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,17 @@ func Test_DiscoverForwarder_Should_KeepSelectedForwarderWhileConnectionIsFine(t
conn, err = nsc.Request(ctx, request.Clone())
require.NoError(t, err)
require.Equal(t, 1, counter.UniqueRequests())
require.Equal(t, 3, counter.Requests())
require.Equal(t, 0, counter.Closes())
require.Equal(t, selectedForwarder, conn.GetPath().GetPathSegments()[2].Name)

// datapath is down
livenessValue.Store(false)
domain.Nodes[0].Forwarders[selectedForwarder].Cancel()
// waiting for healing
require.Eventually(t, func() bool {
return counter.Requests() >= 4
}, timeout, tick)

request.Connection = conn
conn, err = nsc.Request(ctx, request.Clone())
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkservice/core/trace/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (t *traceClient) Close(ctx context.Context, conn *networkservice.Connection
case logrus.TraceLevel:
return t.verbose.Close(ctx, conn, opts...)
case logrus.InfoLevel, logrus.DebugLevel:
return t.verbose.Close(ctx, conn, opts...)
return t.concise.Close(ctx, conn, opts...)
}
return next.Client(ctx).Close(ctx, conn, opts...)
}
8 changes: 4 additions & 4 deletions pkg/registry/core/chain/ns_registry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020 Cisco Systems, Inc.
// Copyright (c) 2020-2023 Cisco Systems, Inc.
//
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
// Copyright (c) 2020-2023 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -27,10 +27,10 @@ import (

// NewNetworkServiceRegistryServer - creates a chain of servers
func NewNetworkServiceRegistryServer(servers ...registry.NetworkServiceRegistryServer) registry.NetworkServiceRegistryServer {
return next.NewNetworkServiceRegistryServer(next.NewWrappedNetworkServiceRegistryServer(trace.NewNetworkServiceRegistryServer, servers...))
return next.NewWrappedNetworkServiceRegistryServer(trace.NewNetworkServiceRegistryServer, servers...)
}

// NewNetworkServiceRegistryClient - creates a chain of clients
func NewNetworkServiceRegistryClient(clients ...registry.NetworkServiceRegistryClient) registry.NetworkServiceRegistryClient {
return next.NewNetworkServiceRegistryClient(next.NewWrappedNetworkServiceRegistryClient(trace.NewNetworkServiceRegistryClient, clients...))
return next.NewWrappedNetworkServiceRegistryClient(trace.NewNetworkServiceRegistryClient, clients...)
}
6 changes: 3 additions & 3 deletions pkg/registry/core/chain/nse_registry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020 Cisco Systems, Inc.
// Copyright (c) 2020-2023 Cisco Systems, Inc.
//
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
// Copyright (c) 2020-2023 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -33,5 +33,5 @@ func NewNetworkServiceEndpointRegistryServer(servers ...registry.NetworkServiceE

// NewNetworkServiceEndpointRegistryClient - creates a chain of clients
func NewNetworkServiceEndpointRegistryClient(clients ...registry.NetworkServiceEndpointRegistryClient) registry.NetworkServiceEndpointRegistryClient {
return next.NewNetworkServiceEndpointRegistryClient(next.NewWrappedNetworkServiceEndpointRegistryClient(trace.NewNetworkServiceEndpointRegistryClient, clients...))
return next.NewWrappedNetworkServiceEndpointRegistryClient(trace.NewNetworkServiceEndpointRegistryClient, clients...)
}
138 changes: 0 additions & 138 deletions pkg/registry/core/trace/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,148 +18,10 @@
package testutil

import (
"context"
"fmt"
"strings"

"github.com/pkg/errors"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/cls"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
)

// NewConnection - create connection for testing
func NewConnection() *networkservice.NetworkServiceRequest {
return &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "conn-1",
Context: &networkservice.ConnectionContext{
IpContext: &networkservice.IPContext{
SrcIpRequired: true,
},
},
},
MechanismPreferences: []*networkservice.Mechanism{
{
Type: "KERNEL",
Cls: cls.LOCAL,
},
{
Type: "KERNEL",
Cls: cls.LOCAL,
Parameters: map[string]string{
"label": "v2",
},
},
},
}
}

// LabelChangerFirstServer - common server for testing
type LabelChangerFirstServer struct{}

// Request - test request
func (c *LabelChangerFirstServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
request.Connection.Labels = map[string]string{
"Label": "A",
}
rv, err := next.Server(ctx).Request(ctx, request)
if err != nil {
return nil, err
}
rv.Labels = map[string]string{
"Label": "D",
}
return rv, err
}

// Close - test request
func (c *LabelChangerFirstServer) Close(ctx context.Context, connection *networkservice.Connection) (*empty.Empty, error) {
connection.Labels = map[string]string{
"Label": "W",
}
r, err := next.Server(ctx).Close(ctx, connection)
connection.Labels = map[string]string{
"Label": "Z",
}
return r, err
}

// LabelChangerSecondServer - common server for testing
type LabelChangerSecondServer struct{}

// Request - test request
func (c *LabelChangerSecondServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
request.Connection.Labels = map[string]string{
"Label": "B",
}
rv, err := next.Server(ctx).Request(ctx, request)
if err != nil {
return nil, err
}
rv.Labels = map[string]string{
"Label": "C",
}
return rv, err
}

// Close - test request
func (c *LabelChangerSecondServer) Close(ctx context.Context, connection *networkservice.Connection) (*empty.Empty, error) {
connection.Labels = map[string]string{
"Label": "X",
}
r, err := next.Server(ctx).Close(ctx, connection)
connection.Labels = map[string]string{
"Label": "Y",
}
return r, err
}

// CustomError - custom error for testing
type CustomError struct{}

// Error - error message example
func (*CustomError) Error() string {
return `Error returned from api/pkg/api/networkservice/networkServiceClient.Close
github.com/networkservicemesh/sdk/pkg/networkservice/core/trace.(*beginTraceClient).Close
/root/go/pkg/mod/github.com/networkservicemesh/[email protected]/pkg/networkservice/core/trace/client.go:85
github.com/networkservicemesh/sdk/pkg/networkservice/core/next.(*nextClient).Close
/root/go/pkg/mod/github.com/networkservicemesh/[email protected]/pkg/networkservice/core/next/client.go:65
github.com/networkservicemesh/sdk/pkg/networkservice/core/next.(*nextClient).Close
/root/go/pkg/mod/github.com/networkservicemesh/[email protected]/pkg/networkservice/core/next/client.go:65
github.com/networkservicemesh/sdk/pkg/networkservice/core/trace.(*endTraceClient).Close
/root/go/pkg/mod/github.com/networkservicemesh/[email protected]/pkg/networkservice/core/trace/client.go:106
github.com/networkservicemesh/sdk/pkg/networkservice/core/next.(*nextClient).Close
/root/go/pkg/mod/github.com/networkservicemesh/[email protected]/pkg/networkservice/core/next/client.go:65`
}

// StackTrace - testing errors utility
func (*CustomError) StackTrace() errors.StackTrace {
return []errors.Frame{}
}

// ErrorServer - error server for testing
type ErrorServer struct{}

// Request - test request
func (c *ErrorServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
request.Connection.Labels = map[string]string{
"Label": "B",
}

_, _ = next.Server(ctx).Request(ctx, request)

return nil, &CustomError{}
}

// Close - test request
func (c *ErrorServer) Close(ctx context.Context, connection *networkservice.Connection) (*empty.Empty, error) {
return next.Server(ctx).Close(ctx, connection)
}

// TrimLogTime - to format logs
func TrimLogTime(buff fmt.Stringer) string {
// Logger created by the trace chain element uses custom formatter, which prints date and time info in each line
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/core/trace/traceconcise/traceconcise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import (
"github.com/sirupsen/logrus"
"go.uber.org/goleak"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/trace/testutil"
"github.com/networkservicemesh/sdk/pkg/registry/common/memory"
"github.com/networkservicemesh/sdk/pkg/registry/common/null"
"github.com/networkservicemesh/sdk/pkg/registry/core/chain"
"github.com/networkservicemesh/sdk/pkg/registry/core/streamchannel"
"github.com/networkservicemesh/sdk/pkg/registry/core/trace/testutil"
"github.com/networkservicemesh/sdk/pkg/registry/utils/inject/injecterror"
"github.com/networkservicemesh/sdk/pkg/tools/log"

Expand Down
4 changes: 2 additions & 2 deletions pkg/tools/dnsutils/fanout/handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Cisco Systems, Inc.
// Copyright (c) 2022-2023 Cisco Systems, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -66,7 +66,7 @@ func (h *fanoutHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, msg
address += fmt.Sprintf(":%d", h.dnsPort)
}

var resp, _, err = client.Exchange(msg, address)
var resp, _, err = client.ExchangeContext(ctx, msg, address)
if err != nil {
log.FromContext(ctx).WithField("fanoutHandler", "ServeDNS").Warnf("got an error during exchanging with address %v: %v", address, err.Error())
responseCh <- nil
Expand Down

0 comments on commit ba512e4

Please sign in to comment.