generated from snivilised/astrolib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(boost): add boost wait group interface (#295)
- Loading branch information
1 parent
2963873
commit 42d182a
Showing
14 changed files
with
123 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,49 @@ | ||
package boost | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
// Tracker | ||
type Tracker func(count int32) | ||
|
||
// TrackWaitGroup returns a trackable wait group for the native sync | ||
// wait group specified; useful for debugging purposes. | ||
func TrackWaitGroup(wg *sync.WaitGroup, add, done Tracker) WaitGroup { | ||
return &TrackableWaitGroup{ | ||
wg: wg, | ||
add: add, | ||
done: done, | ||
} | ||
} | ||
|
||
// TrackableWaitGroup | ||
type TrackableWaitGroup struct { | ||
wg *sync.WaitGroup | ||
counter int32 | ||
add, done Tracker | ||
} | ||
|
||
// Add | ||
func (t *TrackableWaitGroup) Add(delta int) { | ||
n := atomic.AddInt32(&t.counter, int32(delta)) | ||
t.wg.Add(delta) | ||
t.add(n) | ||
} | ||
|
||
// Done | ||
func (t *TrackableWaitGroup) Done() { | ||
n := atomic.AddInt32(&t.counter, int32(-1)) | ||
t.wg.Done() | ||
t.done(n) | ||
} | ||
|
||
// Wait | ||
func (t *TrackableWaitGroup) Wait() { | ||
t.wg.Wait() | ||
} | ||
|
||
func (t *TrackableWaitGroup) Count() int32 { | ||
return atomic.LoadInt32(&t.counter) | ||
} |
This file contains 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,45 @@ | ||
package boost_test | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo ok | ||
. "github.com/onsi/gomega" //nolint:revive // gomega ok | ||
|
||
"github.com/snivilised/lorax/boost" | ||
) | ||
|
||
var _ = Describe("boost.WaitGroup", func() { | ||
Context("given: a sync.WaitGroup", func() { | ||
It("should: track invocations", func() { | ||
var wg sync.WaitGroup | ||
tracker := boost.TrackWaitGroup(&wg, | ||
func(count int32) { | ||
GinkgoWriter.Printf("---> 🔴 Add (%v)\n", count) | ||
}, | ||
func(count int32) { | ||
GinkgoWriter.Printf("---> 🟢 Done (%v)\n", count) | ||
}, | ||
) | ||
|
||
for range 10 { | ||
tracker.Add(1) | ||
go func(tracker boost.WaitGroup) { | ||
defer tracker.Done() | ||
|
||
const delay = time.Millisecond * 100 | ||
time.Sleep(delay) | ||
}(tracker) | ||
} | ||
|
||
tracker.Wait() | ||
|
||
if wg, ok := tracker.(*boost.TrackableWaitGroup); ok { | ||
Expect(wg.Count()).To(BeEquivalentTo(0)) | ||
} else { | ||
Fail("tracker should be *boost.TrackableWaitGroup") | ||
} | ||
}) | ||
}) | ||
}) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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