Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of nap with gopeek #8

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ testImport:
subpackages:
- assert
- package: github.com/uber-go/atomic
- package: github.com/cat2neat/gopeek
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also do glide up so that glide.lock is also updated

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

20 changes: 12 additions & 8 deletions internal/clock/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ package clock

import (
"container/heap"
"fmt"
"os"
"sync"
"time"

"github.com/cat2neat/gopeek"
)

// Mock represents a mock clock that only moves forward programmically.
Expand All @@ -44,12 +48,19 @@ func NewMock() *Mock {

// Add moves the current time of the mock clock forward by the duration.
// This should only be called from a single goroutine at a time.
func (m *Mock) Add(d time.Duration) {
func (m *Mock) Add(d time.Duration, condition *gopeek.Condition) {
m.Lock()
// Calculate the final time.
end := m.now.Add(d)

for len(m.timers) > 0 && m.now.Before(end) {
// Wait before popping from timers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why we need to wait before popping timers? I would expect a wait after popping a timer.

Copy link
Author

@cat2neat cat2neat Dec 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without nap ( Strictly speaking the current test can fail even with nap if a gorotine executing test wake up before workers start to run. actually I've faced sometimes while testing )
there is a possibility that

t := heap.Pop(&m.timers).(*Timer)

may be invoked before one of workers running job get into the point clock.Sleep .
in this case, as there are still only timers registered by clock.AfterFunc , test will fail.

m.Unlock()
_, err := condition.Wait(time.Second)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %+v\n", err)
}
m.Lock()
t := heap.Pop(&m.timers).(*Timer)
m.now = t.next
m.Unlock()
Expand All @@ -58,8 +69,6 @@ func (m *Mock) Add(d time.Duration) {
}

m.Unlock()
// Give a small buffer to make sure the other goroutines get handled.
nap()
}

// Timer produces a timer that will emit a time some duration after now.
Expand Down Expand Up @@ -94,7 +103,6 @@ func (m *Mock) AfterFunc(d time.Duration, f func()) *Timer {
<-t.c
f()
}()
nap()
return t
}

Expand Down Expand Up @@ -126,8 +134,4 @@ func (t *Timer) Tick() {
case t.c <- t.next:
default:
}
nap()
}

// Sleep momentarily so that other goroutines can process.
func nap() { time.Sleep(1 * time.Millisecond) }
21 changes: 17 additions & 4 deletions ratelimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go.uber.org/ratelimit"
"go.uber.org/ratelimit/internal/clock"

"github.com/cat2neat/gopeek"
"github.com/stretchr/testify/assert"
"github.com/uber-go/atomic"
)
Expand Down Expand Up @@ -79,9 +80,14 @@ func TestRateLimiter(t *testing.T) {
wg.Done()
})

clock.Add(4 * time.Second)
condition := gopeek.NewCondition().
CreatedBy("ratelimit_test.TestRateLimiter").
Is(gopeek.StateWaitingChannel).
// go ahead when there is only one waiting channel via Mock.Sleep
// as it's evidence that ratelimit occurred
EQ(1)

clock.Add(5 * time.Second)
clock.Add(4*time.Second, condition)
}

func TestDelayedRateLimiter(t *testing.T) {
Expand Down Expand Up @@ -123,11 +129,18 @@ func TestDelayedRateLimiter(t *testing.T) {
})

clock.AfterFunc(30*time.Second, func() {
assert.InDelta(t, 1200, count.Load(), 10, "count within rate limit")
// 11 is more accurate as a slow job also may count up before this func.
assert.InDelta(t, 1200, count.Load(), 11, "count within rate limit")
wg.Done()
})

clock.Add(40 * time.Second)
condition := gopeek.NewCondition().
CreatedBy("ratelimit_test.TestDelayedRateLimiter").
Is(gopeek.StateWaitingChannel).
// go ahead when there is >= 1 waiting channel via Mock.Sleep
// as it's evidence that ratelimit occurred
GT(0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slight inconsistency here: comment says >= 1, but code says GT(0)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

clock.Add(40*time.Second, condition)
}

func job(rl ratelimit.Limiter, count *atomic.Int32, done <-chan struct{}) {
Expand Down