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

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

Issue 2043423004: Make HTTP middleware easier to use (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: gaemiddleware: add middleware func for WithProd 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
« no previous file with comments | « appengine/tsmon/middleware.go ('k') | appengine/tumble/service.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/golang/protobuf/proto" 13 "github.com/golang/protobuf/proto"
14 "github.com/julienschmidt/httprouter"
15 "github.com/luci/gae/service/datastore" 14 "github.com/luci/gae/service/datastore"
16 "github.com/luci/luci-go/common/tsmon" 15 "github.com/luci/luci-go/common/tsmon"
17 "github.com/luci/luci-go/common/tsmon/field" 16 "github.com/luci/luci-go/common/tsmon/field"
18 "github.com/luci/luci-go/common/tsmon/store" 17 "github.com/luci/luci-go/common/tsmon/store"
19 "github.com/luci/luci-go/common/tsmon/store/storetest" 18 "github.com/luci/luci-go/common/tsmon/store/storetest"
20 "github.com/luci/luci-go/common/tsmon/target" 19 "github.com/luci/luci-go/common/tsmon/target"
21 "github.com/luci/luci-go/common/tsmon/types" 20 "github.com/luci/luci-go/common/tsmon/types"
22 » "golang.org/x/net/context" 21 » "github.com/luci/luci-go/server/router"
23 22
24 . "github.com/smartystreets/goconvey/convey" 23 . "github.com/smartystreets/goconvey/convey"
25 ) 24 )
26 25
27 func TestMiddleware(t *testing.T) { 26 func TestMiddleware(t *testing.T) {
28 t.Parallel() 27 t.Parallel()
29 metric := &storetest.FakeMetric{"m", "", []field.Field{}, types.Cumulati veIntType} 28 metric := &storetest.FakeMetric{"m", "", []field.Field{}, types.Cumulati veIntType}
30 29
31 » f := func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) { 30 » f := func(c *router.Context) {
32 » » So(store.IsNilStore(tsmon.Store(c)), ShouldBeFalse) 31 » » So(store.IsNilStore(tsmon.Store(c.Context)), ShouldBeFalse)
33 32 » » tsmon.Register(c.Context, metric)
34 » » tsmon.Register(c, metric) 33 » » So(tsmon.Store(c.Context).Incr(c.Context, metric, time.Time{}, [ ]interface{}{}, int64(1)), ShouldBeNil)
35 » » So(tsmon.Store(c).Incr(c, metric, time.Time{}, []interface{}{}, int64(1)), ShouldBeNil)
36 } 34 }
37 35
38 Convey("Creates instance entity", t, func() { 36 Convey("Creates instance entity", t, func() {
39 c, _ := buildGAETestContext() 37 c, _ := buildGAETestContext()
40 state, monitor := buildTestState() 38 state, monitor := buildTestState()
41 ds := datastore.Get(c) 39 ds := datastore.Get(c)
42 40
43 exists, err := ds.Exists(ds.NewKey("Instance", instanceEntityID( c), 0, nil)) 41 exists, err := ds.Exists(ds.NewKey("Instance", instanceEntityID( c), 0, nil))
44 So(err, ShouldBeNil) 42 So(err, ShouldBeNil)
45 So(exists.All(), ShouldBeFalse) 43 So(exists.All(), ShouldBeFalse)
46 44
47 rec := httptest.NewRecorder() 45 rec := httptest.NewRecorder()
48 » » state.Middleware(f)(c, rec, &http.Request{}, nil) 46 » » router.RunMiddleware(
47 » » » &router.Context{Context: c, Writer: rec, Request: &http. Request{}},
48 » » » router.MiddlewareChain{state.Middleware},
49 » » » f,
50 » » )
49 So(rec.Code, ShouldEqual, http.StatusOK) 51 So(rec.Code, ShouldEqual, http.StatusOK)
50 52
51 exists, err = ds.Exists(ds.NewKey("Instance", instanceEntityID(c ), 0, nil)) 53 exists, err = ds.Exists(ds.NewKey("Instance", instanceEntityID(c ), 0, nil))
52 So(err, ShouldBeNil) 54 So(err, ShouldBeNil)
53 So(exists.All(), ShouldBeTrue) 55 So(exists.All(), ShouldBeTrue)
54 56
55 // Shouldn't flush since the instance entity doesn't have a task number yet. 57 // Shouldn't flush since the instance entity doesn't have a task number yet.
56 So(len(monitor.Cells), ShouldEqual, 0) 58 So(len(monitor.Cells), ShouldEqual, 0)
57 }) 59 })
58 60
59 Convey("Flushes after 2 minutes", t, func() { 61 Convey("Flushes after 2 minutes", t, func() {
60 c, clock := buildGAETestContext() 62 c, clock := buildGAETestContext()
61 state, monitor := buildTestState() 63 state, monitor := buildTestState()
62 64
63 ds := datastore.Get(c) 65 ds := datastore.Get(c)
64 66
65 i := instance{ 67 i := instance{
66 ID: instanceEntityID(c), 68 ID: instanceEntityID(c),
67 TaskNum: 0, 69 TaskNum: 0,
68 LastUpdated: clock.Now().Add(-2 * time.Minute).UTC(), 70 LastUpdated: clock.Now().Add(-2 * time.Minute).UTC(),
69 } 71 }
70 So(ds.Put(&i), ShouldBeNil) 72 So(ds.Put(&i), ShouldBeNil)
71 73
72 state.lastFlushed = clock.Now().Add(-2 * time.Minute) 74 state.lastFlushed = clock.Now().Add(-2 * time.Minute)
73 75
74 rec := httptest.NewRecorder() 76 rec := httptest.NewRecorder()
75 » » state.Middleware(f)(c, rec, &http.Request{}, nil) 77 » » router.RunMiddleware(
78 » » » &router.Context{Context: c, Writer: rec, Request: &http. Request{}},
79 » » » router.MiddlewareChain{state.Middleware},
80 » » » f,
81 » » )
76 So(rec.Code, ShouldEqual, http.StatusOK) 82 So(rec.Code, ShouldEqual, http.StatusOK)
77 83
78 So(len(monitor.Cells), ShouldEqual, 1) 84 So(len(monitor.Cells), ShouldEqual, 1)
79 So(monitor.Cells[0][0].Name, ShouldEqual, "m") 85 So(monitor.Cells[0][0].Name, ShouldEqual, "m")
80 So(monitor.Cells[0][0].Value, ShouldEqual, int64(1)) 86 So(monitor.Cells[0][0].Value, ShouldEqual, int64(1))
81 87
82 // Flushing should update the LastUpdated time. 88 // Flushing should update the LastUpdated time.
83 inst, err := getOrCreateInstanceEntity(c) 89 inst, err := getOrCreateInstanceEntity(c)
84 So(err, ShouldBeNil) 90 So(err, ShouldBeNil)
85 So(inst.LastUpdated, ShouldResemble, clock.Now().Round(time.Seco nd)) 91 So(inst.LastUpdated, ShouldResemble, clock.Now().Round(time.Seco nd))
86 92
87 // The value should still be set. 93 // The value should still be set.
88 value, err := tsmon.Store(c).Get(c, metric, time.Time{}, []inter face{}{}) 94 value, err := tsmon.Store(c).Get(c, metric, time.Time{}, []inter face{}{})
89 So(err, ShouldBeNil) 95 So(err, ShouldBeNil)
90 So(value, ShouldEqual, int64(1)) 96 So(value, ShouldEqual, int64(1))
91 }) 97 })
92 98
93 Convey("Resets cumulative metrics", t, func() { 99 Convey("Resets cumulative metrics", t, func() {
94 c, clock := buildGAETestContext() 100 c, clock := buildGAETestContext()
95 state, monitor := buildTestState() 101 state, monitor := buildTestState()
96 102
97 state.lastFlushed = clock.Now().Add(-2 * time.Minute) 103 state.lastFlushed = clock.Now().Add(-2 * time.Minute)
98 104
99 rec := httptest.NewRecorder() 105 rec := httptest.NewRecorder()
100 » » state.Middleware(func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) { 106 » » router.RunMiddleware(
101 » » » f(c, rw, r, p) 107 » » » &router.Context{Context: c, Writer: rec, Request: &http. Request{}},
102 108 » » » router.MiddlewareChain{state.Middleware},
103 » » » // Override the TaskNum here - it's created just before this handler runs 109 » » » func(c *router.Context) {
104 » » » // and used just after. 110 » » » » f(c)
105 » » » tar := tsmon.Store(c).DefaultTarget().(*target.Task) 111 » » » » // Override the TaskNum here - it's created just before this handler runs
106 » » » tar.TaskNum = proto.Int32(int32(0)) 112 » » » » // and used just after.
107 » » » tsmon.Store(c).SetDefaultTarget(tar) 113 » » » » tar := tsmon.Store(c.Context).DefaultTarget().(* target.Task)
108 » » })(c, rec, &http.Request{}, nil) 114 » » » » tar.TaskNum = proto.Int32(int32(0))
115 » » » » tsmon.Store(c.Context).SetDefaultTarget(tar)
116 » » » },
117 » » )
109 So(rec.Code, ShouldEqual, http.StatusOK) 118 So(rec.Code, ShouldEqual, http.StatusOK)
110 119
111 So(len(tsmon.GetState(c).RegisteredMetrics), ShouldEqual, 1) 120 So(len(tsmon.GetState(c).RegisteredMetrics), ShouldEqual, 1)
112 So(len(monitor.Cells), ShouldEqual, 0) 121 So(len(monitor.Cells), ShouldEqual, 0)
113 122
114 // Value should be reset. 123 // Value should be reset.
115 value, err := tsmon.Store(c).Get(c, metric, time.Time{}, []inter face{}{}) 124 value, err := tsmon.Store(c).Get(c, metric, time.Time{}, []inter face{}{})
116 So(err, ShouldBeNil) 125 So(err, ShouldBeNil)
117 So(value, ShouldBeNil) 126 So(value, ShouldBeNil)
118 }) 127 })
119 128
120 Convey("Dynamic enable and disable works", t, func() { 129 Convey("Dynamic enable and disable works", t, func() {
121 c, _ := buildGAETestContext() 130 c, _ := buildGAETestContext()
122 state, _ := buildTestState() 131 state, _ := buildTestState()
123 132
124 // Enabled. Store is not nil. 133 // Enabled. Store is not nil.
125 rec := httptest.NewRecorder() 134 rec := httptest.NewRecorder()
126 » » state.Middleware(func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) { 135 » » router.RunMiddleware(
127 » » » So(store.IsNilStore(tsmon.Store(c)), ShouldBeFalse) 136 » » » &router.Context{Context: c, Writer: rec, Request: &http. Request{}},
128 » » })(c, rec, &http.Request{}, nil) 137 » » » router.MiddlewareChain{state.Middleware},
138 » » » func(c *router.Context) {
139 » » » » So(store.IsNilStore(tsmon.Store(c.Context)), Sho uldBeFalse)
140 » » » },
141 » » )
129 So(rec.Code, ShouldEqual, http.StatusOK) 142 So(rec.Code, ShouldEqual, http.StatusOK)
130 143
131 // Disabled. Store is nil. 144 // Disabled. Store is nil.
132 state.testingSettings.Enabled = false 145 state.testingSettings.Enabled = false
133 » » state.Middleware(func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) { 146 » » router.RunMiddleware(
134 » » » So(store.IsNilStore(tsmon.Store(c)), ShouldBeTrue) 147 » » » &router.Context{Context: c, Writer: rec, Request: &http. Request{}},
135 » » })(c, rec, &http.Request{}, nil) 148 » » » router.MiddlewareChain{state.Middleware},
149 » » » func(c *router.Context) {
150 » » » » So(store.IsNilStore(tsmon.Store(c.Context)), Sho uldBeTrue)
151 » » » },
152 » » )
136 So(rec.Code, ShouldEqual, http.StatusOK) 153 So(rec.Code, ShouldEqual, http.StatusOK)
137 }) 154 })
138 } 155 }
OLDNEW
« no previous file with comments | « appengine/tsmon/middleware.go ('k') | appengine/tumble/service.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698