| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package tsmon | 5 package tsmon |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "net/http" | 8 "net/http" |
| 9 "net/http/httptest" | 9 "net/http/httptest" |
| 10 "testing" | 10 "testing" |
| 11 "time" | 11 "time" |
| 12 | 12 |
| 13 "github.com/julienschmidt/httprouter" | |
| 14 "github.com/luci/gae/service/datastore" | 13 "github.com/luci/gae/service/datastore" |
| 15 "github.com/luci/luci-go/common/clock" | 14 "github.com/luci/luci-go/common/clock" |
| 16 "github.com/luci/luci-go/common/tsmon" | 15 "github.com/luci/luci-go/common/tsmon" |
| 17 "github.com/luci/luci-go/common/tsmon/metric" | 16 "github.com/luci/luci-go/common/tsmon/metric" |
| 17 "github.com/luci/luci-go/server/router" |
| 18 "golang.org/x/net/context" | 18 "golang.org/x/net/context" |
| 19 | 19 |
| 20 . "github.com/smartystreets/goconvey/convey" | 20 . "github.com/smartystreets/goconvey/convey" |
| 21 ) | 21 ) |
| 22 | 22 |
| 23 func flushNowWithMiddleware(c context.Context, state *State) { | 23 func flushNowWithMiddleware(c context.Context, state *State) { |
| 24 ds := datastore.Get(c) | 24 ds := datastore.Get(c) |
| 25 | 25 |
| 26 i := instance{ | 26 i := instance{ |
| 27 ID: instanceEntityID(c), | 27 ID: instanceEntityID(c), |
| 28 TaskNum: 0, | 28 TaskNum: 0, |
| 29 LastUpdated: clock.Now(c).Add(-2 * time.Minute).UTC(), | 29 LastUpdated: clock.Now(c).Add(-2 * time.Minute).UTC(), |
| 30 } | 30 } |
| 31 So(ds.Put(&i), ShouldBeNil) | 31 So(ds.Put(&i), ShouldBeNil) |
| 32 | 32 |
| 33 state.lastFlushed = clock.Now(c).Add(-2 * time.Minute) | 33 state.lastFlushed = clock.Now(c).Add(-2 * time.Minute) |
| 34 | 34 |
| 35 rec := httptest.NewRecorder() | 35 rec := httptest.NewRecorder() |
| 36 » state.Middleware(func(c context.Context, rw http.ResponseWriter, r *http
.Request, p httprouter.Params) {})(c, rec, &http.Request{}, nil) | 36 » router.RunMiddleware( |
| 37 » » &router.Context{Context: c, Writer: rec, Request: &http.Request{
}}, |
| 38 » » router.MiddlewareChain{state.Middleware}, |
| 39 » » nil, |
| 40 » ) |
| 37 So(rec.Code, ShouldEqual, http.StatusOK) | 41 So(rec.Code, ShouldEqual, http.StatusOK) |
| 38 } | 42 } |
| 39 | 43 |
| 40 func TestGlobalCallbacks(t *testing.T) { | 44 func TestGlobalCallbacks(t *testing.T) { |
| 41 Convey("Global callbacks", t, func() { | 45 Convey("Global callbacks", t, func() { |
| 42 c, _ := buildGAETestContext() | 46 c, _ := buildGAETestContext() |
| 43 state, mon := buildTestState() | 47 state, mon := buildTestState() |
| 44 | 48 |
| 45 m := metric.NewCallbackStringIn(c, "foo", "") | 49 m := metric.NewCallbackStringIn(c, "foo", "") |
| 46 | 50 |
| 47 tsmon.RegisterGlobalCallbackIn(c, func(c context.Context) { | 51 tsmon.RegisterGlobalCallbackIn(c, func(c context.Context) { |
| 48 m.Set(c, "bar") | 52 m.Set(c, "bar") |
| 49 }, m) | 53 }, m) |
| 50 | 54 |
| 51 Convey("are not run on flush", func() { | 55 Convey("are not run on flush", func() { |
| 52 flushNowWithMiddleware(c, state) | 56 flushNowWithMiddleware(c, state) |
| 53 val, err := tsmon.Store(c).Get(c, m, time.Time{}, []inte
rface{}{}) | 57 val, err := tsmon.Store(c).Get(c, m, time.Time{}, []inte
rface{}{}) |
| 54 So(err, ShouldBeNil) | 58 So(err, ShouldBeNil) |
| 55 So(val, ShouldBeNil) | 59 So(val, ShouldBeNil) |
| 56 }) | 60 }) |
| 57 | 61 |
| 58 Convey("but are run by housekeeping", func() { | 62 Convey("but are run by housekeeping", func() { |
| 59 state.checkSettings(c) // initialize the in-memory store | 63 state.checkSettings(c) // initialize the in-memory store |
| 60 s := tsmon.Store(c) | 64 s := tsmon.Store(c) |
| 61 | 65 |
| 62 rec := httptest.NewRecorder() | 66 rec := httptest.NewRecorder() |
| 63 » » » housekeepingHandler(c, rec, &http.Request{}, nil) | 67 » » » housekeepingHandler(&router.Context{ |
| 68 » » » » Context: c, |
| 69 » » » » Writer: rec, |
| 70 » » » » Request: &http.Request{}, |
| 71 » » » }) |
| 64 So(rec.Code, ShouldEqual, http.StatusOK) | 72 So(rec.Code, ShouldEqual, http.StatusOK) |
| 65 | 73 |
| 66 val, err := s.Get(c, m, time.Time{}, []interface{}{}) | 74 val, err := s.Get(c, m, time.Time{}, []interface{}{}) |
| 67 So(err, ShouldBeNil) | 75 So(err, ShouldBeNil) |
| 68 So(val, ShouldEqual, "bar") | 76 So(val, ShouldEqual, "bar") |
| 69 | 77 |
| 70 Convey("and are reset on flush", func() { | 78 Convey("and are reset on flush", func() { |
| 71 flushNowWithMiddleware(c, state) | 79 flushNowWithMiddleware(c, state) |
| 72 | 80 |
| 73 val, err = s.Get(c, m, time.Time{}, []interface{
}{}) | 81 val, err = s.Get(c, m, time.Time{}, []interface{
}{}) |
| 74 So(err, ShouldBeNil) | 82 So(err, ShouldBeNil) |
| 75 So(val, ShouldBeNil) | 83 So(val, ShouldBeNil) |
| 76 | 84 |
| 77 So(len(mon.Cells), ShouldEqual, 1) | 85 So(len(mon.Cells), ShouldEqual, 1) |
| 78 So(len(mon.Cells[0]), ShouldEqual, 1) | 86 So(len(mon.Cells[0]), ShouldEqual, 1) |
| 79 So(mon.Cells[0][0].Value, ShouldEqual, "bar") | 87 So(mon.Cells[0][0].Value, ShouldEqual, "bar") |
| 80 }) | 88 }) |
| 81 }) | 89 }) |
| 82 }) | 90 }) |
| 83 } | 91 } |
| OLD | NEW |