Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(570)

Side by Side Diff: appengine/tsmon/global_callback_test.go

Issue 2043423004: Make HTTP middleware easier to use (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Update tests Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.ChainHandlers(func(ctx *router.Context) {
37 » » ctx.Context, ctx.Writer, ctx.Request = c, rec, &http.Request{}
38 » }, state.Middleware())()
37 So(rec.Code, ShouldEqual, http.StatusOK) 39 So(rec.Code, ShouldEqual, http.StatusOK)
38 } 40 }
39 41
40 func TestGlobalCallbacks(t *testing.T) { 42 func TestGlobalCallbacks(t *testing.T) {
41 Convey("Global callbacks", t, func() { 43 Convey("Global callbacks", t, func() {
42 c, _ := buildGAETestContext() 44 c, _ := buildGAETestContext()
43 state, mon := buildTestState() 45 state, mon := buildTestState()
44 46
45 m := metric.NewCallbackStringIn(c, "foo", "") 47 m := metric.NewCallbackStringIn(c, "foo", "")
46 48
47 tsmon.RegisterGlobalCallbackIn(c, func(c context.Context) { 49 tsmon.RegisterGlobalCallbackIn(c, func(c context.Context) {
48 m.Set(c, "bar") 50 m.Set(c, "bar")
49 }, m) 51 }, m)
50 52
51 Convey("are not run on flush", func() { 53 Convey("are not run on flush", func() {
52 flushNowWithMiddleware(c, state) 54 flushNowWithMiddleware(c, state)
53 val, err := tsmon.Store(c).Get(c, m, time.Time{}, []inte rface{}{}) 55 val, err := tsmon.Store(c).Get(c, m, time.Time{}, []inte rface{}{})
54 So(err, ShouldBeNil) 56 So(err, ShouldBeNil)
55 So(val, ShouldBeNil) 57 So(val, ShouldBeNil)
56 }) 58 })
57 59
58 Convey("but are run by housekeeping", func() { 60 Convey("but are run by housekeeping", func() {
59 state.checkSettings(c) // initialize the in-memory store 61 state.checkSettings(c) // initialize the in-memory store
60 s := tsmon.Store(c) 62 s := tsmon.Store(c)
61 63
62 rec := httptest.NewRecorder() 64 rec := httptest.NewRecorder()
63 » » » housekeepingHandler(c, rec, &http.Request{}, nil) 65 » » » router.ChainHandlers(func(ctx *router.Context) {
66 » » » » ctx.Context, ctx.Writer, ctx.Request = c, rec, & http.Request{}
67 » » » }, housekeepingHandler)()
64 So(rec.Code, ShouldEqual, http.StatusOK) 68 So(rec.Code, ShouldEqual, http.StatusOK)
65 69
66 val, err := s.Get(c, m, time.Time{}, []interface{}{}) 70 val, err := s.Get(c, m, time.Time{}, []interface{}{})
67 So(err, ShouldBeNil) 71 So(err, ShouldBeNil)
68 So(val, ShouldEqual, "bar") 72 So(val, ShouldEqual, "bar")
69 73
70 Convey("and are reset on flush", func() { 74 Convey("and are reset on flush", func() {
71 flushNowWithMiddleware(c, state) 75 flushNowWithMiddleware(c, state)
72 76
73 val, err = s.Get(c, m, time.Time{}, []interface{ }{}) 77 val, err = s.Get(c, m, time.Time{}, []interface{ }{})
74 So(err, ShouldBeNil) 78 So(err, ShouldBeNil)
75 So(val, ShouldBeNil) 79 So(val, ShouldBeNil)
76 80
77 So(len(mon.Cells), ShouldEqual, 1) 81 So(len(mon.Cells), ShouldEqual, 1)
78 So(len(mon.Cells[0]), ShouldEqual, 1) 82 So(len(mon.Cells[0]), ShouldEqual, 1)
79 So(mon.Cells[0][0].Value, ShouldEqual, "bar") 83 So(mon.Cells[0][0].Value, ShouldEqual, "bar")
80 }) 84 })
81 }) 85 })
82 }) 86 })
83 } 87 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698