open-nomad/scheduler/select_test.go

95 lines
1.5 KiB
Go
Raw Normal View History

2015-08-13 17:05:54 +00:00
package scheduler
import (
"testing"
"github.com/hashicorp/nomad/nomad/mock"
)
func TestLimitIterator(t *testing.T) {
2015-08-13 18:33:58 +00:00
_, ctx := testContext(t)
2015-08-13 17:05:54 +00:00
nodes := []*RankedNode{
2017-09-26 22:26:33 +00:00
{
2015-08-13 17:05:54 +00:00
Node: mock.Node(),
Score: 1,
},
2017-09-26 22:26:33 +00:00
{
2015-08-13 17:05:54 +00:00
Node: mock.Node(),
Score: 2,
},
2017-09-26 22:26:33 +00:00
{
2015-08-13 17:05:54 +00:00
Node: mock.Node(),
Score: 3,
},
}
static := NewStaticRankIterator(ctx, nodes)
limit := NewLimitIterator(ctx, static, 1)
limit.SetLimit(2)
2015-08-13 17:05:54 +00:00
2015-08-13 19:02:42 +00:00
out := collectRanked(limit)
2015-08-13 17:05:54 +00:00
if len(out) != 2 {
t.Fatalf("bad: %v", out)
}
if out[0] != nodes[0] && out[1] != nodes[1] {
t.Fatalf("bad: %v", out)
}
2015-08-13 22:01:02 +00:00
out = collectRanked(limit)
if len(out) != 0 {
t.Fatalf("bad: %v", out)
}
limit.Reset()
out = collectRanked(limit)
if len(out) != 2 {
t.Fatalf("bad: %v", out)
}
if out[0] != nodes[2] && out[1] != nodes[0] {
t.Fatalf("bad: %v", out)
}
2015-08-13 17:05:54 +00:00
}
func TestMaxScoreIterator(t *testing.T) {
2015-08-13 18:33:58 +00:00
_, ctx := testContext(t)
2015-08-13 17:05:54 +00:00
nodes := []*RankedNode{
2017-09-26 22:26:33 +00:00
{
2015-08-13 17:05:54 +00:00
Node: mock.Node(),
Score: 1,
},
2017-09-26 22:26:33 +00:00
{
2015-08-13 17:05:54 +00:00
Node: mock.Node(),
Score: 2,
},
2017-09-26 22:26:33 +00:00
{
2015-08-13 17:05:54 +00:00
Node: mock.Node(),
Score: 3,
},
}
static := NewStaticRankIterator(ctx, nodes)
max := NewMaxScoreIterator(ctx, static)
2015-08-13 19:02:42 +00:00
out := collectRanked(max)
2015-08-13 17:05:54 +00:00
if len(out) != 1 {
t.Fatalf("bad: %v", out)
}
if out[0] != nodes[2] {
t.Fatalf("bad: %v", out)
}
2015-08-13 22:01:02 +00:00
out = collectRanked(max)
if len(out) != 0 {
t.Fatalf("bad: %v", out)
}
max.Reset()
out = collectRanked(max)
if len(out) != 1 {
t.Fatalf("bad: %v", out)
}
if out[0] != nodes[2] {
t.Fatalf("bad: %v", out)
}
2015-08-13 17:05:54 +00:00
}