go-sync-utils, v1.0
Posted on

The library that provides utility entities for syncing.
Major version.
Features
- interface of the
sync.WaitGrouptype; - operating with a set of such interfaces as a whole.
Installation
Prepare the directory:
$ mkdir --parents "$(go env GOPATH)/src/github.com/thewizardplusplus/"
$ cd "$(go env GOPATH)/src/github.com/thewizardplusplus/"
Clone this repository:
$ git clone https://github.com/thewizardplusplus/go-sync-utils.git
$ cd go-sync-utils
Install dependencies with the dep tool:
$ dep ensure -vendor-only
Examples
syncutils.MultiWaitGroup:
package main
import (
"fmt"
"math/rand"
"strings"
"sync"
"time"
syncutils "github.com/thewizardplusplus/go-sync-utils"
)
type Call struct {
Method string
Arguments []interface{}
}
func (call Call) String() string {
arguments := strings.Trim(fmt.Sprint(call.Arguments), "[]")
return fmt.Sprintf("%s(%s)", call.Method, arguments)
}
type MockWaitGroup struct {
sync.Mutex
Calls []Call
}
func (mock *MockWaitGroup) Add(delta int) {
mock.Lock()
defer mock.Unlock()
mock.Calls = append(mock.Calls, Call{"Add", []interface{}{delta}})
}
func (mock *MockWaitGroup) Done() {
mock.Lock()
defer mock.Unlock()
mock.Calls = append(mock.Calls, Call{"Done", []interface{}{}})
}
func (mock *MockWaitGroup) Wait() {
mock.Lock()
defer mock.Unlock()
mock.Calls = append(mock.Calls, Call{"Wait", []interface{}{}})
}
func main() {
waitGroupMock := new(MockWaitGroup)
waitGroups := syncutils.MultiWaitGroup{waitGroupMock, new(sync.WaitGroup)}
for _, duration := range []time.Duration{
time.Duration(rand.Intn(100)) * time.Millisecond,
time.Duration(rand.Intn(100)) * time.Millisecond,
} {
waitGroups.Add(1)
go func(duration time.Duration) {
defer waitGroups.Done()
time.Sleep(duration)
}(duration)
}
waitGroups.Wait()
for _, call := range waitGroupMock.Calls {
fmt.Println(call)
}
// Unordered output:
// Add(1)
// Add(1)
// Done()
// Done()
// Wait()
}
Repository
Link: https://github.com/thewizardplusplus/go-sync-utils/tree/v1.0.
Content: code.
License: MIT.