You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by ne...@apache.org on 2017/01/05 19:47:07 UTC
[06/11] incubator-trafficcontrol git commit: Add TM2
cache.TestStatsMarshall
Add TM2 cache.TestStatsMarshall
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/a595c104
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/a595c104
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/a595c104
Branch: refs/heads/master
Commit: a595c104bcf774da7098fa35c535045abf48e628
Parents: e454587
Author: Robert Butts <ro...@gmail.com>
Authored: Thu Dec 8 15:24:44 2016 -0700
Committer: Dave Neuman <ne...@apache.org>
Committed: Thu Jan 5 12:46:32 2017 -0700
----------------------------------------------------------------------
.../traffic_monitor/cache/cache_test.go | 55 ++++++++++++++++++++
.../traffic_monitor/srvhttp/srvhttp.go | 4 +-
2 files changed, 58 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/a595c104/traffic_monitor/experimental/traffic_monitor/cache/cache_test.go
----------------------------------------------------------------------
diff --git a/traffic_monitor/experimental/traffic_monitor/cache/cache_test.go b/traffic_monitor/experimental/traffic_monitor/cache/cache_test.go
index d08e87b..71e7a78 100644
--- a/traffic_monitor/experimental/traffic_monitor/cache/cache_test.go
+++ b/traffic_monitor/experimental/traffic_monitor/cache/cache_test.go
@@ -20,9 +20,14 @@ package cache
*/
import (
+ "encoding/json"
+ "net/url"
"testing"
+ "time"
+ "github.com/apache/incubator-trafficcontrol/traffic_monitor/experimental/traffic_monitor/enum"
"github.com/apache/incubator-trafficcontrol/traffic_monitor/experimental/traffic_monitor/peer"
+ "github.com/apache/incubator-trafficcontrol/traffic_monitor/experimental/traffic_monitor/srvhttp"
todata "github.com/apache/incubator-trafficcontrol/traffic_monitor/experimental/traffic_monitor/trafficopsdata"
)
@@ -34,3 +39,53 @@ func TestHandlerPrecompute(t *testing.T) {
t.Errorf("expected NewPrecomputeHandler().Precompute() true, actual false")
}
}
+
+type DummyFilterNever struct {
+}
+
+func (f DummyFilterNever) UseStat(name string) bool {
+ return false
+}
+
+func (f DummyFilterNever) UseCache(name enum.CacheName) bool {
+ return false
+}
+
+func (f DummyFilterNever) WithinStatHistoryMax(i int) bool {
+ return false
+}
+
+func TestStatsMarshall(t *testing.T) {
+ hist := randResultHistory()
+ filter := DummyFilterNever{}
+ params := url.Values{}
+ beforeStatsMarshall := time.Now()
+ bytes, err := StatsMarshall(hist, filter, params)
+ afterStatsMarshall := time.Now()
+ if err != nil {
+ t.Fatalf("StatsMarshall return expected nil err, actual err: %v", err)
+ }
+ // if len(bytes) > 0 {
+ // t.Errorf("expected empty bytes, actual: %v", string(bytes))
+ // }
+
+ stats := Stats{}
+ if err := json.Unmarshal(bytes, &stats); err != nil {
+ t.Fatalf("unmarshalling expected nil err, actual err: %v", err)
+ }
+
+ if stats.CommonAPIData.QueryParams != "" {
+ t.Errorf(`unmarshalling stats.CommonAPIData.QueryParams expected "", actual %v`, stats.CommonAPIData.QueryParams)
+ }
+
+ statsDate, err := time.Parse(srvhttp.CommonAPIDataDateFormat, stats.CommonAPIData.DateStr)
+ if err != nil {
+ t.Errorf(`stats.CommonAPIData.DateStr expected format %v, actual %v`, srvhttp.CommonAPIDataDateFormat, stats.CommonAPIData.DateStr)
+ }
+ if beforeStatsMarshall.Round(time.Second).After(statsDate) || statsDate.After(afterStatsMarshall.Round(time.Second)) { // round to second, because CommonAPIDataDateFormat is second-precision
+ t.Errorf(`unmarshalling stats.CommonAPIData.DateStr expected between %v and %v, actual %v`, beforeStatsMarshall, afterStatsMarshall, stats.CommonAPIData.DateStr)
+ }
+ if len(stats.Caches) > 0 {
+ t.Errorf(`unmarshalling stats.Caches expected empty, actual %+v`, stats.Caches)
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/a595c104/traffic_monitor/experimental/traffic_monitor/srvhttp/srvhttp.go
----------------------------------------------------------------------
diff --git a/traffic_monitor/experimental/traffic_monitor/srvhttp/srvhttp.go b/traffic_monitor/experimental/traffic_monitor/srvhttp/srvhttp.go
index 0b9e261..564f5b3 100644
--- a/traffic_monitor/experimental/traffic_monitor/srvhttp/srvhttp.go
+++ b/traffic_monitor/experimental/traffic_monitor/srvhttp/srvhttp.go
@@ -134,9 +134,11 @@ func ParametersStr(params url.Values) string {
return pp
}
+const CommonAPIDataDateFormat = "Mon Jan 02 15:04:05 UTC 2006"
+
// DateStr returns the given time in the format expected by Traffic Monitor 1.0 API users
func DateStr(t time.Time) string {
- return t.UTC().Format("Mon Jan 02 15:04:05 UTC 2006")
+ return t.UTC().Format(CommonAPIDataDateFormat)
}
func (s Server) handleRootFunc() (http.HandlerFunc, error) {