2015-01-21 19:07:45 +00:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-01-04 11:17:31 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package retrieval
|
|
|
|
|
2013-01-12 20:58:52 +00:00
|
|
|
import (
|
2014-10-10 13:24:38 +00:00
|
|
|
"net/http"
|
2013-01-12 20:58:52 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2014-10-10 13:24:38 +00:00
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
2013-01-12 20:58:52 +00:00
|
|
|
)
|
2013-01-04 11:17:31 +00:00
|
|
|
|
2014-05-21 17:27:24 +00:00
|
|
|
func testTargetPool(t testing.TB) {
|
2013-01-04 11:17:31 +00:00
|
|
|
type expectation struct {
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
2013-01-12 20:58:52 +00:00
|
|
|
type input struct {
|
2014-12-26 12:37:30 +00:00
|
|
|
url string
|
2013-01-12 20:58:52 +00:00
|
|
|
scheduledFor time.Time
|
|
|
|
}
|
2013-01-04 11:17:31 +00:00
|
|
|
|
2013-01-12 20:58:52 +00:00
|
|
|
type output struct {
|
2014-12-17 18:40:59 +00:00
|
|
|
url string
|
2013-01-12 20:58:52 +00:00
|
|
|
}
|
2013-01-04 11:17:31 +00:00
|
|
|
|
2013-01-12 20:58:52 +00:00
|
|
|
var scenarios = []struct {
|
|
|
|
name string
|
|
|
|
inputs []input
|
2013-02-21 18:48:29 +00:00
|
|
|
outputs []output
|
2013-01-12 20:58:52 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
inputs: []input{},
|
|
|
|
outputs: []output{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single element",
|
|
|
|
inputs: []input{
|
|
|
|
{
|
2014-12-17 18:40:59 +00:00
|
|
|
url: "single1",
|
2013-01-12 20:58:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
outputs: []output{
|
|
|
|
{
|
2014-12-17 18:40:59 +00:00
|
|
|
url: "single1",
|
2013-01-12 20:58:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-07-29 16:28:48 +00:00
|
|
|
name: "plural schedules",
|
2013-01-12 20:58:52 +00:00
|
|
|
inputs: []input{
|
|
|
|
{
|
2014-12-17 18:40:59 +00:00
|
|
|
url: "plural1",
|
2013-01-12 20:58:52 +00:00
|
|
|
},
|
|
|
|
{
|
2014-12-17 18:40:59 +00:00
|
|
|
url: "plural2",
|
2013-01-12 20:58:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
outputs: []output{
|
|
|
|
{
|
2014-12-17 18:40:59 +00:00
|
|
|
url: "plural1",
|
2013-01-12 20:58:52 +00:00
|
|
|
},
|
|
|
|
{
|
2014-12-17 18:40:59 +00:00
|
|
|
url: "plural2",
|
2013-01-12 20:58:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2013-01-04 11:17:31 +00:00
|
|
|
|
2013-01-12 20:58:52 +00:00
|
|
|
for i, scenario := range scenarios {
|
2015-03-15 02:36:15 +00:00
|
|
|
pool := NewTargetPool(nil, nopAppender{}, time.Duration(1))
|
2013-01-04 11:17:31 +00:00
|
|
|
|
|
|
|
for _, input := range scenario.inputs {
|
2013-01-13 09:46:55 +00:00
|
|
|
target := target{
|
2014-12-26 12:37:30 +00:00
|
|
|
url: input.url,
|
2014-10-10 13:24:38 +00:00
|
|
|
newBaseLabels: make(chan clientmodel.LabelSet, 1),
|
|
|
|
httpClient: &http.Client{},
|
2013-01-04 11:17:31 +00:00
|
|
|
}
|
2013-02-22 20:07:35 +00:00
|
|
|
pool.addTarget(&target)
|
2013-01-12 20:58:52 +00:00
|
|
|
}
|
2013-01-13 09:46:55 +00:00
|
|
|
|
2014-12-17 18:40:59 +00:00
|
|
|
if len(pool.targetsByURL) != len(scenario.outputs) {
|
|
|
|
t.Errorf("%s %d. expected TargetPool size to be %d but was %d", scenario.name, i, len(scenario.outputs), len(pool.targetsByURL))
|
2013-01-12 20:58:52 +00:00
|
|
|
} else {
|
|
|
|
for j, output := range scenario.outputs {
|
2014-12-17 18:40:59 +00:00
|
|
|
if target, ok := pool.targetsByURL[output.url]; !ok {
|
|
|
|
t.Errorf("%s %d.%d. expected Target url to be %s but was %s", scenario.name, i, j, output.url, target.URL())
|
2013-01-12 20:58:52 +00:00
|
|
|
}
|
2013-01-13 09:46:55 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 18:40:59 +00:00
|
|
|
if len(pool.targetsByURL) != len(scenario.outputs) {
|
|
|
|
t.Errorf("%s %d. expected to repopulated with %d elements, got %d", scenario.name, i, len(scenario.outputs), len(pool.targetsByURL))
|
2013-01-12 20:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-13 09:46:55 +00:00
|
|
|
|
|
|
|
func TestTargetPool(t *testing.T) {
|
|
|
|
testTargetPool(t)
|
|
|
|
}
|
|
|
|
|
2013-02-22 20:07:35 +00:00
|
|
|
func TestTargetPoolReplaceTargets(t *testing.T) {
|
2015-03-15 02:36:15 +00:00
|
|
|
pool := NewTargetPool(nil, nopAppender{}, time.Duration(1))
|
2013-02-22 20:07:35 +00:00
|
|
|
oldTarget1 := &target{
|
2014-12-26 12:37:30 +00:00
|
|
|
url: "example1",
|
2015-03-07 20:50:02 +00:00
|
|
|
state: Unhealthy,
|
2014-12-03 17:07:23 +00:00
|
|
|
scraperStopping: make(chan struct{}),
|
|
|
|
scraperStopped: make(chan struct{}),
|
|
|
|
newBaseLabels: make(chan clientmodel.LabelSet, 1),
|
|
|
|
httpClient: &http.Client{},
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|
|
|
|
oldTarget2 := &target{
|
2014-12-26 12:37:30 +00:00
|
|
|
url: "example2",
|
2015-03-07 20:50:02 +00:00
|
|
|
state: Unhealthy,
|
2014-12-03 17:07:23 +00:00
|
|
|
scraperStopping: make(chan struct{}),
|
|
|
|
scraperStopped: make(chan struct{}),
|
|
|
|
newBaseLabels: make(chan clientmodel.LabelSet, 1),
|
|
|
|
httpClient: &http.Client{},
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|
|
|
|
newTarget1 := &target{
|
2014-12-26 12:37:30 +00:00
|
|
|
url: "example1",
|
2015-03-07 20:50:02 +00:00
|
|
|
state: Healthy,
|
2014-12-03 17:07:23 +00:00
|
|
|
scraperStopping: make(chan struct{}),
|
|
|
|
scraperStopped: make(chan struct{}),
|
|
|
|
newBaseLabels: make(chan clientmodel.LabelSet, 1),
|
|
|
|
httpClient: &http.Client{},
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|
|
|
|
newTarget2 := &target{
|
2014-12-26 12:37:30 +00:00
|
|
|
url: "example3",
|
2015-03-07 20:50:02 +00:00
|
|
|
state: Healthy,
|
2014-12-03 17:07:23 +00:00
|
|
|
scraperStopping: make(chan struct{}),
|
|
|
|
scraperStopped: make(chan struct{}),
|
|
|
|
newBaseLabels: make(chan clientmodel.LabelSet, 1),
|
|
|
|
httpClient: &http.Client{},
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pool.addTarget(oldTarget1)
|
|
|
|
pool.addTarget(oldTarget2)
|
|
|
|
|
2014-07-29 18:31:11 +00:00
|
|
|
pool.ReplaceTargets([]Target{newTarget1, newTarget2})
|
2013-02-22 20:07:35 +00:00
|
|
|
|
2014-12-17 18:40:59 +00:00
|
|
|
if len(pool.targetsByURL) != 2 {
|
|
|
|
t.Errorf("Expected 2 elements in pool, had %d", len(pool.targetsByURL))
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 18:40:59 +00:00
|
|
|
if pool.targetsByURL["example1"].State() != oldTarget1.State() {
|
2014-07-29 18:31:11 +00:00
|
|
|
t.Errorf("target1 channel has changed")
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|
2014-12-17 18:40:59 +00:00
|
|
|
if pool.targetsByURL["example3"].State() == oldTarget2.State() {
|
2014-07-29 18:31:11 +00:00
|
|
|
t.Errorf("newTarget2 channel same as oldTarget2's")
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|
2014-07-29 18:31:11 +00:00
|
|
|
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|
|
|
|
|
2013-01-13 09:46:55 +00:00
|
|
|
func BenchmarkTargetPool(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testTargetPool(b)
|
|
|
|
}
|
|
|
|
}
|