Skip to content

Commit

Permalink
add unit tests for extended connection
Browse files Browse the repository at this point in the history
Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com>
  • Loading branch information
NikitaSkrynnik committed Nov 28, 2024
1 parent 3c189d6 commit 0c6d122
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions extendtimeout/connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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 extendtimeout_test

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.fd.io/govpp/api"

"github.com/networkservicemesh/vpphelper/extendtimeout"
)

type testConn struct {
api.Connection
invoke func(ctx context.Context)
}

func (c *testConn) Invoke(ctx context.Context, req, reply api.Message) error {
c.invoke(ctx)
return nil
}

func TestSmallTimeout(t *testing.T) {
testConn := &testConn{invoke: func(ctx context.Context) {
deadline, ok := ctx.Deadline()
require.True(t, ok)
timeout := time.Until(deadline)
require.Greater(t, timeout, time.Second)
}}

smallCtx, cancel := context.WithTimeout(context.Background(), time.Millisecond*10)
defer cancel()

err := extendtimeout.NewConnection(testConn, 2*time.Second).Invoke(smallCtx, nil, nil)
require.NoError(t, err)
}

func TestBigTimeout(t *testing.T) {
testConn := &testConn{invoke: func(ctx context.Context) {
deadline, ok := ctx.Deadline()
require.True(t, ok)
timeout := time.Until(deadline)
require.Greater(t, timeout, 7*time.Second)
}}

smallCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

err := extendtimeout.NewConnection(testConn, 2*time.Second).Invoke(smallCtx, nil, nil)
require.NoError(t, err)
}

0 comments on commit 0c6d122

Please sign in to comment.