github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/nomad/structs/batch_future_test.go (about)

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