2020-01-20 05:17:11 -08:00
|
|
|
// Copyright 2020 The Prometheus Authors
|
|
|
|
// 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.
|
|
|
|
//
|
2021-08-25 07:35:30 -07:00
|
|
|
//go:build !windows
|
2020-01-20 05:17:11 -08:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-11-10 02:33:46 -08:00
|
|
|
"fmt"
|
2020-01-20 05:17:11 -08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2021-11-10 02:33:46 -08:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2020-01-20 05:17:11 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// As soon as prometheus starts responding to http request it should be able to
|
|
|
|
// accept Interrupt signals for a graceful shutdown.
|
|
|
|
func TestStartupInterrupt(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping test in short mode.")
|
|
|
|
}
|
|
|
|
|
2021-11-10 02:33:46 -08:00
|
|
|
port := fmt.Sprintf(":%d", testutil.RandomUnprivilegedPort(t))
|
|
|
|
|
|
|
|
prom := exec.Command(promPath, "-test.main", "--config.file="+promConfig, "--storage.tsdb.path="+t.TempDir(), "--web.listen-address=0.0.0.0"+port)
|
2020-01-20 05:17:11 -08:00
|
|
|
err := prom.Start()
|
|
|
|
if err != nil {
|
2021-11-10 01:43:50 -08:00
|
|
|
t.Fatalf("execution error: %v", err)
|
2020-01-20 05:17:11 -08:00
|
|
|
}
|
|
|
|
|
2020-12-10 01:09:21 -08:00
|
|
|
done := make(chan error, 1)
|
2020-01-20 05:17:11 -08:00
|
|
|
go func() {
|
|
|
|
done <- prom.Wait()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var startedOk bool
|
|
|
|
var stoppedErr error
|
|
|
|
|
2021-11-10 02:33:46 -08:00
|
|
|
url := "http://localhost" + port + "/graph"
|
|
|
|
|
2020-01-20 05:17:11 -08:00
|
|
|
Loop:
|
|
|
|
for x := 0; x < 10; x++ {
|
2022-06-27 07:15:51 -07:00
|
|
|
// error=nil means prometheus has started, so we can send the interrupt
|
2020-01-20 05:17:11 -08:00
|
|
|
// signal and wait for the graceful shutdown.
|
2021-11-10 02:33:46 -08:00
|
|
|
if _, err := http.Get(url); err == nil {
|
2020-01-20 05:17:11 -08:00
|
|
|
startedOk = true
|
|
|
|
prom.Process.Signal(os.Interrupt)
|
|
|
|
select {
|
|
|
|
case stoppedErr = <-done:
|
|
|
|
break Loop
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
}
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !startedOk {
|
2021-11-10 01:43:50 -08:00
|
|
|
t.Fatal("prometheus didn't start in the specified timeout")
|
2020-01-20 05:17:11 -08:00
|
|
|
}
|
style: Replace `else if` cascades with `switch`
Wiser coders than myself have come to the conclusion that a `switch`
statement is almost always superior to a statement that includes any
`else if`.
The exceptions that I have found in our codebase are just these two:
* The `if else` is followed by an additional statement before the next
condition (separated by a `;`).
* The whole thing is within a `for` loop and `break` statements are
used. In this case, using `switch` would require tagging the `for`
loop, which probably tips the balance.
Why are `switch` statements more readable?
For one, fewer curly braces. But more importantly, the conditions all
have the same alignment, so the whole thing follows the natural flow
of going down a list of conditions. With `else if`, in contrast, all
conditions but the first are "hidden" behind `} else if `, harder to
spot and (for no good reason) presented differently from the first
condition.
I'm sure the aforemention wise coders can list even more reasons.
In any case, I like it so much that I have found myself recommending
it in code reviews. I would like to make it a habit in our code base,
without making it a hard requirement that we would test on the CI. But
for that, there has to be a role model, so this commit eliminates all
`if else` occurrences, unless it is autogenerated code or fits one of
the exceptions above.
Signed-off-by: beorn7 <beorn@grafana.com>
2023-04-12 07:14:31 -07:00
|
|
|
switch err := prom.Process.Kill(); {
|
|
|
|
case err == nil:
|
2020-01-20 05:17:11 -08:00
|
|
|
t.Errorf("prometheus didn't shutdown gracefully after sending the Interrupt signal")
|
style: Replace `else if` cascades with `switch`
Wiser coders than myself have come to the conclusion that a `switch`
statement is almost always superior to a statement that includes any
`else if`.
The exceptions that I have found in our codebase are just these two:
* The `if else` is followed by an additional statement before the next
condition (separated by a `;`).
* The whole thing is within a `for` loop and `break` statements are
used. In this case, using `switch` would require tagging the `for`
loop, which probably tips the balance.
Why are `switch` statements more readable?
For one, fewer curly braces. But more importantly, the conditions all
have the same alignment, so the whole thing follows the natural flow
of going down a list of conditions. With `else if`, in contrast, all
conditions but the first are "hidden" behind `} else if `, harder to
spot and (for no good reason) presented differently from the first
condition.
I'm sure the aforemention wise coders can list even more reasons.
In any case, I like it so much that I have found myself recommending
it in code reviews. I would like to make it a habit in our code base,
without making it a hard requirement that we would test on the CI. But
for that, there has to be a role model, so this commit eliminates all
`if else` occurrences, unless it is autogenerated code or fits one of
the exceptions above.
Signed-off-by: beorn7 <beorn@grafana.com>
2023-04-12 07:14:31 -07:00
|
|
|
case stoppedErr != nil && stoppedErr.Error() != "signal: interrupt":
|
|
|
|
// TODO: find a better way to detect when the process didn't exit as expected!
|
2021-11-10 01:43:50 -08:00
|
|
|
t.Errorf("prometheus exited with an unexpected error: %v", stoppedErr)
|
2020-01-20 05:17:11 -08:00
|
|
|
}
|
|
|
|
}
|