mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 23:24:05 -08:00
Add tests for new timestamp/duration functions.
...and fix the first bugs in them where they truncate precision below a second.
This commit is contained in:
parent
cb816ea14a
commit
a68b880c27
|
@ -48,7 +48,7 @@ func parseTimestampOrNow(t string) (clientmodel.Timestamp, error) {
|
|||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return clientmodel.TimestampFromUnixNano(int64(tFloat) * int64(time.Second/time.Nanosecond)), nil
|
||||
return clientmodel.TimestampFromUnixNano(int64(tFloat * float64(time.Second/time.Nanosecond))), nil
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,7 +57,7 @@ func parseDuration(d string) (time.Duration, error) {
|
|||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return time.Duration(dFloat) * (time.Second / time.Nanosecond), nil
|
||||
return time.Duration(dFloat * float64(time.Second/time.Nanosecond)), nil
|
||||
}
|
||||
|
||||
// Query handles the /api/query endpoint.
|
||||
|
|
67
web/api/query_test.go
Normal file
67
web/api/query_test.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
// Copyright 2015 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.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
clientmodel "github.com/prometheus/client_golang/model"
|
||||
)
|
||||
|
||||
func TestParseTimestampOrNow(t *testing.T) {
|
||||
ts, err := parseTimestampOrNow("")
|
||||
if err != nil {
|
||||
t.Fatalf("err = %s; want nil", err)
|
||||
}
|
||||
now := clientmodel.Now()
|
||||
if now.Sub(ts) > time.Second || now.Sub(ts) < 0 {
|
||||
t.Fatalf("ts = %v; want %v <= ts <= %v", ts, now.Sub(ts), now)
|
||||
}
|
||||
|
||||
ts, err = parseTimestampOrNow("1426956073.12345")
|
||||
if err != nil {
|
||||
t.Fatalf("err = %s; want nil", err)
|
||||
}
|
||||
expTS := clientmodel.TimestampFromUnixNano(1426956073123000000)
|
||||
if !ts.Equal(expTS) {
|
||||
t.Fatalf("ts = %v; want %v", ts, expTS)
|
||||
}
|
||||
|
||||
_, err = parseTimestampOrNow("123.45foo")
|
||||
if err == nil {
|
||||
t.Fatalf("err = nil; want %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDuration(t *testing.T) {
|
||||
_, err := parseDuration("")
|
||||
if err == nil {
|
||||
t.Fatalf("err = nil; want %s", err)
|
||||
}
|
||||
|
||||
_, err = parseDuration("1234.56foo")
|
||||
if err == nil {
|
||||
t.Fatalf("err = nil; want %s", err)
|
||||
}
|
||||
|
||||
d, err := parseDuration("1234.56789")
|
||||
if err != nil {
|
||||
t.Fatalf("err = %s; want nil", err)
|
||||
}
|
||||
expD := time.Duration(1234.56789 * float64(time.Second))
|
||||
if d != expD {
|
||||
t.Fatalf("d = %v; want %v", d, expD)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue