Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make onidle disable itself when timeout is set to 0 #929

Merged
merged 3 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/networkservice/common/onidle/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import (
"time"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/pkg/errors"

"github.com/networkservicemesh/api/pkg/api/networkservice"

"github.com/networkservicemesh/sdk/pkg/networkservice/common/null"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/clock"
)
Expand Down Expand Up @@ -60,6 +62,10 @@ func NewServer(ctx context.Context, notify func(), options ...Option) networkser
opt(t)
}

if t.timeout == 0 {
edwarnicke marked this conversation as resolved.
Show resolved Hide resolved
return null.NewServer()
}

t.timer = clockTime.AfterFunc(t.timeout, func() {
if ctx.Err() != nil {
return
Expand Down
22 changes: 21 additions & 1 deletion pkg/networkservice/common/onidle/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
"testing"
"time"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"go.uber.org/goleak"

"github.com/networkservicemesh/api/pkg/api/networkservice"

"github.com/networkservicemesh/sdk/pkg/networkservice/common/onidle"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/inject/injecterror"
Expand All @@ -38,6 +39,25 @@ const (
testTick = testWait / 100
)

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

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

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

var flag atomic.Bool

_ = onidle.NewServer(ctx, func() {
flag.Store(true)
}, onidle.WithTimeout(0))

clockMock.Add(time.Hour * 100)
require.Never(t, flag.Load, testWait, testTick)
}

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

Expand Down