diff --git a/core/coreutil/schedule_test.go b/core/coreutil/schedule_test.go index a4a3613b1..196f5a0d6 100644 --- a/core/coreutil/schedule_test.go +++ b/core/coreutil/schedule_test.go @@ -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 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) +} diff --git a/core/coreutil/waiter_test.go b/core/coreutil/waiter_test.go index d5629ecc2..75c5a1f56 100644 --- a/core/coreutil/waiter_test.go +++ b/core/coreutil/waiter_test.go @@ -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 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) +} diff --git a/core/engine/instance.go b/core/engine/instance.go index d271f8d63..8c36f3cdd 100644 --- a/core/engine/instance.go +++ b/core/engine/instance.go @@ -67,24 +67,18 @@ type instanceSharedDeps struct { // Run blocks until ammo finish, error or context cancel. // Expects, that gun is already bind. -func (i *instance) Run(ctx context.Context) error { - i.log.Debug("Instance started") - i.metrics.InstanceStart.Add(1) - defer func() { - i.metrics.InstanceFinish.Add(1) - i.log.Debug("Instance finished") - }() - - return i.shoot(ctx) -} - -func (i *instance) shoot(ctx context.Context) (err error) { +func (i *instance) Run(ctx context.Context) (recoverErr error) { defer func() { r := recover() if r != nil { - err = errors.Errorf("shoot panic: %s", r) + recoverErr = errors.Errorf("shoot panic: %s", r) } + + i.log.Debug("Instance finished") + i.metrics.InstanceFinish.Add(1) }() + i.log.Debug("Instance started") + i.metrics.InstanceStart.Add(1) waiter := coreutil.NewWaiter(i.schedule, ctx) // Checking, that schedule is not finished, required, to not consume extra ammo,