Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Solodkov <oleg.solodkov@xored.com>
  • Loading branch information
sol-0 committed Nov 29, 2021
1 parent 050a1f4 commit 1d4c030
Showing 1 changed file with 61 additions and 34 deletions.
95 changes: 61 additions & 34 deletions pkg/networkservice/common/mechanisms/recvfd/server_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
// Copyright (c) 2020-2021 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.

// +build linux

package recvfd_test

import (
"context"
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"path"
"runtime"
"syscall"
"testing"
"time"

"github.com/edwarnicke/grpcfd"
"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
Expand All @@ -22,15 +50,6 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/peer"
"io/ioutil"
"net"
"net/url"
"os"
"path"
"runtime"
"syscall"
"testing"
"time"
)

type checkFileClosed struct {
Expand Down Expand Up @@ -93,7 +112,7 @@ func (w *wrapTransceiver) RecvFileByURL(urlStr string) (<-chan *os.File, error)
return chanRes, nil
}

func createFile(fileName string, t *testing.T) (inodeUrlStr string, fileClosedContext context.Context, cancelFunc func()) {
func createFile(fileName string, t *testing.T) (inodeURLStr string, fileClosedContext context.Context, cancelFunc func()) {
f, err := os.Create(fileName)
require.NoError(t, err, "Failed to create and open a file: %v", err)

Expand All @@ -107,16 +126,16 @@ func createFile(fileName string, t *testing.T) (inodeUrlStr string, fileClosedCo
require.NoError(t, err, "Failed to close file: %v", err)

fileClosedContext, cancelFunc = context.WithCancel(context.Background())
inodeUrlStr = fmt.Sprintf("inode://%d/%d", stat.Dev, stat.Ino)
inodeURLStr = fmt.Sprintf("inode://%d/%d", stat.Dev, stat.Ino)

return
}

func createServerAndClient(t *testing.T, ctx context.Context, testServerChain *networkservice.NetworkServiceServer, serveUrl *url.URL) (testClient networkservice.NetworkServiceClient){
func createServerAndClient(ctx context.Context, t *testing.T, testServerChain *networkservice.NetworkServiceServer, serveURL *url.URL) (testClient networkservice.NetworkServiceClient) {
var grpcServer = grpc.NewServer(grpc.Creds(grpcfd.TransportCredentials(insecure.NewCredentials())))
networkservice.RegisterNetworkServiceServer(grpcServer, *testServerChain)

var errCh = grpcutils.ListenAndServe(ctx, serveUrl, grpcServer)
var errCh = grpcutils.ListenAndServe(ctx, serveURL, grpcServer)

select {
case e := <-errCh:
Expand All @@ -126,7 +145,7 @@ func createServerAndClient(t *testing.T, ctx context.Context, testServerChain *n

testClient = client.NewClient(
ctx,
client.WithClientURL(sandbox.CloneURL(serveUrl)),
client.WithClientURL(sandbox.CloneURL(serveURL)),
client.WithDialOptions(grpc.WithTransportCredentials(
grpcfd.TransportCredentials(insecure.NewCredentials())),
),
Expand All @@ -151,28 +170,28 @@ func TestRecvfdClosesSingleFile(t *testing.T) {
s, err := os.Create(path.Join(dir, "test.sock"))
require.NoError(t, err)

var testFileName = path.Join(dir, t.Name() + ".test")
var testFileName = path.Join(dir, t.Name()+".test")

inodeUrlStr, fileClosedContext, cancelFunc := createFile(testFileName, t)
inodeURLStr, fileClosedContext, cancelFunc := createFile(testFileName, t)

serveUrl := &url.URL{Scheme: "unix", Path: s.Name(), Host: "0.0.0.0:5000"}
serveURL := &url.URL{Scheme: "unix", Path: s.Name(), Host: "0.0.0.0:5000"}

var testChain = chain.NewNetworkServiceServer(
&checkFileClosed{
onFileClosed: map[string]func() {
inodeUrlStr: cancelFunc,
onFileClosed: map[string]func(){
inodeURLStr: cancelFunc,
},
},
recvfd.NewServer())

testClient := createServerAndClient(t, ctx, &testChain, serveUrl)
testClient := createServerAndClient(ctx, t, &testChain, serveURL)

request := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
{
Cls: cls.LOCAL,
Cls: cls.LOCAL,
Type: kernel.MECHANISM,
Parameters: map[string]string {
Parameters: map[string]string{
common.InodeURL: "file:" + testFileName,
},
},
Expand Down Expand Up @@ -218,40 +237,48 @@ func TestRecvfdClosesMultipleFiles(t *testing.T) {
for i := 0; i < numFiles; i++ {
filePath = path.Join(dir, fmt.Sprintf("%s.test%d", t.Name(), i))

inodeUrlStr, fileClosedContext, cancelFunc := createFile(filePath, t)
onFileClosedFuncs[inodeUrlStr] = cancelFunc
inodeURLStr, fileClosedContext, cancelFunc := createFile(filePath, t)
onFileClosedFuncs[inodeURLStr] = cancelFunc
fileClosedContexts[i] = fileClosedContext

request.MechanismPreferences = append(request.MechanismPreferences,
&networkservice.Mechanism{
Cls: cls.LOCAL,
Cls: cls.LOCAL,
Type: kernel.MECHANISM,
Parameters: map[string]string {
Parameters: map[string]string{
common.InodeURL: "file:" + filePath,
},
})
}

serveUrl := &url.URL{Scheme: "unix", Path: s.Name(), Host: "0.0.0.0:5000"}
serveURL := &url.URL{Scheme: "unix", Path: s.Name(), Host: "0.0.0.0:5000"}

var testChain = chain.NewNetworkServiceServer(
&checkFileClosed{
onFileClosed: onFileClosedFuncs,
},
recvfd.NewServer())

var testClient = createServerAndClient(t, ctx, &testChain, serveUrl)
var testClient = createServerAndClient(ctx, t, &testChain, serveURL)

conn, err := testClient.Request(ctx, request)
require.NoError(t, err)

_, err = testClient.Close(ctx, conn)
require.NoError(t, err)

for _, closedCtx := range fileClosedContexts {
require.Eventually(t, func() bool {
runtime.GC()
return closedCtx.Err() != nil
}, time.Second, time.Millisecond*100)
}
require.Eventually(t, func() bool {
runtime.GC()
return fileClosedContexts[0].Err() != nil
}, time.Second, time.Millisecond*100)

require.Eventually(t, func() bool {
runtime.GC()
return fileClosedContexts[1].Err() != nil
}, time.Second, time.Millisecond*100)

require.Eventually(t, func() bool {
runtime.GC()
return fileClosedContexts[2].Err() != nil
}, time.Second, time.Millisecond*100)
}

0 comments on commit 1d4c030

Please sign in to comment.