2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-04-02 23:40:06 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2022-03-15 12:42:43 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-04-02 23:40:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBatchFuture(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-04-02 23:40:06 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|