-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make eventually helper and timecop time travel to improve chances for…
… background goroutines
- Loading branch information
Showing
6 changed files
with
94 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package rth | ||
|
||
import ( | ||
"runtime" | ||
"time" | ||
) | ||
|
||
func Schedule(maxWait time.Duration) { | ||
const WaitUnit = time.Nanosecond | ||
var ( | ||
goroutNum = runtime.NumGoroutine() | ||
startedAt = time.Now() | ||
) | ||
for i := 0; i < goroutNum; i++ { // since goroutines don't have guarantee when they will be scheduled | ||
runtime.Gosched() // we explicitly mark that we are okay with other goroutines to be scheduled | ||
elapsed := time.Since(startedAt) | ||
if maxWait <= elapsed { // if max wait time is reached | ||
return | ||
} | ||
if elapsed < maxWait { // if we withint the max wait time, | ||
time.Sleep(WaitUnit) // then we could just yield CPU too with sleep | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package rth_test | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"go.llib.dev/testcase/assert" | ||
"go.llib.dev/testcase/internal/rth" | ||
"go.llib.dev/testcase/random" | ||
) | ||
|
||
func TestSchedule(t *testing.T) { | ||
rnd := random.New(random.CryptoSeed{}) | ||
done := make(chan struct{}) | ||
defer close(done) | ||
|
||
for i := 0; i < 1000; i++ { | ||
// fire goroutines | ||
go func() { | ||
for { | ||
select { | ||
case <-done: // stop when the program is done | ||
return | ||
default: // do something that requires CPU time | ||
strconv.Itoa(rnd.Int()) | ||
} | ||
} | ||
}() | ||
} | ||
|
||
var adjusted = func(d time.Duration, m float64) time.Duration { | ||
return time.Duration(float64(d) * m) | ||
} | ||
|
||
for _, dur := range []time.Duration{time.Second, time.Millisecond} { | ||
assert.Within(t, adjusted(dur, 1.2), func(ctx context.Context) { | ||
rth.Schedule(dur) | ||
}) | ||
} | ||
} |