-
Notifications
You must be signed in to change notification settings - Fork 19
Feat/stateful goroutines exercise #112
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
Open
aviralgarg05
wants to merge
6
commits into
zhravan:main
Choose a base branch
from
aviralgarg05:feat/stateful-goroutines-exercise
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
71f8d69
feat: Add Stateful Goroutines exercise (28_stateful_goroutines)
aviralgarg05 4cbb20d
chore: Fix whitespace formatting and update .gitignore
aviralgarg05 6d8cc5a
fix: Format Go code according to Go standards
aviralgarg05 3657076
fix: address PR review issues
aviralgarg05 21ab013
feat: Migrate stateful goroutines exercise to new catalog structure (…
aviralgarg05 7af8201
fix(solution): explicitly close all channels in Close
aviralgarg05 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,4 +31,8 @@ go.work.sum | |
| # Editor/IDE | ||
| # .idea/ | ||
| # .vscode/ | ||
| bin/ | ||
| bin/ | ||
|
|
||
| # Codacy | ||
| .codacy/ | ||
| .github/instructions/ | ||
8 changes: 8 additions & 0 deletions
8
internal/exercises/Catalog/Concepts/47_stateful_goroutines.yaml
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,8 @@ | ||
| slug: 47_stateful_goroutines | ||
| title: Stateful Goroutines | ||
| test_regex: ".*" | ||
| hints: | ||
| - Use channels to send read and write operations to a state-owning goroutine. | ||
| - Create readOp and writeOp structs with response channels. | ||
| - The state-owning goroutine uses select to handle operations from channels. | ||
| - This pattern avoids mutexes by ensuring only one goroutine accesses shared state. |
72 changes: 72 additions & 0 deletions
72
internal/exercises/solutions/47_stateful_goroutines/stateful_goroutines.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,72 @@ | ||
| package stateful_goroutines | ||
|
|
||
| // readOp represents a read request | ||
| type readOp struct { | ||
| resp chan int | ||
| } | ||
|
|
||
| // writeOp represents a write request (increment) | ||
| type writeOp struct { | ||
| amount int | ||
| resp chan bool | ||
| } | ||
|
|
||
| type Counter struct { | ||
| reads chan readOp | ||
| writes chan writeOp | ||
| done chan struct{} | ||
| } | ||
|
|
||
| // NewCounter creates and starts a new stateful counter | ||
| func NewCounter() *Counter { | ||
| c := &Counter{ | ||
| reads: make(chan readOp), | ||
| writes: make(chan writeOp), | ||
| done: make(chan struct{}), | ||
| } | ||
|
|
||
| // Start the state-owning goroutine | ||
| go func() { | ||
| var state int | ||
| for { | ||
| select { | ||
| case read := <-c.reads: | ||
| read.resp <- state | ||
| case write := <-c.writes: | ||
| state += write.amount | ||
| write.resp <- true | ||
| case <-c.done: | ||
| return | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| // Increment increments the counter by the given amount | ||
| func (c *Counter) Increment(amount int) { | ||
| write := writeOp{ | ||
| amount: amount, | ||
| resp: make(chan bool), | ||
| } | ||
| c.writes <- write | ||
| <-write.resp | ||
| } | ||
|
|
||
| // GetValue returns the current counter value | ||
| func (c *Counter) GetValue() int { | ||
| read := readOp{ | ||
| resp: make(chan int), | ||
| } | ||
| c.reads <- read | ||
| return <-read.resp | ||
| } | ||
|
|
||
| // Close stops the state-owning goroutine | ||
| func (c *Counter) Close() { | ||
| close(c.done) | ||
| // Closing these channels ensures cleanup, though calling methods after Close will panic. | ||
| close(c.reads) | ||
| close(c.writes) | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
internal/exercises/templates/47_stateful_goroutines/stateful_goroutines.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,40 @@ | ||
| package stateful_goroutines | ||
|
|
||
| // TODO: | ||
| // - Implement a Counter that manages state using a single goroutine and channels. | ||
| // - The counter should support Increment and GetValue operations. | ||
| // - State must be owned by a single goroutine to avoid race conditions. | ||
| // - Other goroutines communicate via channels to read or modify the state. | ||
|
|
||
| // readOp represents a read request | ||
| type readOp struct { | ||
| resp chan int | ||
| } | ||
|
|
||
| // writeOp represents a write request (increment) | ||
| type writeOp struct { | ||
| amount int | ||
| resp chan bool | ||
| } | ||
|
|
||
| type Counter struct { | ||
| reads chan readOp | ||
| writes chan writeOp | ||
| } | ||
|
|
||
| // NewCounter creates and starts a new stateful counter | ||
| func NewCounter() *Counter { | ||
| // TODO: initialize channels and start the state-owning goroutine | ||
| return &Counter{} | ||
| } | ||
|
|
||
| // Increment increments the counter by the given amount | ||
| func (c *Counter) Increment(amount int) { | ||
| // TODO: send a write operation and wait for confirmation | ||
| } | ||
|
|
||
| // GetValue returns the current counter value | ||
| func (c *Counter) GetValue() int { | ||
| // TODO: send a read operation and return the value | ||
| return 0 | ||
| } |
110 changes: 110 additions & 0 deletions
110
internal/exercises/templates/47_stateful_goroutines/stateful_goroutines_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,110 @@ | ||
| package stateful_goroutines | ||
|
|
||
| import ( | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestCounterInitialization(t *testing.T) { | ||
| counter := NewCounter() | ||
| if counter == nil { | ||
| t.Fatal("NewCounter() returned nil") | ||
| } | ||
|
|
||
| value := counter.GetValue() | ||
| if value != 0 { | ||
| t.Errorf("Initial counter value = %d, want 0", value) | ||
| } | ||
| } | ||
|
|
||
| func TestCounterIncrement(t *testing.T) { | ||
| counter := NewCounter() | ||
|
|
||
| counter.Increment(5) | ||
| counter.Increment(3) | ||
|
|
||
| value := counter.GetValue() | ||
| if value != 8 { | ||
| t.Errorf("Counter value = %d, want 8", value) | ||
| } | ||
| } | ||
|
|
||
| func TestCounterConcurrentIncrements(t *testing.T) { | ||
| counter := NewCounter() | ||
|
|
||
| var wg sync.WaitGroup | ||
| numGoroutines := 100 | ||
| incrementsPerGoroutine := 10 | ||
|
|
||
| for i := 0; i < numGoroutines; i++ { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for j := 0; j < incrementsPerGoroutine; j++ { | ||
| counter.Increment(1) | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| wg.Wait() | ||
|
|
||
| expected := numGoroutines * incrementsPerGoroutine | ||
| value := counter.GetValue() | ||
| if value != expected { | ||
| t.Errorf("Counter value = %d, want %d", value, expected) | ||
| } | ||
| } | ||
|
|
||
| func TestCounterConcurrentReadsAndWrites(t *testing.T) { | ||
| counter := NewCounter() | ||
|
|
||
| var wg sync.WaitGroup | ||
| numReaders := 50 | ||
| numWriters := 50 | ||
|
|
||
| // Start writers | ||
| for i := 0; i < numWriters; i++ { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for j := 0; j < 5; j++ { | ||
| counter.Increment(1) | ||
| time.Sleep(time.Microsecond) | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| // Start readers | ||
| for i := 0; i < numReaders; i++ { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for j := 0; j < 5; j++ { | ||
| _ = counter.GetValue() | ||
| time.Sleep(time.Microsecond) | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| wg.Wait() | ||
|
|
||
| // Verify final value | ||
| expected := numWriters * 5 | ||
| value := counter.GetValue() | ||
| if value != expected { | ||
| t.Errorf("Counter value = %d, want %d", value, expected) | ||
| } | ||
| } | ||
|
|
||
| func TestCounterNegativeIncrement(t *testing.T) { | ||
| counter := NewCounter() | ||
|
|
||
| counter.Increment(10) | ||
| counter.Increment(-3) | ||
|
|
||
| value := counter.GetValue() | ||
| if value != 7 { | ||
| t.Errorf("Counter value = %d, want 7", value) | ||
| } | ||
| } |
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.