mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-19 06:31:57 +00:00
libs: internalize some packages (#6366)
## Description Internalize some libs. This reduces the amount ot public API tendermint is supporting. The moved libraries are mainly ones that are used within Tendermint-core.
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
package sync
|
||||
|
||||
import "sync"
|
||||
|
||||
// Closer implements a primitive to close a channel that signals process
|
||||
// termination while allowing a caller to call Close multiple times safely. It
|
||||
// should be used in cases where guarantees cannot be made about when and how
|
||||
// many times closure is executed.
|
||||
type Closer struct {
|
||||
closeOnce sync.Once
|
||||
doneCh chan struct{}
|
||||
}
|
||||
|
||||
// NewCloser returns a reference to a new Closer.
|
||||
func NewCloser() *Closer {
|
||||
return &Closer{doneCh: make(chan struct{})}
|
||||
}
|
||||
|
||||
// Done returns the internal done channel allowing the caller either block or wait
|
||||
// for the Closer to be terminated/closed.
|
||||
func (c *Closer) Done() <-chan struct{} {
|
||||
return c.doneCh
|
||||
}
|
||||
|
||||
// Close gracefully closes the Closer. A caller should only call Close once, but
|
||||
// it is safe to call it successive times.
|
||||
func (c *Closer) Close() {
|
||||
c.closeOnce.Do(func() {
|
||||
close(c.doneCh)
|
||||
})
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package sync_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
tmsync "github.com/tendermint/tendermint/libs/sync"
|
||||
)
|
||||
|
||||
func TestCloser(t *testing.T) {
|
||||
closer := tmsync.NewCloser()
|
||||
|
||||
var timeout bool
|
||||
|
||||
select {
|
||||
case <-closer.Done():
|
||||
case <-time.After(time.Second):
|
||||
timeout = true
|
||||
}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
closer.Close()
|
||||
}
|
||||
|
||||
require.True(t, timeout)
|
||||
<-closer.Done()
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
// +build deadlock
|
||||
|
||||
package sync
|
||||
|
||||
import (
|
||||
deadlock "github.com/sasha-s/go-deadlock"
|
||||
)
|
||||
|
||||
// A Mutex is a mutual exclusion lock.
|
||||
type Mutex struct {
|
||||
deadlock.Mutex
|
||||
}
|
||||
|
||||
// An RWMutex is a reader/writer mutual exclusion lock.
|
||||
type RWMutex struct {
|
||||
deadlock.RWMutex
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
// +build !deadlock
|
||||
|
||||
package sync
|
||||
|
||||
import "sync"
|
||||
|
||||
// A Mutex is a mutual exclusion lock.
|
||||
type Mutex struct {
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
// An RWMutex is a reader/writer mutual exclusion lock.
|
||||
type RWMutex struct {
|
||||
sync.RWMutex
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package sync
|
||||
|
||||
// Waker is used to wake up a sleeper when some event occurs. It debounces
|
||||
// multiple wakeup calls occurring between each sleep, and wakeups are
|
||||
// non-blocking to avoid having to coordinate goroutines.
|
||||
type Waker struct {
|
||||
wakeCh chan struct{}
|
||||
}
|
||||
|
||||
// NewWaker creates a new Waker.
|
||||
func NewWaker() *Waker {
|
||||
return &Waker{
|
||||
wakeCh: make(chan struct{}, 1), // buffer used for debouncing
|
||||
}
|
||||
}
|
||||
|
||||
// Sleep returns a channel that blocks until Wake() is called.
|
||||
func (w *Waker) Sleep() <-chan struct{} {
|
||||
return w.wakeCh
|
||||
}
|
||||
|
||||
// Wake wakes up the sleeper.
|
||||
func (w *Waker) Wake() {
|
||||
// A non-blocking send with a size 1 buffer ensures that we never block, and
|
||||
// that we queue up at most a single wakeup call between each Sleep().
|
||||
select {
|
||||
case w.wakeCh <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package sync_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
tmsync "github.com/tendermint/tendermint/libs/sync"
|
||||
)
|
||||
|
||||
func TestWaker(t *testing.T) {
|
||||
|
||||
// A new waker should block when sleeping.
|
||||
waker := tmsync.NewWaker()
|
||||
|
||||
select {
|
||||
case <-waker.Sleep():
|
||||
require.Fail(t, "unexpected wakeup")
|
||||
default:
|
||||
}
|
||||
|
||||
// Wakeups should not block, and should cause the next sleeper to awaken.
|
||||
waker.Wake()
|
||||
|
||||
select {
|
||||
case <-waker.Sleep():
|
||||
default:
|
||||
require.Fail(t, "expected wakeup, but sleeping instead")
|
||||
}
|
||||
|
||||
// Multiple wakeups should only wake a single sleeper.
|
||||
waker.Wake()
|
||||
waker.Wake()
|
||||
waker.Wake()
|
||||
|
||||
select {
|
||||
case <-waker.Sleep():
|
||||
default:
|
||||
require.Fail(t, "expected wakeup, but sleeping instead")
|
||||
}
|
||||
|
||||
select {
|
||||
case <-waker.Sleep():
|
||||
require.Fail(t, "unexpected wakeup")
|
||||
default:
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user