Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clockmock sandbox test
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
Vladimir Popov committed Jun 23, 2021
1 parent 2ea9a61 commit c5b30d7
Showing 3 changed files with 104 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/registry/common/refresh/nse_registry_client_test.go
Original file line number Diff line number Diff line change
@@ -95,10 +95,10 @@ func Test_RefreshNSEClient_CalledRegisterTwice(t *testing.T) {
countClient,
)

reg, err := client.Register(context.Background(), testNSE(clockMock))
reg, err := client.Register(ctx, testNSE(clockMock))
require.NoError(t, err)

reg, err = client.Register(context.Background(), reg)
reg, err = client.Register(ctx, reg)
require.NoError(t, err)

clockMock.Add(expireTimeout)
1 change: 1 addition & 0 deletions pkg/tools/clockmock/mock.go
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@ func New(ctx context.Context) *Mock {
speedCh: make(chan float64),
mock: libclock.NewMock(),
}
m.Set(time.Now())

var speed float64
var realStart, mockStart, mockTime = time.Now(), m.Now(), m.Now()
101 changes: 101 additions & 0 deletions pkg/tools/clockmock/mock_test.go
Original file line number Diff line number Diff line change
@@ -22,10 +22,18 @@ import (
"testing"
"time"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/cls"
kernelmech "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel"
"github.com/networkservicemesh/api/pkg/api/registry"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/clock"
"github.com/networkservicemesh/sdk/pkg/tools/clockmock"
"github.com/networkservicemesh/sdk/pkg/tools/sandbox"
)

const (
@@ -505,3 +513,96 @@ func TestMock_WithTimeout(t *testing.T) {
require.FailNow(t, "too late")
}
}

func TestMock_Sandbox(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

deadline, _ := ctx.Deadline()

m := clockmock.New(ctx)
ctx = clock.WithClock(ctx, m)

domain := sandbox.NewBuilder(ctx, t).
SetNodesCount(1).
SetNSMgrProxySupplier(nil).
SetRegistryProxySupplier(nil).
SetTokenGeneratorSupplier(sandbox.GenerateTestToken).
Build()

nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken)

nsReg, err := nsRegistryClient.Register(ctx, &registry.NetworkService{
Name: "ns",
})
require.NoError(t, err)

nseReg := &registry.NetworkServiceEndpoint{
Name: "nse",
NetworkServiceNames: []string{nsReg.Name},
}

counter := new(counterServer)
domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, counter)

const tokenTimeout = time.Hour

nscCtx, nscCancel := context.WithCancel(ctx)
nsc := domain.Nodes[0].NewClient(nscCtx, sandbox.GenerateExpiringToken(tokenTimeout))

request := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
{Cls: cls.LOCAL, Type: kernelmech.MECHANISM},
},
Connection: &networkservice.Connection{
Id: "1",
NetworkService: nsReg.Name,
Context: &networkservice.ConnectionContext{},
},
}

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
require.Equal(t, int32(1), atomic.LoadInt32(&counter.requests))

// 1. Simulate refresh from client
refreshRequest := request.Clone()
refreshRequest.Connection = conn.Clone()

_, err = nsc.Request(ctx, refreshRequest)
require.NoError(t, err)
require.Equal(t, int32(2), atomic.LoadInt32(&counter.requests))

// 2. Wait for refresh from client
m.Add(tokenTimeout / 5)
require.Eventually(t, func() bool {
return atomic.LoadInt32(&counter.requests) >= 3
}, time.Until(deadline), testTick)

// 3. Wait for timeout
nscCancel()
time.Sleep(testWait)

m.Add(tokenTimeout)
require.Eventually(t, func() bool {
return atomic.LoadInt32(&counter.closes) >= 1
}, time.Until(deadline), testTick)
}

type counterServer struct {
requests, closes int32
}

func (c *counterServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
atomic.AddInt32(&c.requests, 1)

return next.Server(ctx).Request(ctx, request)
}

func (c *counterServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
atomic.AddInt32(&c.closes, 1)

return next.Server(ctx).Close(ctx, conn)
}

0 comments on commit c5b30d7

Please sign in to comment.