Skip to content

Commit

Permalink
tcase better test facilities
Browse files Browse the repository at this point in the history
add some throttlers cases
  • Loading branch information
1pkg committed Jul 26, 2020
1 parent ad04291 commit 8ce7515
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
8 changes: 0 additions & 8 deletions heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,24 @@ type blatheap struct {
lock sync.Mutex
}

// Len is the number of elements in the collection.
func (lh *blatheap) Len() int {
lh.lock.Lock()
defer lh.lock.Unlock()
return len(lh.buffer)
}

// Less reports whether the element with
// index i should sort before the element with index j.
func (lh *blatheap) Less(i int, j int) bool {
lh.lock.Lock()
defer lh.lock.Unlock()
return lh.buffer[i] < lh.buffer[j]
}

// Swap swaps the elements with indexes i and j.
func (lh *blatheap) Swap(i int, j int) {
lh.lock.Lock()
defer lh.lock.Unlock()
lh.buffer[i], lh.buffer[j] = lh.buffer[j], lh.buffer[i]
}

// Push add x as element Len().
func (lh *blatheap) Push(x interface{}) {
lh.lock.Lock()
defer lh.lock.Unlock()
Expand All @@ -38,7 +33,6 @@ func (lh *blatheap) Push(x interface{}) {
}
}

// Pop remove and return element Len() - 1.
func (lh *blatheap) Pop() interface{} {
lh.lock.Lock()
defer lh.lock.Unlock()
Expand All @@ -48,14 +42,12 @@ func (lh *blatheap) Pop() interface{} {
return val
}

// At returns element at position.
func (lh *blatheap) At(pos int) uint64 {
lh.lock.Lock()
defer lh.lock.Unlock()
return lh.buffer[pos]
}

// Prune cleans heap buffer.
func (lh *blatheap) Prune() {
lh.lock.Lock()
defer lh.lock.Unlock()
Expand Down
36 changes: 26 additions & 10 deletions throttlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,27 @@ type tcase struct {
durs []time.Duration
}

func (t tcase) run() (err error, dur time.Duration) {
defer func() {
if msg := recover(); msg != nil {
err = errors.New(msg.(string))
}
}()
func (t tcase) run(index int) (err error, dur time.Duration) {
ts := time.Now()
err = t.thr.Acquire(t.ctx)
// try catch panic into error
func() {
defer func() {
if msg := recover(); msg != nil {
err = errors.New(msg.(string))
}
}()
err = t.thr.Acquire(t.ctx)
}()
dur = time.Since(ts)
// run additional payload only if present
if t.act != nil {
_ = t.act(t.ctx)
}
if err := t.thr.Release(t.ctx); err != nil {
return err, dur
// imitate over releasing
for i := 0; i < index+1; i++ {
if err := t.thr.Release(t.ctx); err != nil {
return err, dur
}
}
return
}
Expand Down Expand Up @@ -122,6 +129,15 @@ func TestThrottlerPattern(t *testing.T) {
errors.New("throttler has reached chance threshold"),
},
},
"Throttler chance should throttle on >1": {
thr: NewThrottlerChance(10.10),
ctx: context.Background(),
errs: []error{
errors.New("throttler has reached chance threshold"),
errors.New("throttler has reached chance threshold"),
errors.New("throttler has reached chance threshold"),
},
},
"Throttler chance should not throttle on 0": {
thr: NewThrottlerChance(0),
ctx: context.Background(),
Expand Down Expand Up @@ -175,9 +191,9 @@ func TestThrottlerPattern(t *testing.T) {
for i := range tcase.errs {
t.Run(fmt.Sprintf("run %d", i+1), func(t *testing.T) {
t.Parallel()
err, dur := tcase.run()
index := int(atomic.AddInt64(&index, 1) - 1)
resErr, resDur := tcase.result(index)
err, dur := tcase.run(index)
assert.Equal(t, resErr, err)
assert.LessOrEqual(t, int64(resDur), int64(dur))
})
Expand Down

0 comments on commit 8ce7515

Please sign in to comment.