From a8e65c3fd4b68e28969a7da0dff572c9a48500d2 Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Mon, 2 Apr 2018 16:40:06 -0700 Subject: [PATCH] drain: refactor batch_future into its own file aka What If structs.go Wasn't So Big? --- nomad/structs/batch_future.go | 43 ++++++++++++++++++++++++++++++ nomad/structs/batch_future_test.go | 35 ++++++++++++++++++++++++ nomad/structs/structs.go | 42 ----------------------------- nomad/structs/structs_test.go | 28 ------------------- 4 files changed, 78 insertions(+), 70 deletions(-) create mode 100644 nomad/structs/batch_future.go create mode 100644 nomad/structs/batch_future_test.go diff --git a/nomad/structs/batch_future.go b/nomad/structs/batch_future.go new file mode 100644 index 000000000..c0ddc30f3 --- /dev/null +++ b/nomad/structs/batch_future.go @@ -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) +} diff --git a/nomad/structs/batch_future_test.go b/nomad/structs/batch_future_test.go new file mode 100644 index 000000000..52ff12563 --- /dev/null +++ b/nomad/structs/batch_future_test.go @@ -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()) + } +} diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 328ff86a1..23aa8fc5b 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -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) -} diff --git a/nomad/structs/structs_test.go b/nomad/structs/structs_test.go index 1f2193b29..b76bb9b98 100644 --- a/nomad/structs/structs_test.go +++ b/nomad/structs/structs_test.go @@ -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)