-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests
- Loading branch information
Showing
3 changed files
with
81 additions
and
91 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,38 @@ | ||
// Copyright (c) 2018 Yandex LLC. All rights reserved. | ||
// Use of this source code is governed by a MPL 2.0 | ||
// license that can be found in the LICENSE file. | ||
// Author: Vladimir Skipor <[email protected]> | ||
|
||
package coreutil | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/stretchr/testify/require" | ||
"github.com/yandex/pandora/core/schedule" | ||
) | ||
|
||
var _ = Describe("callback on finish schedule", func() { | ||
It("callback once", func() { | ||
var callbackTimes int | ||
wrapped := schedule.NewOnce(1) | ||
testee := NewCallbackOnFinishSchedule(wrapped, func() { | ||
callbackTimes++ | ||
}) | ||
startAt := time.Now() | ||
testee.Start(startAt) | ||
tx, ok := testee.Next() | ||
Expect(ok).To(BeTrue()) | ||
Expect(tx).To(Equal(startAt)) | ||
Expect(callbackTimes).To(Equal(0)) | ||
func TestCallbackOnFinishSchedule(t *testing.T) { | ||
var callbackTimes int | ||
wrapped := schedule.NewOnce(1) | ||
testee := NewCallbackOnFinishSchedule(wrapped, func() { | ||
callbackTimes++ | ||
}) | ||
startAt := time.Now() | ||
testee.Start(startAt) | ||
tx, ok := testee.Next() | ||
|
||
tx, ok = testee.Next() | ||
Expect(ok).To(BeFalse()) | ||
Expect(tx).To(Equal(startAt)) | ||
Expect(callbackTimes).To(Equal(1)) | ||
require.True(t, ok) | ||
require.Equal(t, startAt, tx) | ||
require.Equal(t, 0, callbackTimes) | ||
|
||
tx, ok = testee.Next() | ||
Expect(ok).To(BeFalse()) | ||
Expect(tx).To(Equal(startAt)) | ||
Expect(callbackTimes).To(Equal(1)) | ||
}) | ||
tx, ok = testee.Next() | ||
require.False(t, ok) | ||
require.Equal(t, startAt, tx) | ||
require.Equal(t, 1, callbackTimes) | ||
|
||
}) | ||
tx, ok = testee.Next() | ||
require.False(t, ok) | ||
require.Equal(t, startAt, tx) | ||
require.Equal(t, 1, callbackTimes) | ||
} |
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 |
---|---|---|
@@ -1,70 +1,69 @@ | ||
// Copyright (c) 2018 Yandex LLC. All rights reserved. | ||
// Use of this source code is governed by a MPL 2.0 | ||
// license that can be found in the LICENSE file. | ||
// Author: Vladimir Skipor <[email protected]> | ||
|
||
package coreutil | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/stretchr/testify/require" | ||
"github.com/yandex/pandora/core/schedule" | ||
) | ||
|
||
var _ = Describe("waiter", func() { | ||
func TestWaiter_Unstarted(t *testing.T) { | ||
sched := schedule.NewOnce(1) | ||
ctx := context.Background() | ||
w := NewWaiter(sched, ctx) | ||
var i int | ||
for ; w.Wait(); i++ { | ||
} | ||
require.Equal(t, 1, i) | ||
} | ||
|
||
It("unstarted", func() { | ||
sched := schedule.NewOnce(1) | ||
ctx := context.Background() | ||
w := NewWaiter(sched, ctx) | ||
var i int | ||
for ; w.Wait(); i++ { | ||
} | ||
Expect(i).To(BeEquivalentTo(1)) | ||
}) | ||
func TestWaiter_WaitAsExpected(t *testing.T) { | ||
const ( | ||
duration = 100 * time.Millisecond | ||
ops = 100 | ||
times = ops * duration / time.Second | ||
) | ||
sched := schedule.NewConst(ops, duration) | ||
ctx := context.Background() | ||
w := NewWaiter(sched, ctx) | ||
start := time.Now() | ||
sched.Start(start) | ||
var i int | ||
for ; w.Wait(); i++ { | ||
} | ||
finish := time.Now() | ||
|
||
It("wait as expected", func() { | ||
const ( | ||
duration = 100 * time.Millisecond | ||
ops = 100 | ||
times = ops * duration / time.Second | ||
) | ||
sched := schedule.NewConst(ops, duration) | ||
ctx := context.Background() | ||
w := NewWaiter(sched, ctx) | ||
start := time.Now() | ||
sched.Start(start) | ||
var i int | ||
for ; w.Wait(); i++ { | ||
} | ||
finish := time.Now() | ||
Expect(i).To(BeEquivalentTo(times)) | ||
Expect(finish.Sub(start)).To(BeNumerically(">=", duration*(times-1)/times)) | ||
Expect(finish.Sub(start)).To(BeNumerically("<", 3*duration)) // Smaller interval will be more flaky. | ||
}) | ||
require.Equal(t, int(times), i) | ||
dur := finish.Sub(start) | ||
require.True(t, dur >= duration*(times-1)/times) | ||
require.True(t, dur < 3*duration) // Smaller interval will be more flaky. | ||
} | ||
func TestWaiter_ContextCanceledBeforeWait(t *testing.T) { | ||
sched := schedule.NewOnce(1) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
cancel() | ||
w := NewWaiter(sched, ctx) | ||
require.False(t, w.Wait()) | ||
} | ||
|
||
It("context canceled before wait", func() { | ||
sched := schedule.NewOnce(1) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
cancel() | ||
w := NewWaiter(sched, ctx) | ||
Expect(w.Wait()).To(BeFalse()) | ||
}) | ||
func TestWaiter_ContextCanceledDuringWait(t *testing.T) { | ||
sched := schedule.NewConstConf(schedule.ConstConfig{Ops: 0.1, Duration: 100 * time.Second}) | ||
timeout := 20 * time.Millisecond | ||
start := time.Now() | ||
ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
defer cancel() | ||
w := NewWaiter(sched, ctx) | ||
|
||
It("context canceled during wait", func() { | ||
sched := schedule.NewConstConf(schedule.ConstConfig{Ops: 0.1, Duration: 100 * time.Second}) | ||
timeout := 20 * time.Millisecond | ||
start := time.Now() | ||
ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
defer cancel() | ||
w := NewWaiter(sched, ctx) | ||
Expect(w.Wait()).To(BeTrue()) // 0 | ||
Expect(w.Wait()).To(BeFalse()) | ||
Expect(time.Since(start)).To(BeNumerically(">", timeout)) | ||
Expect(time.Since(start)).To(BeNumerically("<", 10*timeout)) | ||
}) | ||
require.True(t, w.Wait()) // 0 | ||
require.False(t, w.Wait()) | ||
|
||
}) | ||
since := time.Since(start) | ||
require.True(t, since > timeout) | ||
require.True(t, since < 10*timeout) | ||
} |
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