drain: refactor batch_future into its own file

aka What If structs.go Wasn't So Big?
This commit is contained in:
Michael Schurter 2018-04-02 16:40:06 -07:00
parent 44a749a7cc
commit 6840becf46
4 changed files with 78 additions and 70 deletions

View file

@ -0,0 +1,43 @@
package structs
// BatchFuture is used to wait on a batch update to complete
type BatchFuture struct {
doneCh chan struct{}
err error
index uint64
}
// NewBatchFuture creates a new batch future
func NewBatchFuture() *BatchFuture {
return &BatchFuture{
doneCh: make(chan struct{}),
}
}
// Wait is used to block for the future to complete and returns the error
func (b *BatchFuture) Wait() error {
<-b.doneCh
return b.err
}
// WaitCh is used to block for the future to complete
func (b *BatchFuture) WaitCh() <-chan struct{} {
return b.doneCh
}
// Error is used to return the error of the batch, only after Wait()
func (b *BatchFuture) Error() error {
return b.err
}
// Index is used to return the index of the batch, only after Wait()
func (b *BatchFuture) Index() uint64 {
return b.index
}
// Respond is used to unblock the future
func (b *BatchFuture) Respond(index uint64, err error) {
b.index = index
b.err = err
close(b.doneCh)
}

View file

@ -0,0 +1,35 @@
package structs
import (
"fmt"
"testing"
"time"
)
func TestBatchFuture(t *testing.T) {
t.Parallel()
bf := NewBatchFuture()
// Async respond to the future
expect := fmt.Errorf("testing")
go func() {
time.Sleep(10 * time.Millisecond)
bf.Respond(1000, expect)
}()
// Block for the result
start := time.Now()
err := bf.Wait()
diff := time.Since(start)
if diff < 5*time.Millisecond {
t.Fatalf("too fast")
}
// Check the results
if err != expect {
t.Fatalf("bad: %s", err)
}
if bf.Index() != 1000 {
t.Fatalf("bad: %d", bf.Index())
}
}

View file

@ -7101,45 +7101,3 @@ type ACLTokenUpsertResponse struct {
Tokens []*ACLToken
WriteMeta
}
// BatchFuture is used to wait on a batch update to complete
type BatchFuture struct {
doneCh chan struct{}
err error
index uint64
}
// NewBatchFuture creates a new batch future
func NewBatchFuture() *BatchFuture {
return &BatchFuture{
doneCh: make(chan struct{}),
}
}
// Wait is used to block for the future to complete and returns the error
func (b *BatchFuture) Wait() error {
<-b.doneCh
return b.err
}
// WaitCh is used to block for the future to complete
func (b *BatchFuture) WaitCh() <-chan struct{} {
return b.doneCh
}
// Error is used to return the error of the batch, only after Wait()
func (b *BatchFuture) Error() error {
return b.err
}
// Index is used to return the index of the batch, only after Wait()
func (b *BatchFuture) Index() uint64 {
return b.index
}
// Respond is used to unblock the future
func (b *BatchFuture) Respond(index uint64, err error) {
b.index = index
b.err = err
close(b.doneCh)
}

View file

@ -3652,34 +3652,6 @@ func TestNetworkResourcesEquals(t *testing.T) {
}
}
func TestBatchFuture(t *testing.T) {
t.Parallel()
bf := NewBatchFuture()
// Async respond to the future
expect := fmt.Errorf("testing")
go func() {
time.Sleep(10 * time.Millisecond)
bf.Respond(1000, expect)
}()
// Block for the result
start := time.Now()
err := bf.Wait()
diff := time.Since(start)
if diff < 5*time.Millisecond {
t.Fatalf("too fast")
}
// Check the results
if err != expect {
t.Fatalf("bad: %s", err)
}
if bf.Index() != 1000 {
t.Fatalf("bad: %d", bf.Index())
}
}
func TestNode_Canonicalize(t *testing.T) {
t.Parallel()
require := require.New(t)