Skip to content

Commit

Permalink
feat: add new methods to routineGroup struct and test case
Browse files Browse the repository at this point in the history
- Add a new method `Run` to the `routineGroup` struct in `thread.go`
- Add a new method `Wait` to the `routineGroup` struct in `thread.go`
- Add a new test case for the `Run` method in `thread_test.go`

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Nov 26, 2023
1 parent a9de4f3 commit 6b77f3c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package graceful

import "sync"

// routineGroup represents a group of goroutines.
type routineGroup struct {
waitGroup sync.WaitGroup
}

// newRoutineGroup creates a new routineGroup.
func newRoutineGroup() *routineGroup {
return new(routineGroup)
}

// Run runs a function in a new goroutine.
func (g *routineGroup) Run(fn func()) {
g.waitGroup.Add(1)

Expand All @@ -19,6 +22,7 @@ func (g *routineGroup) Run(fn func()) {
}()
}

// Wait waits for all goroutines to finish.
func (g *routineGroup) Wait() {
g.waitGroup.Wait()
}
27 changes: 27 additions & 0 deletions thread_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
package graceful

import (
"testing"
)

func TestRun(t *testing.T) {
g := newRoutineGroup()

// Define a flag to check if the function was executed
var executed bool

// Define the function to be executed
fn := func() {
executed = true
}

// Run the function in a goroutine
g.Run(fn)

// Wait for the goroutine to finish
g.Wait()

// Check if the function was executed
if !executed {
t.Error("Function was not executed")
}
}

0 comments on commit 6b77f3c

Please sign in to comment.