Skip to content

Commit

Permalink
Add clockmock sandbox test
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
  • Loading branch information
Vladimir Popov committed May 18, 2021
1 parent 70e202c commit b63ed34
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/tools/clockmock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
66 changes: 66 additions & 0 deletions pkg/tools/clockmock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ import (
"testing"
"time"

"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/tools/clock"
"github.com/networkservicemesh/sdk/pkg/tools/clockmock"
"github.com/networkservicemesh/sdk/pkg/tools/sandbox"
)

const (
Expand Down Expand Up @@ -505,3 +511,63 @@ 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()

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

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

nsReg, err := domain.Nodes[0].NSRegistryClient.Register(ctx, &registry.NetworkService{
Name: "ns",
})
require.NoError(t, err)

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

domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken)

nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)

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

requestCtx, requestCancel := m.WithTimeout(ctx, time.Second)
defer requestCancel()

conn, err := nsc.Request(requestCtx, request.Clone())
require.NoError(t, err)

// Simulate refresh from client.

refreshRequest := request.Clone()
refreshRequest.Connection = conn.Clone()

conn, err = nsc.Request(requestCtx, refreshRequest)
require.NoError(t, err)

// Close.

_, err = nsc.Close(requestCtx, conn)
require.NoError(t, err)
}

0 comments on commit b63ed34

Please sign in to comment.