github.com/hernad/nomad@v1.6.112/nomad/structs/batch_future_test.go (about)

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