drain: refactor batch_future into its own file
aka What If structs.go Wasn't So Big?
This commit is contained in:
parent
44a749a7cc
commit
6840becf46
43
nomad/structs/batch_future.go
Normal file
43
nomad/structs/batch_future.go
Normal 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)
|
||||
}
|
35
nomad/structs/batch_future_test.go
Normal file
35
nomad/structs/batch_future_test.go
Normal 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())
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue