Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
tests
  • Loading branch information
oke11o committed May 24, 2023
1 parent fe316d2 commit b16e289
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 91 deletions.
49 changes: 23 additions & 26 deletions core/coreutil/schedule_test.go
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)
}
103 changes: 51 additions & 52 deletions core/coreutil/waiter_test.go
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)
}
20 changes: 7 additions & 13 deletions core/engine/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b16e289

Please sign in to comment.