-
Notifications
You must be signed in to change notification settings - Fork 36
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
Fix metadata, add helper generator #612
Closed
Closed
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
772dcd7
Fix metadata, add tests
199182c
Add metadata helper generator
51d92ba
Rework metadata helper with genny
c1dc3b6
Move metadata/helper to tools/metadatahelper
ddf7cd1
Add scripts/genny.sh
0de98ae
Remove scripts/genny.sh
2354651
Rework metadata/helper
66baa06
Add metadata/maphelper
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Metadata helper | ||
|
||
Metadata helper is a [genny](https://github.com/cheekybits/genny) template to generate a helper class for the metadata | ||
usage. Template parameters are: | ||
* prefix - prefix for the metadata type and constructor; | ||
* valueType - stored type. | ||
Generated helpers can both be exported (**P**refix) and unexported (**p**refix). | ||
|
||
gen.go: | ||
```go | ||
package connect | ||
|
||
//go:generate bash -c "genny -in=$(go list -f '{{.Dir}} github.com/networkservicemesh/sdk/pkg/tools/metadatahelper)/meta_data.template.go -out=client_meta_data.gen.go -pkg=$GOPACKAGE gen 'prefix=client valueType=networkservice.NetworkServiceClient'" | ||
``` | ||
client_meta_data.gen.go: | ||
```go | ||
// This file was automatically generated by genny. | ||
// Any changes will be lost if this file is regenerated. | ||
// see https://github.com/cheekybits/genny | ||
|
||
package connect | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" | ||
) | ||
|
||
type _ClientKeyType struct{} | ||
|
||
type clientMetadataHelper struct { | ||
m *sync.Map | ||
} | ||
|
||
func clientMetadata(ctx context.Context, isClient bool) *clientMetadataHelper { | ||
return &clientMetadataHelper{ | ||
m: metadata.Map(ctx, isClient), | ||
} | ||
} | ||
|
||
func (h *clientMetadataHelper) Store(value networkservice.NetworkServiceClient) { | ||
h.m.Store(_ClientKeyType{}, value) | ||
} | ||
|
||
func (h *clientMetadataHelper) LoadOrStore(value networkservice.NetworkServiceClient) (networkservice.NetworkServiceClient, bool) { | ||
raw, ok := h.m.LoadOrStore(_ClientKeyType{}, value) | ||
return raw.(networkservice.NetworkServiceClient), ok | ||
} | ||
|
||
func (h *clientMetadataHelper) Load() (networkservice.NetworkServiceClient, bool) { | ||
if raw, ok := h.m.Load(_ClientKeyType{}); ok { | ||
return raw.(networkservice.NetworkServiceClient), true | ||
} | ||
return func() (v networkservice.NetworkServiceClient) { return }(), false | ||
} | ||
|
||
func (h *clientMetadataHelper) LoadAndDelete() (networkservice.NetworkServiceClient, bool) { | ||
if raw, ok := h.m.LoadAndDelete(_ClientKeyType{}); ok { | ||
return raw.(networkservice.NetworkServiceClient), true | ||
} | ||
return func() (v networkservice.NetworkServiceClient) { return }(), false | ||
} | ||
|
||
func (h *clientMetadataHelper) Delete() { | ||
h.m.Delete(_ClientKeyType{}) | ||
} | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although... it should always be true that the conn..GetId() below is the same as the request.GetConnection().GetId() ... its good defensive coding what you've done here, but an issue here would have been a sign of a bug in updatetoken ...