-
Notifications
You must be signed in to change notification settings - Fork 24
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
4
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.
+232
−1
Open
Changes from 3 commits
Commits
Show all changes
4 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 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 |
|---|---|---|
|
|
@@ -30,4 +30,8 @@ go.work.sum | |
| # Editor/IDE | ||
| # .idea/ | ||
| # .vscode/ | ||
| bin/ | ||
| bin/ | ||
|
|
||
| # Codacy | ||
| .codacy/ | ||
| .github/instructions/ | ||
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
60 changes: 60 additions & 0 deletions
60
internal/exercises/solutions/28_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,60 @@ | ||
| 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 | ||
| } | ||
|
|
||
| // NewCounter creates and starts a new stateful counter | ||
| func NewCounter() *Counter { | ||
| c := &Counter{ | ||
| reads: make(chan readOp), | ||
| writes: make(chan writeOp), | ||
| } | ||
|
|
||
| // 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 | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| 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 | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
internal/exercises/templates/28_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 | ||
| } |
113 changes: 113 additions & 0 deletions
113
internal/exercises/templates/28_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,113 @@ | ||
| package stateful_goroutines | ||
|
|
||
| import ( | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestCounterInitialization(t *testing.T) { | ||
| counter := NewCounter() | ||
| if counter == nil { | ||
| t.Fatal("NewCounter() returned nil") | ||
| } | ||
|
|
||
| // Give goroutine time to start | ||
| time.Sleep(10 * time.Millisecond) | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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.