-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a timeout for Closes in begin.Server (#1650)
* fix corner cases of the begin chain element Signed-off-by: denis-tingaikin <denis.tingajkin@xored.com> Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> * disable Test_RestartDuringRefresh Signed-off-by: denis-tingaikin <denis.tingajkin@xored.com> Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> * add fresh context Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> * add extended context Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> * add refreshed close context everywhere in begin Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> * fix some unit tests Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> * unskip some tests Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> * fix golang linter issues Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> * debug Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> * cleanup Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> * fix race condition Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> * add unit tests Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> * fix go linter issues Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> * fix race conditiong Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> * apply review comments Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> --------- Signed-off-by: denis-tingaikin <denis.tingajkin@xored.com> Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com> Co-authored-by: denis-tingaikin <denis.tingajkin@xored.com>
- Loading branch information
1 parent
bc1d964
commit 3016313
Showing
5 changed files
with
164 additions
and
23 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) 2024 Cisco 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 begin_test | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/goleak" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/begin" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next" | ||
) | ||
|
||
const ( | ||
waitTime = time.Second | ||
) | ||
|
||
type waitServer struct { | ||
requestDone atomic.Int32 | ||
closeDone atomic.Int32 | ||
} | ||
|
||
func (s *waitServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) { | ||
time.Sleep(waitTime) | ||
s.requestDone.Store(1) | ||
return next.Server(ctx).Request(ctx, request) | ||
} | ||
|
||
func (s *waitServer) Close(ctx context.Context, connection *networkservice.Connection) (*empty.Empty, error) { | ||
time.Sleep(waitTime) | ||
s.closeDone.Store(1) | ||
return next.Server(ctx).Close(ctx, connection) | ||
} | ||
|
||
func TestBeginWorksWithSmallTimeout(t *testing.T) { | ||
t.Cleanup(func() { | ||
goleak.VerifyNone(t) | ||
}) | ||
requestCtx, cancel := context.WithTimeout(context.Background(), time.Millisecond*200) | ||
defer cancel() | ||
|
||
waitSrv := &waitServer{} | ||
server := next.NewNetworkServiceServer( | ||
begin.NewServer(), | ||
waitSrv, | ||
) | ||
|
||
request := testRequest("id") | ||
_, err := server.Request(requestCtx, request) | ||
require.EqualError(t, err, context.DeadlineExceeded.Error()) | ||
require.Equal(t, int32(0), waitSrv.requestDone.Load()) | ||
require.Eventually(t, func() bool { | ||
return waitSrv.requestDone.Load() == 1 | ||
}, waitTime*2, time.Millisecond*500) | ||
|
||
closeCtx, cancel := context.WithTimeout(context.Background(), time.Millisecond*200) | ||
defer cancel() | ||
_, err = server.Close(closeCtx, request.Connection) | ||
require.EqualError(t, err, context.DeadlineExceeded.Error()) | ||
require.Equal(t, int32(0), waitSrv.closeDone.Load()) | ||
require.Eventually(t, func() bool { | ||
return waitSrv.closeDone.Load() == 1 | ||
}, waitTime*2, time.Millisecond*500) | ||
} |