Skip to content

Commit

Permalink
fix: Skip similar networkservices in netsvcmonitor element (#1524)
Browse files Browse the repository at this point in the history
* fix for issue sdk/1521

Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com>

* rerun CI

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

---------

Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com>
Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>
  • Loading branch information
NikitaSkrynnik authored Oct 5, 2023
1 parent f2a585e commit 7ad780b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/networkservice/common/netsvcmonitor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ func (m *monitorServer) Request(ctx context.Context, request *networkservice.Net
netsvcStreamIsAlive = false
break
}

// TODO: Remove this condition when https://github.com/networkservicemesh/api/issues/51 will be done
if netsvc.GetNetworkService().GetName() != conn.GetNetworkService() {
continue
}

nseStream, err := m.nseClient.Find(monitorCtx, &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: &registry.NetworkServiceEndpoint{
Name: conn.GetNetworkServiceEndpointName(),
Expand Down
93 changes: 93 additions & 0 deletions pkg/networkservice/common/netsvcmonitor/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) 2023 Cisco Systems, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package netsvcmonitor_test

import (
"context"
"fmt"
"testing"
"time"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/registry"
"github.com/stretchr/testify/require"

"github.com/networkservicemesh/sdk/pkg/networkservice/common/begin"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/netsvcmonitor"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/count"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
"github.com/networkservicemesh/sdk/pkg/registry/common/memory"
"github.com/networkservicemesh/sdk/pkg/registry/core/adapters"
)

func Test_Netsvcmonitor_And_GroupOfSimilarNetworkServices(t *testing.T) {
var testCtx, cancel = context.WithTimeout(context.Background(), time.Second)
defer cancel()

var nsServer = memory.NewNetworkServiceRegistryServer()
var nseServer = memory.NewNetworkServiceEndpointRegistryServer()
var counter count.Server

_, _ = nsServer.Register(context.Background(), &registry.NetworkService{
Name: "service-1",
})

_, _ = nseServer.Register(context.Background(), &registry.NetworkServiceEndpoint{
Name: "endpoint-1",
NetworkServiceNames: []string{"service-1"},
})

var server = chain.NewNetworkServiceServer(
metadata.NewServer(),
begin.NewServer(),
netsvcmonitor.NewServer(
testCtx,
adapters.NetworkServiceServerToClient(nsServer),
adapters.NetworkServiceEndpointServerToClient(nseServer),
),
&counter,
)

var request = &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "1",
NetworkService: "service-1",
NetworkServiceEndpointName: "endpoint-1",
},
}
var _, err = server.Request(testCtx, request)
require.NoError(t, err)
require.Equal(t, 0, counter.Closes())
for i := 0; i < 10; i++ {
_, err = nsServer.Register(context.Background(), &registry.NetworkService{
Name: fmt.Sprintf("service-1%v", i),
Matches: []*registry.Match{
{
SourceSelector: map[string]string{
"color": "red",
},
},
},
})
require.NoError(t, err)
}

require.Never(t, func() bool {
return counter.Closes() > 0
}, time.Millisecond*300, time.Millisecond*50)
}

0 comments on commit 7ad780b

Please sign in to comment.