2018-03-08 23:08:23 +00:00
|
|
|
package drainer
|
2018-03-02 23:19:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-04-03 23:51:57 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2018-03-02 23:19:55 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDeadlineHeap_Interface(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-03-02 23:19:55 +00:00
|
|
|
require := require.New(t)
|
|
|
|
h := NewDeadlineHeap(context.Background(), 1*time.Second)
|
|
|
|
require.Implements((*DrainDeadlineNotifier)(nil), h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeadlineHeap_WatchAndGet(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-03-02 23:19:55 +00:00
|
|
|
require := require.New(t)
|
|
|
|
h := NewDeadlineHeap(context.Background(), 1*time.Second)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
nodeID := "1"
|
|
|
|
wait := 10 * time.Millisecond
|
|
|
|
deadline := now.Add(wait)
|
|
|
|
h.Watch(nodeID, deadline)
|
|
|
|
|
|
|
|
var batch []string
|
|
|
|
select {
|
|
|
|
case batch = <-h.NextBatch():
|
2018-10-03 16:47:18 +00:00
|
|
|
case <-time.After(testutil.Timeout(3 * wait)):
|
2018-03-02 23:19:55 +00:00
|
|
|
t.Fatal("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Len(batch, 1)
|
|
|
|
require.Equal(nodeID, batch[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeadlineHeap_WatchThenUpdateAndGet(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-03-02 23:19:55 +00:00
|
|
|
require := require.New(t)
|
|
|
|
h := NewDeadlineHeap(context.Background(), 1*time.Second)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
nodeID := "1"
|
|
|
|
wait := 10 * time.Millisecond
|
|
|
|
deadline := now.Add(wait)
|
|
|
|
|
|
|
|
// Initially watch way in the future
|
|
|
|
h.Watch(nodeID, now.Add(24*time.Hour))
|
|
|
|
|
|
|
|
// Rewatch
|
|
|
|
h.Watch(nodeID, deadline)
|
|
|
|
|
|
|
|
var batch []string
|
|
|
|
select {
|
|
|
|
case batch = <-h.NextBatch():
|
2018-04-03 23:51:57 +00:00
|
|
|
case <-time.After(testutil.Timeout(2 * wait)):
|
2018-03-02 23:19:55 +00:00
|
|
|
t.Fatal("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Len(batch, 1)
|
|
|
|
require.Equal(nodeID, batch[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeadlineHeap_MultiwatchAndDelete(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-03-02 23:19:55 +00:00
|
|
|
require := require.New(t)
|
|
|
|
h := NewDeadlineHeap(context.Background(), 1*time.Second)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
wait := 50 * time.Millisecond
|
|
|
|
deadline := now.Add(wait)
|
|
|
|
|
|
|
|
nodeID1 := "1"
|
|
|
|
nodeID2 := "2"
|
|
|
|
h.Watch(nodeID1, deadline)
|
|
|
|
h.Watch(nodeID2, deadline)
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
h.Remove(nodeID2)
|
|
|
|
|
|
|
|
var batch []string
|
|
|
|
select {
|
|
|
|
case batch = <-h.NextBatch():
|
2018-04-03 23:51:57 +00:00
|
|
|
case <-time.After(testutil.Timeout(2 * wait)):
|
2018-03-02 23:19:55 +00:00
|
|
|
t.Fatal("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Len(batch, 1)
|
|
|
|
require.Equal(nodeID1, batch[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeadlineHeap_WatchCoalesce(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-03-02 23:19:55 +00:00
|
|
|
require := require.New(t)
|
2018-03-09 00:08:21 +00:00
|
|
|
h := NewDeadlineHeap(context.Background(), 100*time.Millisecond)
|
2018-03-02 23:19:55 +00:00
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
group1 := map[string]time.Time{
|
|
|
|
"1": now.Add(5 * time.Millisecond),
|
|
|
|
"2": now.Add(10 * time.Millisecond),
|
|
|
|
"3": now.Add(20 * time.Millisecond),
|
|
|
|
"4": now.Add(100 * time.Millisecond),
|
|
|
|
}
|
|
|
|
|
|
|
|
group2 := map[string]time.Time{
|
2018-03-09 00:08:21 +00:00
|
|
|
"10": now.Add(350 * time.Millisecond),
|
2018-03-02 23:19:55 +00:00
|
|
|
"11": now.Add(360 * time.Millisecond),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, g := range []map[string]time.Time{group1, group2} {
|
|
|
|
for n, d := range g {
|
|
|
|
h.Watch(n, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var batch []string
|
|
|
|
select {
|
|
|
|
case batch = <-h.NextBatch():
|
2018-04-03 23:51:57 +00:00
|
|
|
case <-time.After(testutil.Timeout(time.Second)):
|
2018-03-02 23:19:55 +00:00
|
|
|
t.Fatal("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Len(batch, len(group1))
|
|
|
|
for nodeID := range group1 {
|
|
|
|
require.Contains(batch, nodeID)
|
|
|
|
}
|
|
|
|
batch = nil
|
|
|
|
|
|
|
|
select {
|
|
|
|
case batch = <-h.NextBatch():
|
2018-04-03 23:51:57 +00:00
|
|
|
case <-time.After(testutil.Timeout(2 * time.Second)):
|
2018-03-02 23:19:55 +00:00
|
|
|
t.Fatal("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Len(batch, len(group2))
|
|
|
|
for nodeID := range group2 {
|
|
|
|
require.Contains(batch, nodeID)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-h.NextBatch():
|
|
|
|
t.Fatal("unexpected batch")
|
2018-04-03 23:51:57 +00:00
|
|
|
case <-time.After(testutil.Timeout(100 * time.Millisecond)):
|
2018-03-02 23:19:55 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-30 16:58:29 +00:00
|
|
|
|
|
|
|
func TestDeadlineHeap_MultipleForce(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-03-30 16:58:29 +00:00
|
|
|
require := require.New(t)
|
|
|
|
h := NewDeadlineHeap(context.Background(), 1*time.Second)
|
|
|
|
|
|
|
|
nodeID := "1"
|
|
|
|
deadline := time.Time{}
|
|
|
|
h.Watch(nodeID, deadline)
|
|
|
|
|
|
|
|
var batch []string
|
|
|
|
select {
|
|
|
|
case batch = <-h.NextBatch():
|
2018-04-03 23:51:57 +00:00
|
|
|
case <-time.After(testutil.Timeout(10 * time.Millisecond)):
|
2018-03-30 16:58:29 +00:00
|
|
|
t.Fatal("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Len(batch, 1)
|
|
|
|
require.Equal(nodeID, batch[0])
|
|
|
|
|
|
|
|
nodeID = "2"
|
|
|
|
h.Watch(nodeID, deadline)
|
|
|
|
select {
|
|
|
|
case batch = <-h.NextBatch():
|
2018-04-03 23:51:57 +00:00
|
|
|
case <-time.After(testutil.Timeout(10 * time.Millisecond)):
|
2018-03-30 16:58:29 +00:00
|
|
|
t.Fatal("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Len(batch, 1)
|
|
|
|
require.Equal(nodeID, batch[0])
|
|
|
|
}
|