Skip to content

Commit

Permalink
Fix metadata, add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
  • Loading branch information
Vladimir Popov committed Dec 3, 2020
1 parent 98deb86 commit 772dcd7
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 23 deletions.
8 changes: 6 additions & 2 deletions pkg/networkservice/utils/metadata/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ func NewClient() networkservice.NetworkServiceClient {
}

func (m *metaDataClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
ctx = store(ctx, request.GetConnection().GetId(), &m.Map)
connID := request.GetConnection().GetId()

ctx = store(ctx, connID, &m.Map)

conn, err := next.Client(ctx).Request(ctx, request, opts...)
if err != nil {
del(ctx, conn.GetId(), &m.Map)
del(ctx, connID, &m.Map)
return nil, err
}

return conn, nil
}

Expand Down
10 changes: 0 additions & 10 deletions pkg/networkservice/utils/metadata/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,3 @@ type metaData struct {
client sync.Map
server sync.Map
}

// LoadAndDelete deletes the value for a key, returning the previous value if any.
// The loaded result reports whether the key was present.
func (m *metaDataMap) LoadAndDelete(key string) (value *metaData, loaded bool) {
actual, loaded := (*sync.Map)(m).LoadAndDelete(key)
if actual == nil {
return nil, loaded
}
return actual.(*metaData), loaded
}
51 changes: 42 additions & 9 deletions pkg/networkservice/utils/metadata/meta_data_map.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

150 changes: 150 additions & 0 deletions pkg/networkservice/utils/metadata/meta_data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright (c) 2020 Doc.ai and/or its affiliates.
//
// 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 metadata_test

import (
"context"
"testing"

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

"github.com/networkservicemesh/sdk/pkg/networkservice/common/updatepath"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/adapters"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkcontext"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/inject/injecterror"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)

const (
testKey = "test"
)

func testServer(server networkservice.NetworkServiceServer) networkservice.NetworkServiceServer {
return next.NewNetworkServiceServer(
updatepath.NewServer("server"),
server,
)
}

func testRequest(conn *networkservice.Connection) *networkservice.NetworkServiceRequest {
return &networkservice.NetworkServiceRequest{
Connection: conn,
}
}

type sample struct {
name string
test func(t *testing.T, server networkservice.NetworkServiceServer, isClient bool)
}

var samples = []*sample{
{
name: "Request",
test: func(t *testing.T, server networkservice.NetworkServiceServer, isClient bool) {
var actual, expected map[string]string = nil, map[string]string{"a": "A"}

chainServer := next.NewNetworkServiceServer(
testServer(server),
checkcontext.NewServer(t, func(_ *testing.T, ctx context.Context) {
metadata.Map(ctx, isClient).Store(testKey, expected)
}),
)
conn, err := chainServer.Request(context.TODO(), testRequest(nil))
require.NoError(t, err)

chainServer = next.NewNetworkServiceServer(
testServer(server),
checkcontext.NewServer(t, func(_ *testing.T, ctx context.Context) {
if raw, ok := metadata.Map(ctx, isClient).Load(testKey); ok {
actual = raw.(map[string]string)
} else {
actual = nil
}
}),
)
_, err = chainServer.Request(context.TODO(), testRequest(conn))
require.NoError(t, err)

require.Equal(t, expected, actual)
},
},
{
name: "Request failed",
test: func(t *testing.T, server networkservice.NetworkServiceServer, _ bool) {
chainServer := next.NewNetworkServiceServer(
testServer(server),
injecterror.NewServer(errors.New("error")),
)
_, err := chainServer.Request(context.TODO(), testRequest(nil))
require.Error(t, err)
},
},
{
name: "Close",
test: func(t *testing.T, server networkservice.NetworkServiceServer, isClient bool) {
data := map[string]string{"a": "A"}

chainServer := next.NewNetworkServiceServer(
testServer(server),
checkcontext.NewServer(t, func(_ *testing.T, ctx context.Context) {
metadata.Map(ctx, isClient).Store(testKey, data)
}),
)
conn, err := chainServer.Request(context.TODO(), testRequest(nil))
require.NoError(t, err)

_, err = testServer(server).Close(context.TODO(), conn)
require.NoError(t, err)

chainServer = next.NewNetworkServiceServer(
testServer(server),
checkcontext.NewServer(t, func(_ *testing.T, ctx context.Context) {
if raw, ok := metadata.Map(ctx, isClient).Load(testKey); ok {
data = raw.(map[string]string)
} else {
data = nil
}
}),
)
_, err = chainServer.Request(context.TODO(), testRequest(conn))
require.NoError(t, err)

require.Nil(t, data)
},
},
}

func TestMetaDataServer(t *testing.T) {
for i := range samples {
sample := samples[i]
t.Run(sample.name, func(t *testing.T) {
sample.test(t, metadata.NewServer(), false)
})
}
}

func TestMetaDataClient(t *testing.T) {
for i := range samples {
sample := samples[i]
t.Run(sample.name, func(t *testing.T) {
sample.test(t, adapters.NewClientToServer(metadata.NewClient()), true)
})
}
}
9 changes: 7 additions & 2 deletions pkg/networkservice/utils/metadata/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ func NewServer() networkservice.NetworkServiceServer {
}

func (m *metadataServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
ctx = store(ctx, request.GetConnection().GetId(), &m.Map)
connID := request.GetConnection().GetId()

ctx = store(ctx, connID, &m.Map)

conn, err := next.Server(ctx).Request(ctx, request)
if err != nil {
del(ctx, conn.GetId(), &m.Map)
del(ctx, connID, &m.Map)
return nil, err
}

return conn, nil
}

Expand Down

0 comments on commit 772dcd7

Please sign in to comment.