| 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 "fmt" | 8 "fmt" |
| 9 "net/http" | 9 "net/http" |
| 10 "net/http/httptest" | 10 "net/http/httptest" |
| 11 "testing" | 11 "testing" |
| 12 "time" | 12 "time" |
| 13 | 13 |
| 14 "github.com/luci/gae/service/datastore" | 14 "github.com/luci/gae/service/datastore" |
| 15 "github.com/luci/gae/service/info" | 15 "github.com/luci/gae/service/info" |
| 16 "github.com/luci/luci-go/appengine/gaetesting" | 16 "github.com/luci/luci-go/appengine/gaetesting" |
| 17 "github.com/luci/luci-go/common/clock/testclock" | 17 "github.com/luci/luci-go/common/clock/testclock" |
| 18 "github.com/luci/luci-go/common/logging/gologger" | 18 "github.com/luci/luci-go/common/logging/gologger" |
| 19 "github.com/luci/luci-go/common/tsmon" | 19 "github.com/luci/luci-go/common/tsmon" |
| 20 "github.com/luci/luci-go/common/tsmon/monitor" | 20 "github.com/luci/luci-go/common/tsmon/monitor" |
| 21 "github.com/luci/luci-go/server/router" |
| 21 "golang.org/x/net/context" | 22 "golang.org/x/net/context" |
| 22 | 23 |
| 23 . "github.com/smartystreets/goconvey/convey" | 24 . "github.com/smartystreets/goconvey/convey" |
| 24 ) | 25 ) |
| 25 | 26 |
| 26 type fakeInfo struct { | 27 type fakeInfo struct { |
| 27 info.RawInterface | 28 info.RawInterface |
| 28 } | 29 } |
| 29 | 30 |
| 30 func (i *fakeInfo) InstanceID() string { return "instance" } | 31 func (i *fakeInfo) InstanceID() string { return "instance" } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 | 92 |
| 92 func TestHousekeepingHandler(t *testing.T) { | 93 func TestHousekeepingHandler(t *testing.T) { |
| 93 Convey("Assigns task numbers to unassigned instances", t, func() { | 94 Convey("Assigns task numbers to unassigned instances", t, func() { |
| 94 c, _ := buildGAETestContext() | 95 c, _ := buildGAETestContext() |
| 95 | 96 |
| 96 i, err := getOrCreateInstanceEntity(c) | 97 i, err := getOrCreateInstanceEntity(c) |
| 97 So(err, ShouldBeNil) | 98 So(err, ShouldBeNil) |
| 98 So(i.TaskNum, ShouldEqual, -1) | 99 So(i.TaskNum, ShouldEqual, -1) |
| 99 | 100 |
| 100 rec := httptest.NewRecorder() | 101 rec := httptest.NewRecorder() |
| 101 » » housekeepingHandler(c, rec, &http.Request{}, nil) | 102 » » housekeepingHandler(&router.Context{ |
| 103 » » » Context: c, |
| 104 » » » Writer: rec, |
| 105 » » » Request: &http.Request{}, |
| 106 » » }) |
| 102 So(rec.Code, ShouldEqual, http.StatusOK) | 107 So(rec.Code, ShouldEqual, http.StatusOK) |
| 103 | 108 |
| 104 i, err = getOrCreateInstanceEntity(c) | 109 i, err = getOrCreateInstanceEntity(c) |
| 105 So(err, ShouldBeNil) | 110 So(err, ShouldBeNil) |
| 106 So(i.TaskNum, ShouldEqual, 0) | 111 So(i.TaskNum, ShouldEqual, 0) |
| 107 }) | 112 }) |
| 108 | 113 |
| 109 Convey("Doesn't reassign the same task number", t, func() { | 114 Convey("Doesn't reassign the same task number", t, func() { |
| 110 c, clock := buildGAETestContext() | 115 c, clock := buildGAETestContext() |
| 111 | 116 |
| 112 otherInstance := instance{ | 117 otherInstance := instance{ |
| 113 ID: "foobar", | 118 ID: "foobar", |
| 114 TaskNum: 0, | 119 TaskNum: 0, |
| 115 LastUpdated: clock.Now(), | 120 LastUpdated: clock.Now(), |
| 116 } | 121 } |
| 117 So(datastore.Get(c).Put(&otherInstance), ShouldBeNil) | 122 So(datastore.Get(c).Put(&otherInstance), ShouldBeNil) |
| 118 | 123 |
| 119 getOrCreateInstanceEntity(c) | 124 getOrCreateInstanceEntity(c) |
| 120 | 125 |
| 121 rec := httptest.NewRecorder() | 126 rec := httptest.NewRecorder() |
| 122 » » housekeepingHandler(c, rec, &http.Request{}, nil) | 127 » » housekeepingHandler(&router.Context{ |
| 128 » » » Context: c, |
| 129 » » » Writer: rec, |
| 130 » » » Request: &http.Request{}, |
| 131 » » }) |
| 123 So(rec.Code, ShouldEqual, http.StatusOK) | 132 So(rec.Code, ShouldEqual, http.StatusOK) |
| 124 | 133 |
| 125 i, err := getOrCreateInstanceEntity(c) | 134 i, err := getOrCreateInstanceEntity(c) |
| 126 So(err, ShouldBeNil) | 135 So(err, ShouldBeNil) |
| 127 So(i.TaskNum, ShouldEqual, 1) | 136 So(i.TaskNum, ShouldEqual, 1) |
| 128 }) | 137 }) |
| 129 | 138 |
| 130 Convey("Expires old instances", t, func() { | 139 Convey("Expires old instances", t, func() { |
| 131 c, clock := buildGAETestContext() | 140 c, clock := buildGAETestContext() |
| 132 ds := datastore.Get(c) | 141 ds := datastore.Get(c) |
| 133 | 142 |
| 134 oldInstance := instance{ | 143 oldInstance := instance{ |
| 135 ID: "foobar", | 144 ID: "foobar", |
| 136 TaskNum: 0, | 145 TaskNum: 0, |
| 137 LastUpdated: clock.Now(), | 146 LastUpdated: clock.Now(), |
| 138 } | 147 } |
| 139 So(ds.Put(&oldInstance), ShouldBeNil) | 148 So(ds.Put(&oldInstance), ShouldBeNil) |
| 140 exists, err := ds.Exists(ds.NewKey("Instance", "foobar", 0, nil)
) | 149 exists, err := ds.Exists(ds.NewKey("Instance", "foobar", 0, nil)
) |
| 141 So(err, ShouldBeNil) | 150 So(err, ShouldBeNil) |
| 142 So(exists.All(), ShouldBeTrue) | 151 So(exists.All(), ShouldBeTrue) |
| 143 | 152 |
| 144 clock.Add(instanceExpirationTimeout + time.Second) | 153 clock.Add(instanceExpirationTimeout + time.Second) |
| 145 | 154 |
| 146 rec := httptest.NewRecorder() | 155 rec := httptest.NewRecorder() |
| 147 » » housekeepingHandler(c, rec, &http.Request{}, nil) | 156 » » housekeepingHandler(&router.Context{ |
| 157 » » » Context: c, |
| 158 » » » Writer: rec, |
| 159 » » » Request: &http.Request{}, |
| 160 » » }) |
| 148 So(rec.Code, ShouldEqual, http.StatusOK) | 161 So(rec.Code, ShouldEqual, http.StatusOK) |
| 149 | 162 |
| 150 exists, err = ds.Exists(ds.NewKey("Instance", "foobar", 0, nil)) | 163 exists, err = ds.Exists(ds.NewKey("Instance", "foobar", 0, nil)) |
| 151 So(err, ShouldBeNil) | 164 So(err, ShouldBeNil) |
| 152 So(exists.All(), ShouldBeFalse) | 165 So(exists.All(), ShouldBeFalse) |
| 153 }) | 166 }) |
| 154 } | 167 } |
| OLD | NEW |