Skip to content

Commit

Permalink
over releasing test fix
Browse files Browse the repository at this point in the history
fix running and priority tests
slim test matrix
  • Loading branch information
1pkg committed Oct 10, 2020
1 parent d1c0a67 commit 9a81c29
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
strategy:
matrix:
go-version: [1.15.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: setup
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
strategy:
matrix:
go-version: [1.15.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: setup
Expand Down
2 changes: 2 additions & 0 deletions throttlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,15 @@ func NewThrottlerRunning(threshold uint64) *trunning {
}

func (thr *trunning) Acquire(context.Context) error {
fmt.Println("Acquire")
if running := atomicBIncr(&thr.running); running > thr.threshold {
return errors.New("throttler has exceed running threshold")
}
return nil
}

func (thr *trunning) Release(context.Context) error {
fmt.Println("Release")
atomicBDecr(&thr.running)
return nil
}
Expand Down
48 changes: 28 additions & 20 deletions throttlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ import (
)

const (
ms0_0 time.Duration = 0
ms0_9 time.Duration = time.Duration(0.9 * float64(time.Millisecond))
ms1_0 time.Duration = time.Millisecond
ms2_0 time.Duration = 2 * time.Millisecond
ms3_0 time.Duration = 3 * time.Millisecond
ms4_0 time.Duration = 4 * time.Millisecond
ms5_0 time.Duration = 5 * time.Millisecond
ms7_0 time.Duration = 7 * time.Millisecond
ms9_0 time.Duration = 9 * time.Millisecond
ms0_0 time.Duration = 0
ms0_9 time.Duration = time.Duration(0.9 * float64(1*time.Millisecond))
ms1_0 time.Duration = 1 * time.Millisecond
ms2_0 time.Duration = 2 * time.Millisecond
ms3_0 time.Duration = 3 * time.Millisecond
ms4_0 time.Duration = 4 * time.Millisecond
ms5_0 time.Duration = 5 * time.Millisecond
ms7_0 time.Duration = 7 * time.Millisecond
ms9_0 time.Duration = 9 * time.Millisecond
ms100_0 time.Duration = 100 * time.Millisecond
ms200_0 time.Duration = 200 * time.Millisecond
ms300_0 time.Duration = 300 * time.Millisecond
)

type tcase struct {
Expand All @@ -37,6 +40,7 @@ type tcase struct {
}

func (t *tcase) run(index int) (err error, dur time.Duration) {
fmt.Println("INDEX", index)
// get context with fallback
ctx := context.Background()
if index < len(t.ctxs) {
Expand All @@ -51,9 +55,9 @@ func (t *tcase) run(index int) (err error, dur time.Duration) {
var ts time.Time
// try catch panic into error
func() {
defer atomicIncr(&t.idx)
defer func() {
if msg := recover(); msg != nil {
atomicIncr(&t.idx)
err = errors.New(msg.(string))
}
}()
Expand All @@ -67,17 +71,17 @@ func (t *tcase) run(index int) (err error, dur time.Duration) {
ctx = WithTimestamp(ctx, time.Now().Add(t.tss[index]))
}
err = t.thr.Acquire(ctx)
atomicIncr(&t.idx)
}()
dur = time.Since(ts)
// run additional action only if present
if index < len(t.acts) {
if act := t.acts[index]; act != nil {
fmt.Println("ACT")
_ = act(ctx)
}
}
limit := 1
if t.over { // imitate over releasing
if t.over && uint64(index+1) == t.tms { // imitate over releasing on last call
limit = index + 1
}
for i := 0; i < limit; i++ {
Expand Down Expand Up @@ -220,9 +224,9 @@ func TestThrottlers(t *testing.T) {
tms: 3,
thr: NewThrottlerRunning(1),
acts: []Runnable{
delayed(ms3_0, nope),
delayed(ms3_0, nope),
delayed(ms3_0, nope),
delayed(ms100_0, nope),
delayed(ms100_0, nope),
delayed(ms100_0, nope),
},
errs: []error{
nil,
Expand Down Expand Up @@ -265,9 +269,13 @@ func TestThrottlers(t *testing.T) {
tms: 7,
thr: NewThrottlerPriority(5, 2),
acts: []Runnable{
delayed(ms1_0, nope),
delayed(ms1_0, nope),
delayed(ms1_0, nope),
delayed(ms300_0, nope),
delayed(ms300_0, nope),
delayed(ms300_0, nope),
delayed(ms300_0, nope),
delayed(ms300_0, nope),
delayed(ms300_0, nope),
delayed(ms300_0, nope),
},
ctxs: []context.Context{
WithPriority(context.Background(), 1),
Expand All @@ -281,11 +289,11 @@ func TestThrottlers(t *testing.T) {
durs: []time.Duration{
0,
0,
ms0_9,
ms200_0,
0,
0,
0,
ms0_9,
ms200_0,
},
},
"Throttler timed should throttle after threshold": {
Expand Down

0 comments on commit 9a81c29

Please sign in to comment.