2018-08-30 23:59:16 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-11-17 01:29:25 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2018-08-30 23:59:16 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-08-30 21:33:50 +00:00
|
|
|
// TestAllocBroadcaster_SendRecv asserts the latest sends to a broadcaster are
|
|
|
|
// received by listeners.
|
2018-08-30 23:59:16 +00:00
|
|
|
func TestAllocBroadcaster_SendRecv(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-08-30 23:59:16 +00:00
|
|
|
|
2018-11-17 01:29:25 +00:00
|
|
|
b := NewAllocBroadcaster(testlog.HCLogger(t))
|
2018-08-30 23:59:16 +00:00
|
|
|
defer b.Close()
|
|
|
|
|
2018-08-30 21:33:50 +00:00
|
|
|
// Create a listener and assert it blocks until an update
|
2018-08-30 23:59:16 +00:00
|
|
|
l := b.Listen()
|
|
|
|
defer l.Close()
|
2018-08-30 21:33:50 +00:00
|
|
|
select {
|
2018-11-17 01:29:25 +00:00
|
|
|
case <-l.Ch():
|
2018-08-30 21:33:50 +00:00
|
|
|
t.Fatalf("unexpected initial alloc")
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
// Ok! Ch is empty until a Send
|
|
|
|
}
|
2018-08-30 23:59:16 +00:00
|
|
|
|
2018-08-30 21:33:50 +00:00
|
|
|
// Send an update
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.AllocModifyIndex = 10
|
2018-08-30 23:59:16 +00:00
|
|
|
require.NoError(t, b.Send(alloc.Copy()))
|
2018-11-17 01:29:25 +00:00
|
|
|
recvd := <-l.Ch()
|
2018-08-30 23:59:16 +00:00
|
|
|
require.Equal(t, alloc.AllocModifyIndex, recvd.AllocModifyIndex)
|
|
|
|
|
|
|
|
// Send two now copies and assert only the last was received
|
|
|
|
alloc.AllocModifyIndex = 30
|
|
|
|
require.NoError(t, b.Send(alloc.Copy()))
|
|
|
|
alloc.AllocModifyIndex = 40
|
|
|
|
require.NoError(t, b.Send(alloc.Copy()))
|
|
|
|
|
2018-11-17 01:29:25 +00:00
|
|
|
recvd = <-l.Ch()
|
2018-08-30 23:59:16 +00:00
|
|
|
require.Equal(t, alloc.AllocModifyIndex, recvd.AllocModifyIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAllocBroadcaster_RecvBlocks asserts listeners are blocked until a send occurs.
|
|
|
|
func TestAllocBroadcaster_RecvBlocks(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-08-30 23:59:16 +00:00
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
2018-11-17 01:29:25 +00:00
|
|
|
b := NewAllocBroadcaster(testlog.HCLogger(t))
|
2018-08-30 23:59:16 +00:00
|
|
|
defer b.Close()
|
|
|
|
|
|
|
|
l1 := b.Listen()
|
|
|
|
defer l1.Close()
|
|
|
|
|
|
|
|
l2 := b.Listen()
|
|
|
|
defer l2.Close()
|
|
|
|
|
|
|
|
done := make(chan int, 2)
|
|
|
|
|
|
|
|
// Subsequent listens should block until a subsequent send
|
|
|
|
go func() {
|
2018-11-17 01:29:25 +00:00
|
|
|
<-l1.Ch()
|
2018-08-30 23:59:16 +00:00
|
|
|
done <- 1
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2018-11-17 01:29:25 +00:00
|
|
|
<-l2.Ch()
|
2018-08-30 23:59:16 +00:00
|
|
|
done <- 1
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
t.Fatalf("unexpected receive by a listener")
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a Send and expect both listeners to receive it
|
|
|
|
b.Send(alloc)
|
|
|
|
<-done
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAllocBroadcaster_Concurrency asserts that the broadcaster behaves
|
|
|
|
// correctly with concurrent listeners being added and closed.
|
|
|
|
func TestAllocBroadcaster_Concurrency(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-08-30 23:59:16 +00:00
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
2018-11-17 01:29:25 +00:00
|
|
|
b := NewAllocBroadcaster(testlog.HCLogger(t))
|
2018-08-30 23:59:16 +00:00
|
|
|
defer b.Close()
|
|
|
|
|
|
|
|
errs := make(chan error, 10)
|
|
|
|
listeners := make([]*AllocListener, 10)
|
|
|
|
for i := 0; i < len(listeners); i++ {
|
|
|
|
l := b.Listen()
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
listeners[i] = l
|
|
|
|
go func(index uint64, listener *AllocListener) {
|
|
|
|
defer listener.Close()
|
|
|
|
for {
|
2018-11-17 01:29:25 +00:00
|
|
|
a, ok := <-listener.Ch()
|
2018-08-30 23:59:16 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.AllocModifyIndex < index {
|
|
|
|
errs <- fmt.Errorf("index=%d < %d", a.AllocModifyIndex, index)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
index = a.AllocModifyIndex
|
|
|
|
}
|
|
|
|
}(alloc.AllocModifyIndex, l)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
alloc.AllocModifyIndex++
|
|
|
|
require.NoError(t, b.Send(alloc.Copy()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
t.Fatalf("%d listener errors. First error:\n%v", len(errs), <-errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Closing a couple shouldn't cause errors
|
|
|
|
listeners[0].Close()
|
|
|
|
listeners[1].Close()
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
alloc.AllocModifyIndex++
|
|
|
|
require.NoError(t, b.Send(alloc.Copy()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
t.Fatalf("%d listener errors. First error:\n%v", len(errs), <-errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Closing the broadcaster *should* error
|
|
|
|
b.Close()
|
|
|
|
require.Equal(t, ErrAllocBroadcasterClosed, b.Send(alloc))
|
|
|
|
|
|
|
|
// All Listeners should be closed
|
|
|
|
for _, l := range listeners {
|
|
|
|
select {
|
2018-11-17 01:29:25 +00:00
|
|
|
case _, ok := <-l.Ch():
|
2018-08-30 23:59:16 +00:00
|
|
|
if ok {
|
|
|
|
// This check can beat the goroutine above to
|
|
|
|
// recv'ing the final update. Listener must be
|
|
|
|
// closed on next recv.
|
2018-11-17 01:29:25 +00:00
|
|
|
if _, ok := <-l.Ch(); ok {
|
2018-08-30 23:59:16 +00:00
|
|
|
t.Fatalf("expected listener to be closed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatalf("expected listener to be closed; not blocking")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-17 01:29:25 +00:00
|
|
|
|
|
|
|
// TestAllocBroadcaster_PrimeListener asserts that newly created listeners are
|
|
|
|
// primed with the last sent alloc.
|
|
|
|
func TestAllocBroadcaster_PrimeListener(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-11-17 01:29:25 +00:00
|
|
|
|
|
|
|
b := NewAllocBroadcaster(testlog.HCLogger(t))
|
|
|
|
defer b.Close()
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
|
|
|
|
// Send an update before creating a listener
|
|
|
|
require.NoError(t, b.Send(alloc))
|
|
|
|
|
|
|
|
// Create a listener and assert it immediately receives an update
|
|
|
|
l := b.Listen()
|
|
|
|
defer l.Close()
|
|
|
|
select {
|
|
|
|
case recv := <-l.Ch():
|
|
|
|
require.Equal(t, alloc, recv)
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
2018-11-28 00:33:55 +00:00
|
|
|
t.Fatalf("expected to receive initial value")
|
2018-11-17 01:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-20 00:33:24 +00:00
|
|
|
|
|
|
|
// TestAllocBroadcaster_Closed asserts that newly created listeners are
|
|
|
|
// primed with the last sent alloc even when the broadcaster is closed.
|
|
|
|
func TestAllocBroadcaster_Closed(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-11-20 00:33:24 +00:00
|
|
|
|
|
|
|
b := NewAllocBroadcaster(testlog.HCLogger(t))
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
|
|
|
|
// Send an update before creating a listener
|
|
|
|
require.NoError(t, b.Send(alloc))
|
|
|
|
|
|
|
|
// Close the broadcaster after sending a single update
|
|
|
|
b.Close()
|
|
|
|
|
|
|
|
// Create a listener and assert it immediately receives an update
|
|
|
|
l := b.Listen()
|
|
|
|
defer l.Close()
|
|
|
|
select {
|
|
|
|
case recv := <-l.Ch():
|
|
|
|
require.Equal(t, alloc, recv)
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
2018-11-28 00:33:55 +00:00
|
|
|
t.Fatalf("expected to receive initial value")
|
2018-11-20 00:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ch should now be closed.
|
|
|
|
select {
|
|
|
|
case _, ok := <-l.Ch():
|
|
|
|
require.False(t, ok)
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
t.Fatalf("expected Ch() to be closed")
|
|
|
|
}
|
|
|
|
}
|