| 1 |
/* |
| 2 |
Copyright 2013-2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
package launch_helper |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
"fmt" |
| 22 |
"io/ioutil" |
| 23 |
"os" |
| 24 |
"path/filepath" |
| 25 |
"time" |
| 26 |
|
| 27 |
"github.com/adrg/xdg" |
| 28 |
. "gopkg.in/check.v1" |
| 29 |
|
| 30 |
"gitlab.com/ubports/development/core/lomiri-push-service/click" |
| 31 |
clickhelp "gitlab.com/ubports/development/core/lomiri-push-service/click/testing" |
| 32 |
"gitlab.com/ubports/development/core/lomiri-push-service/launch_helper/cual" |
| 33 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 34 |
) |
| 35 |
|
| 36 |
type testArgs [4]string |
| 37 |
|
| 38 |
type poolSuite struct { |
| 39 |
log *helpers.TestLogger |
| 40 |
pool HelperPool |
| 41 |
fakeLauncher *fakeHelperLauncher |
| 42 |
} |
| 43 |
|
| 44 |
var _ = Suite(&poolSuite{}) |
| 45 |
|
| 46 |
func takeNext(ch chan *HelperResult, c *C) *HelperResult { |
| 47 |
select { |
| 48 |
case res := <-ch: |
| 49 |
return res |
| 50 |
case <-time.After(time.Second): |
| 51 |
c.Fatal("timeout waiting for result") |
| 52 |
} |
| 53 |
return nil |
| 54 |
} |
| 55 |
|
| 56 |
type fakeHelperLauncher struct { |
| 57 |
done func(string) |
| 58 |
obs int |
| 59 |
err error |
| 60 |
lhex string |
| 61 |
argCh chan testArgs |
| 62 |
runid int |
| 63 |
} |
| 64 |
|
| 65 |
func (fhl *fakeHelperLauncher) InstallObserver(done func(string)) error { |
| 66 |
fhl.done = done |
| 67 |
fhl.obs++ |
| 68 |
return nil |
| 69 |
} |
| 70 |
|
| 71 |
func (fhl *fakeHelperLauncher) RemoveObserver() error { |
| 72 |
fhl.obs-- |
| 73 |
return nil |
| 74 |
} |
| 75 |
|
| 76 |
func (fhl *fakeHelperLauncher) HelperAppID(app *click.AppId) string { |
| 77 |
if app.Click { |
| 78 |
return app.String() |
| 79 |
} else { |
| 80 |
return app.Application |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func (fhl *fakeHelperLauncher) Launch(appId, f1, f2 string) (string, error) { |
| 85 |
fhl.argCh <- testArgs{"Launch", appId, f1, f2} |
| 86 |
runid := fmt.Sprintf("%d", fhl.runid) |
| 87 |
fhl.runid++ |
| 88 |
return runid, fhl.err |
| 89 |
} |
| 90 |
|
| 91 |
func (fhl *fakeHelperLauncher) Stop(appId string, iid string) error { |
| 92 |
fhl.argCh <- testArgs{"Stop", appId, iid, ""} |
| 93 |
return nil |
| 94 |
} |
| 95 |
|
| 96 |
func (s *poolSuite) waitForArgs(c *C, method string) testArgs { |
| 97 |
var args testArgs |
| 98 |
select { |
| 99 |
case args = <-s.fakeLauncher.argCh: |
| 100 |
case <-time.After(2 * time.Second): |
| 101 |
c.Fatal("didn't call " + method) |
| 102 |
} |
| 103 |
c.Assert(args[0], Equals, method) |
| 104 |
return args |
| 105 |
} |
| 106 |
|
| 107 |
func (s *poolSuite) SetUpSuite(c *C) { |
| 108 |
xdgCacheHome = c.MkDir |
| 109 |
} |
| 110 |
|
| 111 |
func (s *poolSuite) TearDownSuite(c *C) { |
| 112 |
xdgCacheHome = func() string { |
| 113 |
return xdg.CacheHome |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func (s *poolSuite) SetUpTest(c *C) { |
| 118 |
s.log = helpers.NewTestLogger(c, "debug") |
| 119 |
s.fakeLauncher = &fakeHelperLauncher{argCh: make(chan testArgs, 10)} |
| 120 |
s.pool = NewHelperPool(map[string]HelperLauncher{"fake": s.fakeLauncher}, s.log) |
| 121 |
} |
| 122 |
|
| 123 |
func (s *poolSuite) TearDownTest(c *C) { |
| 124 |
s.pool = nil |
| 125 |
} |
| 126 |
|
| 127 |
func (s *poolSuite) TestDefaultLaunchers(c *C) { |
| 128 |
launchers := DefaultLaunchers(s.log) |
| 129 |
_, ok := launchers["click"] |
| 130 |
c.Check(ok, Equals, true) |
| 131 |
_, ok = launchers["legacy"] |
| 132 |
c.Check(ok, Equals, true) |
| 133 |
} |
| 134 |
|
| 135 |
// check that Stop (tries to) remove the observer |
| 136 |
func (s *poolSuite) TestStartStopWork(c *C) { |
| 137 |
c.Check(s.fakeLauncher.obs, Equals, 0) |
| 138 |
s.pool.Start() |
| 139 |
c.Check(s.fakeLauncher.done, NotNil) |
| 140 |
c.Check(s.fakeLauncher.obs, Equals, 1) |
| 141 |
s.pool.Stop() |
| 142 |
c.Check(s.fakeLauncher.obs, Equals, 0) |
| 143 |
} |
| 144 |
|
| 145 |
func (s *poolSuite) TestRunLaunches(c *C) { |
| 146 |
s.pool.Start() |
| 147 |
defer s.pool.Stop() |
| 148 |
appId := "com.example.test_test-app" |
| 149 |
app := clickhelp.MustParseAppId(appId) |
| 150 |
helpId := app.Base() |
| 151 |
input := HelperInput{ |
| 152 |
App: app, |
| 153 |
NotificationId: "foo", |
| 154 |
Payload: []byte(`"hello"`), |
| 155 |
} |
| 156 |
s.pool.Run("fake", &input) |
| 157 |
launchArgs := s.waitForArgs(c, "Launch") |
| 158 |
c.Check(launchArgs[:2], DeepEquals, []string{"Launch", helpId}) |
| 159 |
args := s.pool.(*kindHelperPool).peekId("fake:0", func(*HelperArgs) {}) |
| 160 |
c.Assert(args, NotNil) |
| 161 |
args.Timer.Stop() |
| 162 |
c.Check(args.AppId, Equals, helpId) |
| 163 |
c.Check(args.Input, Equals, &input) |
| 164 |
c.Check(args.FileIn, NotNil) |
| 165 |
c.Check(args.FileOut, NotNil) |
| 166 |
} |
| 167 |
|
| 168 |
func (s *poolSuite) TestRunLaunchesLegacyStyle(c *C) { |
| 169 |
s.fakeLauncher.lhex = "lhex" |
| 170 |
s.pool.Start() |
| 171 |
defer s.pool.Stop() |
| 172 |
appId := "_legacy" |
| 173 |
app := clickhelp.MustParseAppId(appId) |
| 174 |
input := HelperInput{ |
| 175 |
App: app, |
| 176 |
NotificationId: "foo", |
| 177 |
Payload: []byte(`"hello"`), |
| 178 |
} |
| 179 |
s.pool.Run("fake", &input) |
| 180 |
launchArgs := s.waitForArgs(c, "Launch") |
| 181 |
c.Check(launchArgs[:2], DeepEquals, []string{"Launch", "legacy"}) |
| 182 |
args := s.pool.(*kindHelperPool).peekId("fake:0", func(*HelperArgs) {}) |
| 183 |
c.Assert(args, NotNil) |
| 184 |
args.Timer.Stop() |
| 185 |
c.Check(args.Input, Equals, &input) |
| 186 |
c.Check(args.FileIn, NotNil) |
| 187 |
c.Check(args.FileOut, NotNil) |
| 188 |
} |
| 189 |
|
| 190 |
func (s *poolSuite) TestGetOutputIfHelperLaunchFail(c *C) { |
| 191 |
ch := s.pool.Start() |
| 192 |
defer s.pool.Stop() |
| 193 |
app := clickhelp.MustParseAppId("com.example.test_test-app") |
| 194 |
input := HelperInput{ |
| 195 |
App: app, |
| 196 |
NotificationId: "foo", |
| 197 |
Payload: []byte(`"hello"`), |
| 198 |
} |
| 199 |
s.pool.Run("not-there", &input) |
| 200 |
res := takeNext(ch, c) |
| 201 |
c.Check(res.Message, DeepEquals, input.Payload) |
| 202 |
c.Check(res.Notification, IsNil) |
| 203 |
c.Check(*res.Input, DeepEquals, input) |
| 204 |
} |
| 205 |
|
| 206 |
func (s *poolSuite) TestGetOutputIfHelperLaunchFail2(c *C) { |
| 207 |
s.fakeLauncher.err = ErrCantFindHelper |
| 208 |
ch := s.pool.Start() |
| 209 |
defer s.pool.Stop() |
| 210 |
app := clickhelp.MustParseAppId("_legacy") |
| 211 |
input := HelperInput{ |
| 212 |
App: app, |
| 213 |
NotificationId: "foo", |
| 214 |
Payload: []byte(`"hello"`), |
| 215 |
} |
| 216 |
s.pool.Run("fake", &input) |
| 217 |
res := takeNext(ch, c) |
| 218 |
c.Check(res.Message, DeepEquals, input.Payload) |
| 219 |
c.Check(res.Notification, IsNil) |
| 220 |
c.Check(*res.Input, DeepEquals, input) |
| 221 |
} |
| 222 |
|
| 223 |
func (s *poolSuite) TestRunCantLaunch(c *C) { |
| 224 |
s.fakeLauncher.err = cual.ErrCantLaunch |
| 225 |
ch := s.pool.Start() |
| 226 |
defer s.pool.Stop() |
| 227 |
appId := "com.example.test_test-app" |
| 228 |
app := clickhelp.MustParseAppId(appId) |
| 229 |
helpId := app.Base() |
| 230 |
input := HelperInput{ |
| 231 |
App: app, |
| 232 |
NotificationId: "foo", |
| 233 |
Payload: []byte(`"hello"`), |
| 234 |
} |
| 235 |
s.pool.Run("fake", &input) |
| 236 |
launchArgs := s.waitForArgs(c, "Launch") |
| 237 |
c.Check(launchArgs[:2], DeepEquals, []string{"Launch", helpId}) |
| 238 |
res := takeNext(ch, c) |
| 239 |
c.Check(res.Message, DeepEquals, input.Payload) |
| 240 |
c.Check(s.log.Captured(), Equals, "DEBUG using helper com.example.test_test-app for app com.example.test_test-app\n"+"ERROR unable to launch helper com.example.test_test-app: can't launch helper\n"+"ERROR unable to get helper output; putting payload into message\n") |
| 241 |
} |
| 242 |
|
| 243 |
func (s *poolSuite) TestRunLaunchesAndTimeout(c *C) { |
| 244 |
s.pool.(*kindHelperPool).maxRuntime = 500 * time.Millisecond |
| 245 |
ch := s.pool.Start() |
| 246 |
defer s.pool.Stop() |
| 247 |
appId := "com.example.test_test-app" |
| 248 |
app := clickhelp.MustParseAppId(appId) |
| 249 |
helpId := app.Base() |
| 250 |
input := HelperInput{ |
| 251 |
App: app, |
| 252 |
NotificationId: "foo", |
| 253 |
Payload: []byte(`"hello"`), |
| 254 |
} |
| 255 |
s.pool.Run("fake", &input) |
| 256 |
launchArgs := s.waitForArgs(c, "Launch") |
| 257 |
c.Check(launchArgs[0], Equals, "Launch") |
| 258 |
stopArgs := s.waitForArgs(c, "Stop") |
| 259 |
c.Check(stopArgs[:3], DeepEquals, []string{"Stop", helpId, "0"}) |
| 260 |
// this will be invoked |
| 261 |
go s.fakeLauncher.done("0") |
| 262 |
|
| 263 |
res := takeNext(ch, c) |
| 264 |
c.Check(res.Message, DeepEquals, input.Payload) |
| 265 |
} |
| 266 |
|
| 267 |
func (s *poolSuite) TestOneDoneNop(c *C) { |
| 268 |
pool := s.pool.(*kindHelperPool) |
| 269 |
pool.OneDone("") |
| 270 |
} |
| 271 |
|
| 272 |
func (s *poolSuite) TestOneDoneOnValid(c *C) { |
| 273 |
pool := s.pool.(*kindHelperPool) |
| 274 |
ch := pool.Start() |
| 275 |
defer pool.Stop() |
| 276 |
|
| 277 |
d := c.MkDir() |
| 278 |
|
| 279 |
app := clickhelp.MustParseAppId("com.example.test_test-app") |
| 280 |
input := &HelperInput{ |
| 281 |
App: app, |
| 282 |
} |
| 283 |
args := HelperArgs{ |
| 284 |
Input: input, |
| 285 |
FileOut: filepath.Join(d, "file_out.json"), |
| 286 |
Timer: time.NewTimer(0), |
| 287 |
} |
| 288 |
pool.hmap["l:1"] = &args |
| 289 |
|
| 290 |
f, err := os.Create(args.FileOut) |
| 291 |
c.Assert(err, IsNil) |
| 292 |
defer f.Close() |
| 293 |
_, err = f.Write([]byte(`{"notification": {"sound": "hello", "tag": "a-tag"}}`)) |
| 294 |
c.Assert(err, IsNil) |
| 295 |
|
| 296 |
go pool.OneDone("l:1") |
| 297 |
|
| 298 |
res := takeNext(ch, c) |
| 299 |
|
| 300 |
expected := HelperOutput{Notification: &Notification{RawSound: json.RawMessage(`"hello"`), Tag: "a-tag"}} |
| 301 |
c.Check(res.HelperOutput, DeepEquals, expected) |
| 302 |
c.Check(pool.hmap, HasLen, 0) |
| 303 |
} |
| 304 |
|
| 305 |
func (s *poolSuite) TestOneDoneOnBadFileOut(c *C) { |
| 306 |
pool := s.pool.(*kindHelperPool) |
| 307 |
ch := pool.Start() |
| 308 |
defer pool.Stop() |
| 309 |
|
| 310 |
app := clickhelp.MustParseAppId("com.example.test_test-app") |
| 311 |
args := HelperArgs{ |
| 312 |
Input: &HelperInput{ |
| 313 |
App: app, |
| 314 |
NotificationId: "foo", |
| 315 |
Payload: []byte(`"hello"`), |
| 316 |
}, |
| 317 |
FileOut: "/does-not-exist", |
| 318 |
Timer: time.NewTimer(0), |
| 319 |
} |
| 320 |
pool.hmap["l:1"] = &args |
| 321 |
|
| 322 |
go pool.OneDone("l:1") |
| 323 |
|
| 324 |
res := takeNext(ch, c) |
| 325 |
|
| 326 |
expected := HelperOutput{Message: args.Input.Payload} |
| 327 |
c.Check(res.HelperOutput, DeepEquals, expected) |
| 328 |
} |
| 329 |
|
| 330 |
func (s *poolSuite) TestOneDonwOnBadJSONOut(c *C) { |
| 331 |
pool := s.pool.(*kindHelperPool) |
| 332 |
ch := pool.Start() |
| 333 |
defer pool.Stop() |
| 334 |
|
| 335 |
d := c.MkDir() |
| 336 |
|
| 337 |
app := clickhelp.MustParseAppId("com.example.test_test-app") |
| 338 |
args := HelperArgs{ |
| 339 |
FileOut: filepath.Join(d, "file_out.json"), |
| 340 |
Input: &HelperInput{ |
| 341 |
App: app, |
| 342 |
NotificationId: "foo", |
| 343 |
Payload: []byte(`"hello"`), |
| 344 |
}, |
| 345 |
Timer: time.NewTimer(0), |
| 346 |
} |
| 347 |
pool.hmap["l:1"] = &args |
| 348 |
|
| 349 |
f, err := os.Create(args.FileOut) |
| 350 |
c.Assert(err, IsNil) |
| 351 |
defer f.Close() |
| 352 |
_, err = f.Write([]byte(`potato`)) |
| 353 |
c.Assert(err, IsNil) |
| 354 |
|
| 355 |
go pool.OneDone("l:1") |
| 356 |
|
| 357 |
res := takeNext(ch, c) |
| 358 |
|
| 359 |
expected := HelperOutput{Message: args.Input.Payload} |
| 360 |
c.Check(res.HelperOutput, DeepEquals, expected) |
| 361 |
} |
| 362 |
|
| 363 |
func (s *poolSuite) TestCreateInputTempFile(c *C) { |
| 364 |
tmpDir := c.MkDir() |
| 365 |
GetTempDir = func(pkgName string) (string, error) { |
| 366 |
return tmpDir, nil |
| 367 |
} |
| 368 |
// restore it when we are done |
| 369 |
defer func() { |
| 370 |
GetTempDir = _getTempDir |
| 371 |
}() |
| 372 |
|
| 373 |
app := clickhelp.MustParseAppId("com.example.test_test-app") |
| 374 |
input := &HelperInput{ |
| 375 |
App: app, |
| 376 |
NotificationId: "foo", |
| 377 |
Payload: []byte(`"hello"`), |
| 378 |
} |
| 379 |
|
| 380 |
pool := s.pool.(*kindHelperPool) |
| 381 |
f1, err := pool.createInputTempFile(input) |
| 382 |
c.Assert(err, IsNil) |
| 383 |
c.Check(f1, Not(Equals), "") |
| 384 |
f2, err := pool.createOutputTempFile(input) |
| 385 |
c.Assert(err, IsNil) |
| 386 |
c.Check(f2, Not(Equals), "") |
| 387 |
files, err := ioutil.ReadDir(filepath.Dir(f1)) |
| 388 |
c.Check(err, IsNil) |
| 389 |
c.Check(files, HasLen, 2) |
| 390 |
} |
| 391 |
|
| 392 |
func (s *poolSuite) TestGetTempFilename(c *C) { |
| 393 |
tmpDir := c.MkDir() |
| 394 |
GetTempDir = func(pkgName string) (string, error) { |
| 395 |
return tmpDir, nil |
| 396 |
} |
| 397 |
// restore it when we are done |
| 398 |
defer func() { |
| 399 |
GetTempDir = _getTempDir |
| 400 |
}() |
| 401 |
fname, err := getTempFilename("pkg.name") |
| 402 |
c.Check(err, IsNil) |
| 403 |
dirname := filepath.Dir(fname) |
| 404 |
files, err := ioutil.ReadDir(dirname) |
| 405 |
c.Check(err, IsNil) |
| 406 |
c.Check(files, HasLen, 1) |
| 407 |
} |
| 408 |
|
| 409 |
func (s *poolSuite) TestGetTempDir(c *C) { |
| 410 |
tmpDir := c.MkDir() |
| 411 |
oldCacheHome := xdgCacheHome |
| 412 |
xdgCacheHome = func() string { |
| 413 |
return tmpDir |
| 414 |
} |
| 415 |
// restore it when we are done |
| 416 |
defer func() { |
| 417 |
xdgCacheHome = oldCacheHome |
| 418 |
}() |
| 419 |
dname, err := GetTempDir("pkg.name") |
| 420 |
c.Check(err, IsNil) |
| 421 |
c.Check(dname, Equals, filepath.Join(tmpDir, "pkg.name")) |
| 422 |
} |
| 423 |
|
| 424 |
// checks that the a second helper run of an already-running helper |
| 425 |
// (for an app) goes to the backlog |
| 426 |
func (s *poolSuite) TestSecondRunSameAppToBacklog(c *C) { |
| 427 |
ch := s.pool.Start() |
| 428 |
defer s.pool.Stop() |
| 429 |
|
| 430 |
app1 := clickhelp.MustParseAppId("com.example.test_test-app-1") |
| 431 |
input1 := &HelperInput{ |
| 432 |
App: app1, |
| 433 |
NotificationId: "foo1", |
| 434 |
Payload: []byte(`"hello1"`), |
| 435 |
} |
| 436 |
app2 := clickhelp.MustParseAppId("com.example.test_test-app-1") |
| 437 |
input2 := &HelperInput{ |
| 438 |
App: app2, |
| 439 |
NotificationId: "foo2", |
| 440 |
Payload: []byte(`"hello2"`), |
| 441 |
} |
| 442 |
|
| 443 |
c.Assert(app1.Base(), Equals, app2.Base()) |
| 444 |
|
| 445 |
s.pool.Run("fake", input1) |
| 446 |
s.pool.Run("fake", input2) |
| 447 |
|
| 448 |
s.waitForArgs(c, "Launch") |
| 449 |
go s.fakeLauncher.done("0") |
| 450 |
takeNext(ch, c) |
| 451 |
|
| 452 |
// this is where we check that: |
| 453 |
c.Check(s.log.Captured(), Matches, `(?ms).* helper input backlog has grown to 1 entries.$`) |
| 454 |
} |
| 455 |
|
| 456 |
// checks that the an Nth helper run goes to the backlog |
| 457 |
func (s *poolSuite) TestRunNthAppToBacklog(c *C) { |
| 458 |
s.pool.(*kindHelperPool).maxNum = 2 |
| 459 |
doGrowBacklog := s.pool.(*kindHelperPool).doGrowBacklog |
| 460 |
grownTo1 := make(chan struct{}) |
| 461 |
s.pool.(*kindHelperPool).growBacklog = func(bl []*HelperInput, in *HelperInput) []*HelperInput { |
| 462 |
res := doGrowBacklog(bl, in) |
| 463 |
if len(res) == 1 { |
| 464 |
close(grownTo1) |
| 465 |
} |
| 466 |
return res |
| 467 |
} |
| 468 |
ch := s.pool.Start() |
| 469 |
defer s.pool.Stop() |
| 470 |
|
| 471 |
app1 := clickhelp.MustParseAppId("com.example.test_test-app-1") |
| 472 |
input1 := &HelperInput{ |
| 473 |
App: app1, |
| 474 |
NotificationId: "foo1", |
| 475 |
Payload: []byte(`"hello1"`), |
| 476 |
} |
| 477 |
app2 := clickhelp.MustParseAppId("com.example.test_test-app-2") |
| 478 |
input2 := &HelperInput{ |
| 479 |
App: app2, |
| 480 |
NotificationId: "foo2", |
| 481 |
Payload: []byte(`"hello2"`), |
| 482 |
} |
| 483 |
app3 := clickhelp.MustParseAppId("com.example.test_test-app-3") |
| 484 |
input3 := &HelperInput{ |
| 485 |
App: app3, |
| 486 |
NotificationId: "foo3", |
| 487 |
Payload: []byte(`"hello3"`), |
| 488 |
} |
| 489 |
|
| 490 |
s.pool.Run("fake", input1) |
| 491 |
s.waitForArgs(c, "Launch") |
| 492 |
|
| 493 |
s.pool.Run("fake", input2) |
| 494 |
s.log.ResetCapture() |
| 495 |
s.waitForArgs(c, "Launch") |
| 496 |
|
| 497 |
s.pool.Run("fake", input3) |
| 498 |
|
| 499 |
select { |
| 500 |
case <-grownTo1: |
| 501 |
case <-time.After(time.Second): |
| 502 |
c.Fatal("timeout waiting for result") |
| 503 |
} |
| 504 |
go s.fakeLauncher.done("0") |
| 505 |
|
| 506 |
res := takeNext(ch, c) |
| 507 |
c.Assert(res, NotNil) |
| 508 |
|
| 509 |
s.waitForArgs(c, "Launch") |
| 510 |
|
| 511 |
c.Assert(res.Input, NotNil) |
| 512 |
c.Assert(res.Input.App, NotNil) |
| 513 |
c.Assert(res.Input.App.Original(), Equals, "com.example.test_test-app-1") |
| 514 |
go s.fakeLauncher.done("1") |
| 515 |
go s.fakeLauncher.done("2") |
| 516 |
takeNext(ch, c) |
| 517 |
takeNext(ch, c) |
| 518 |
|
| 519 |
// this is the crux: we're checking that the third Run() went to the backlog. |
| 520 |
c.Check(s.log.Captured(), Matches, |
| 521 |
`(?ms).* helper input backlog has grown to 1 entries\.$.*shrunk to 0 entries\.$`) |
| 522 |
} |
| 523 |
|
| 524 |
func (s *poolSuite) TestRunBacklogFailedContinuesDiffApp(c *C) { |
| 525 |
s.pool.(*kindHelperPool).maxNum = 1 |
| 526 |
doGrowBacklog := s.pool.(*kindHelperPool).doGrowBacklog |
| 527 |
grownTo3 := make(chan struct{}) |
| 528 |
s.pool.(*kindHelperPool).growBacklog = func(bl []*HelperInput, in *HelperInput) []*HelperInput { |
| 529 |
res := doGrowBacklog(bl, in) |
| 530 |
if len(res) == 3 { |
| 531 |
close(grownTo3) |
| 532 |
} |
| 533 |
return res |
| 534 |
} |
| 535 |
|
| 536 |
ch := s.pool.Start() |
| 537 |
defer s.pool.Stop() |
| 538 |
|
| 539 |
app1 := clickhelp.MustParseAppId("com.example.test_test-app-1") |
| 540 |
input1 := &HelperInput{ |
| 541 |
App: app1, |
| 542 |
NotificationId: "foo1", |
| 543 |
Payload: []byte(`"hello1"`), |
| 544 |
} |
| 545 |
app2 := clickhelp.MustParseAppId("com.example.test_test-app-2") |
| 546 |
input2 := &HelperInput{ |
| 547 |
App: app2, |
| 548 |
NotificationId: "foo2", |
| 549 |
Payload: []byte(`"hello2"`), |
| 550 |
} |
| 551 |
app3 := clickhelp.MustParseAppId("com.example.test_test-app-3") |
| 552 |
input3 := &HelperInput{ |
| 553 |
App: app3, |
| 554 |
NotificationId: "foo3", |
| 555 |
Payload: []byte(`"hello3"`), |
| 556 |
} |
| 557 |
app4 := clickhelp.MustParseAppId("com.example.test_test-app-4") |
| 558 |
input4 := &HelperInput{ |
| 559 |
App: app4, |
| 560 |
NotificationId: "foo4", |
| 561 |
Payload: []byte(`"hello4"`), |
| 562 |
} |
| 563 |
|
| 564 |
s.pool.Run("fake", input1) |
| 565 |
s.waitForArgs(c, "Launch") |
| 566 |
s.pool.Run("NOT-THERE", input2) // this will fail |
| 567 |
s.pool.Run("fake", input3) |
| 568 |
s.pool.Run("fake", input4) |
| 569 |
|
| 570 |
select { |
| 571 |
case <-grownTo3: |
| 572 |
case <-time.After(time.Second): |
| 573 |
c.Fatal("timeout waiting for result") |
| 574 |
} |
| 575 |
go s.fakeLauncher.done("0") |
| 576 |
// Everything up to here was just set-up. |
| 577 |
// |
| 578 |
// What we're checking for is that, if a helper launch fails, the |
| 579 |
// next one in the backlog is picked up. |
| 580 |
takeNext(ch, c) |
| 581 |
takeNext(ch, c) |
| 582 |
s.waitForArgs(c, "Launch") |
| 583 |
go s.fakeLauncher.done("1") |
| 584 |
c.Assert(takeNext(ch, c).Input.App, Equals, app3) |
| 585 |
c.Check(s.log.Captured(), Matches, |
| 586 |
`(?ms).* helper input backlog has grown to 3 entries\.$.*shrunk to 1 entries\.$`) |
| 587 |
} |
| 588 |
|
| 589 |
func (s *poolSuite) TestBigBacklogShrinks(c *C) { |
| 590 |
oldBufSz := InputBufferSize |
| 591 |
InputBufferSize = 0 |
| 592 |
defer func() { InputBufferSize = oldBufSz }() |
| 593 |
s.pool.(*kindHelperPool).maxNum = 1 |
| 594 |
ch := s.pool.Start() |
| 595 |
defer s.pool.Stop() |
| 596 |
|
| 597 |
app := clickhelp.MustParseAppId("com.example.test_test-app") |
| 598 |
s.pool.Run("fake", &HelperInput{App: app, NotificationId: "0", Payload: []byte(`""`)}) |
| 599 |
s.pool.Run("fake", &HelperInput{App: app, NotificationId: "1", Payload: []byte(`""`)}) |
| 600 |
s.pool.Run("fake", &HelperInput{App: app, NotificationId: "2", Payload: []byte(`""`)}) |
| 601 |
s.waitForArgs(c, "Launch") |
| 602 |
go s.fakeLauncher.done("0") |
| 603 |
takeNext(ch, c) |
| 604 |
// so now there's one done, one "running", and one more waiting. |
| 605 |
// kicking it forward one more notch before checking the logs: |
| 606 |
s.waitForArgs(c, "Launch") |
| 607 |
go s.fakeLauncher.done("1") |
| 608 |
takeNext(ch, c) |
| 609 |
// (two done, one "running") |
| 610 |
c.Check(s.log.Captured(), Matches, `(?ms).* shrunk to 1 entries\.$`) |
| 611 |
// and the backlog shrinker shrunk the backlog |
| 612 |
c.Check(s.log.Captured(), Matches, `(?ms).*copying backlog to avoid wasting too much space .*`) |
| 613 |
} |
| 614 |
|
| 615 |
func (s *poolSuite) TestBacklogShrinkerNilToNil(c *C) { |
| 616 |
pool := s.pool.(*kindHelperPool) |
| 617 |
c.Check(pool.shrinkBacklog(nil, 0), IsNil) |
| 618 |
} |
| 619 |
|
| 620 |
func (s *poolSuite) TestBacklogShrinkerEmptyToNil(c *C) { |
| 621 |
pool := s.pool.(*kindHelperPool) |
| 622 |
empty := []*HelperInput{nil, nil, nil} |
| 623 |
c.Check(pool.shrinkBacklog(empty, 0), IsNil) |
| 624 |
} |
| 625 |
|
| 626 |
func (s *poolSuite) TestBacklogShrinkerFullUntouched(c *C) { |
| 627 |
pool := s.pool.(*kindHelperPool) |
| 628 |
input := &HelperInput{} |
| 629 |
full := []*HelperInput{input, input, input} |
| 630 |
c.Check(pool.shrinkBacklog(full, 3), DeepEquals, full) |
| 631 |
} |
| 632 |
|
| 633 |
func (s *poolSuite) TestBacklogShrinkerSparseShrunk(c *C) { |
| 634 |
pool := s.pool.(*kindHelperPool) |
| 635 |
input := &HelperInput{} |
| 636 |
sparse := []*HelperInput{nil, input, nil, input, nil} |
| 637 |
full := []*HelperInput{input, input} |
| 638 |
c.Check(pool.shrinkBacklog(sparse, 2), DeepEquals, full) |
| 639 |
} |