-
Notifications
You must be signed in to change notification settings - Fork 20
Add Time Exercise: 41_time_delay (#83) #159
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,90 @@ | ||
| package timedelay | ||
|
|
||
| // Real implementations using time.Now, time.Add and comparisons for the exercise. | ||
|
|
||
| import ( | ||
| "errors" | ||
| "time" | ||
| ) | ||
|
|
||
| // WaitFor pauses execution until roughly ms milliseconds have passed. | ||
| func WaitFor(ms int) { | ||
| target := time.Now().Add(time.Duration(ms) * time.Millisecond) | ||
| for time.Now().Before(target) { | ||
| time.Sleep(1 * time.Millisecond) | ||
| } | ||
| } | ||
|
|
||
| // NotifyAfter returns a channel that receives true after roughly ms milliseconds. | ||
| func NotifyAfter(ms int) chan bool { | ||
| ch := make(chan bool, 1) | ||
| target := time.Now().Add(time.Duration(ms) * time.Millisecond) | ||
|
|
||
| go func() { | ||
| for time.Now().Before(target) { | ||
| time.Sleep(1 * time.Millisecond) | ||
| } | ||
| ch <- true | ||
| }() | ||
|
|
||
| return ch | ||
| } | ||
|
|
||
| // WaitUntil blocks until the provided target time is reached (returns immediately if in the past). | ||
| func WaitUntil(target time.Time) { | ||
| for time.Now().Before(target) { | ||
| time.Sleep(1 * time.Millisecond) | ||
| } | ||
| } | ||
|
|
||
| // NotifyAt returns a channel that receives true when target time is reached (sends immediately if in the past). | ||
| func NotifyAt(target time.Time) chan bool { | ||
| ch := make(chan bool, 1) | ||
|
|
||
| go func() { | ||
| for time.Now().Before(target) { | ||
| time.Sleep(1 * time.Millisecond) | ||
| } | ||
| ch <- true | ||
| }() | ||
|
|
||
| return ch | ||
| } | ||
|
|
||
| // ElapsedMillis returns how many milliseconds have elapsed since t. | ||
| func ElapsedMillis(t time.Time) int64 { | ||
| return time.Since(t).Milliseconds() | ||
| } | ||
|
|
||
| // WaitForOrTimeout waits up to ms milliseconds but fails if total waiting exceeds timeoutMs. | ||
| func WaitForOrTimeout(ms int, timeoutMs int) error { | ||
| target := time.Now().Add(time.Duration(ms) * time.Millisecond) | ||
| deadline := time.Now().Add(time.Duration(timeoutMs) * time.Millisecond) | ||
|
|
||
| for { | ||
| now := time.Now() | ||
| if !now.Before(target) { | ||
| return nil | ||
| } | ||
| if !now.Before(deadline) { | ||
| return errors.New("timeout exceeded before target reached") | ||
| } | ||
| time.Sleep(1 * time.Millisecond) | ||
| } | ||
| } | ||
|
|
||
| // ScheduleAfter runs fn after roughly ms milliseconds and returns a channel closed when fn finishes. | ||
| func ScheduleAfter(ms int, fn func()) chan struct{} { | ||
| done := make(chan struct{}) | ||
| target := time.Now().Add(time.Duration(ms) * time.Millisecond) | ||
|
|
||
| go func() { | ||
| for time.Now().Before(target) { | ||
| time.Sleep(1 * time.Millisecond) | ||
| } | ||
| fn() | ||
| close(done) | ||
| }() | ||
|
|
||
| return done | ||
| } |
This file contains hidden or 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,45 @@ | ||
| package timedelay | ||
|
|
||
| // Template with TODOs for teammates to implement using time.Now and time.Add. | ||
|
|
||
| import "time" | ||
|
|
||
| // WaitFor pauses execution until roughly ms milliseconds have passed. | ||
| func WaitForTemplate(ms int) { | ||
| // TODO: compute a target time using time.Now().Add and wait until reached | ||
| } | ||
|
|
||
| // NotifyAfter waits for roughly ms milliseconds then sends true on a channel. | ||
| func NotifyAfterTemplate(ms int) chan bool { | ||
| // TODO: start a goroutine that checks time.Now against a target and sends on the channel | ||
| return nil | ||
| } | ||
|
|
||
| // WaitUntil blocks until the provided target time is reached (or returns if in the past). | ||
| func WaitUntilTemplate(target time.Time) { | ||
| // TODO: loop until time.Now() is not before target | ||
| } | ||
|
|
||
| // NotifyAt sends true on a channel when the provided target time is reached. | ||
| func NotifyAtTemplate(target time.Time) chan bool { | ||
| // TODO: return a channel that will receive true when the target time arrives | ||
| return nil | ||
| } | ||
|
|
||
| // ElapsedMillis returns how many milliseconds have passed since t. | ||
| func ElapsedMillisTemplate(t time.Time) int64 { | ||
| // TODO: use time.Since to compute milliseconds | ||
| return 0 | ||
| } | ||
|
|
||
| // WaitForOrTimeout waits for ms milliseconds but returns an error if waiting exceeds timeoutMs. | ||
| func WaitForOrTimeoutTemplate(ms int, timeoutMs int) error { | ||
| // TODO: compute target and deadline with time.Now().Add and return error on timeout | ||
| return nil | ||
| } | ||
|
|
||
| // ScheduleAfter runs fn after roughly ms milliseconds and returns a channel closed when fn finishes. | ||
| func ScheduleAfterTemplate(ms int, fn func()) chan struct{} { | ||
| // TODO: start a goroutine that waits until target, runs fn, then closes the done channel | ||
| return nil | ||
| } |
102 changes: 102 additions & 0 deletions
102
internal/exercises/templates/41_time_delay/time_delay_test.go
This file contains hidden or 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,102 @@ | ||
| package timedelay | ||
|
|
||
| // Tests that validate the timing helpers with reasonable slack for CI. | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestWaitFor(t *testing.T) { | ||
| start := time.Now() | ||
| WaitFor(100) | ||
| el := time.Since(start).Milliseconds() | ||
| if el < 90 { | ||
| t.Fatalf("WaitFor(100) too short: %dms", el) | ||
| } | ||
| if el > 400 { | ||
| t.Fatalf("WaitFor(100) too long: %dms", el) | ||
| } | ||
| } | ||
|
|
||
| func TestNotifyAfter(t *testing.T) { | ||
| start := time.Now() | ||
| ch := NotifyAfter(100) | ||
|
|
||
| select { | ||
| case <-ch: | ||
| el := time.Since(start).Milliseconds() | ||
| if el < 90 { | ||
| t.Fatalf("NotifyAfter(100) too early: %dms", el) | ||
| } | ||
| if el > 500 { | ||
| t.Fatalf("NotifyAfter(100) too late: %dms", el) | ||
| } | ||
| case <-time.After(600 * time.Millisecond): | ||
| t.Fatal("NotifyAfter(100) did not send") | ||
| } | ||
| } | ||
|
|
||
| func TestWaitUntilAndNotifyAt(t *testing.T) { | ||
| start := time.Now() | ||
| target := time.Now().Add(120 * time.Millisecond) | ||
| WaitUntil(target) | ||
| el := time.Since(start).Milliseconds() | ||
| if el < 100 { | ||
| t.Fatalf("WaitUntil reached too early: %dms", el) | ||
| } | ||
| if el > 600 { | ||
| t.Fatalf("WaitUntil took too long: %dms", el) | ||
| } | ||
|
|
||
| start2 := time.Now() | ||
| target2 := time.Now().Add(100 * time.Millisecond) | ||
| ch := NotifyAt(target2) | ||
| select { | ||
| case <-ch: | ||
| el2 := time.Since(start2).Milliseconds() | ||
| if el2 < 80 { | ||
| t.Fatalf("NotifyAt too early: %dms", el2) | ||
| } | ||
| if el2 > 500 { | ||
| t.Fatalf("NotifyAt too late: %dms", el2) | ||
| } | ||
| case <-time.After(600 * time.Millisecond): | ||
| t.Fatal("NotifyAt did not send") | ||
| } | ||
| } | ||
|
|
||
| func TestElapsedMillis(t *testing.T) { | ||
| past := time.Now().Add(-150 * time.Millisecond) | ||
| if ElapsedMillis(past) < 130 { | ||
| t.Fatalf("ElapsedMillis too small: %dms", ElapsedMillis(past)) | ||
| } | ||
| } | ||
|
|
||
| func TestWaitForOrTimeout(t *testing.T) { | ||
| if err := WaitForOrTimeout(100, 300); err != nil { | ||
| t.Fatalf("expected nil, got %v", err) | ||
| } | ||
| if err := WaitForOrTimeout(300, 50); err == nil { | ||
| t.Fatal("expected timeout error, got nil") | ||
| } | ||
| } | ||
|
|
||
| func TestScheduleAfter(t *testing.T) { | ||
| flag := make(chan bool, 1) | ||
| fn := func() { flag <- true } | ||
|
|
||
| complete := ScheduleAfter(120, fn) | ||
|
|
||
| select { | ||
| case <-flag: | ||
| case <-time.After(700 * time.Millisecond): | ||
| t.Fatal("scheduled function did not run") | ||
| } | ||
|
|
||
| select { | ||
| case <-complete: | ||
| case <-time.After(700 * time.Millisecond): | ||
| t.Fatal("completion channel not closed") | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.