github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/nomad/structs/batch_future_test.go (about)

     1  package structs
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestBatchFuture(t *testing.T) {
    10  	t.Parallel()
    11  	bf := NewBatchFuture()
    12  
    13  	// Async respond to the future
    14  	expect := fmt.Errorf("testing")
    15  	go func() {
    16  		time.Sleep(10 * time.Millisecond)
    17  		bf.Respond(1000, expect)
    18  	}()
    19  
    20  	// Block for the result
    21  	start := time.Now()
    22  	err := bf.Wait()
    23  	diff := time.Since(start)
    24  	if diff < 5*time.Millisecond {
    25  		t.Fatalf("too fast")
    26  	}
    27  
    28  	// Check the results
    29  	if err != expect {
    30  		t.Fatalf("bad: %s", err)
    31  	}
    32  	if bf.Index() != 1000 {
    33  		t.Fatalf("bad: %d", bf.Index())
    34  	}
    35  }