-
Notifications
You must be signed in to change notification settings - Fork 297
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ testImport: | |
subpackages: | ||
- assert | ||
- package: github.com/uber-go/atomic | ||
- package: github.com/cat2neat/gopeek | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ) t := heap.Pop(&m.timers).(*Timer) may be invoked before one of workers running job get into the point clock.Sleep . |
||
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() | ||
|
@@ -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. | ||
|
@@ -94,7 +103,6 @@ func (m *Mock) AfterFunc(d time.Duration, f func()) *Timer { | |
<-t.c | ||
f() | ||
}() | ||
nap() | ||
return t | ||
} | ||
|
||
|
@@ -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) } |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
@@ -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) { | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slight inconsistency here: comment says >= 1, but code says There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{}) { | ||
|
There was a problem hiding this comment.
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 thatglide.lock
is also updatedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.