Skip to content

Commit

Permalink
feat(coroutine): Added Concurrent and Parallel
Browse files Browse the repository at this point in the history
Signed-off-by: Flc゛ <[email protected]>
  • Loading branch information
flc1125 committed Jan 25, 2024
1 parent 6540102 commit dbff970
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
8 changes: 8 additions & 0 deletions coroutine/concurrent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package coroutine

import (
"bytes"
"sync"
"testing"
"time"

Expand All @@ -12,16 +13,23 @@ func TestConcurrent(t *testing.T) {
var (
start = time.Now()
buffer bytes.Buffer
mu sync.Mutex
)

Concurrent(2, func() {
time.Sleep(1 * time.Second)
mu.Lock()
defer mu.Unlock()
buffer.WriteString("1")
}, func() {
time.Sleep(2 * time.Second)
mu.Lock()
defer mu.Unlock()
buffer.WriteString("2")
}, func() {
time.Sleep(3 * time.Second)
mu.Lock()
defer mu.Unlock()
buffer.WriteString("3")
})

Expand Down
15 changes: 12 additions & 3 deletions coroutine/parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,34 @@ package coroutine

import (
"bytes"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestParallel(t *testing.T) {
start := time.Now()

var buffer bytes.Buffer
var (
start = time.Now()
buffer bytes.Buffer
mu sync.Mutex
)

Parallel(func() {
time.Sleep(1 * time.Second)
mu.Lock()
defer mu.Unlock()
buffer.WriteString("1")
}, func() {
time.Sleep(2 * time.Second)
mu.Lock()
defer mu.Unlock()
buffer.WriteString("2")
}, func() {
time.Sleep(3 * time.Second)
mu.Lock()
defer mu.Unlock()
buffer.WriteString("3")
})

Expand Down

0 comments on commit dbff970

Please sign in to comment.