~axwalk/juju-core/lp1303195-manual-ubuntuuser-bash

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package worker

import (
	"errors"

	gc "launchpad.net/gocheck"

	"launchpad.net/juju-core/testing/testbase"
)

type simpleWorkerSuite struct {
	testbase.LoggingSuite
}

var _ = gc.Suite(&simpleWorkerSuite{})

var testError = errors.New("test error")

func (s *simpleWorkerSuite) TestWait(c *gc.C) {
	doWork := func(_ <-chan struct{}) error {
		return testError
	}

	w := NewSimpleWorker(doWork)
	c.Assert(w.Wait(), gc.Equals, testError)
}

func (s *simpleWorkerSuite) TestWaitNil(c *gc.C) {
	doWork := func(_ <-chan struct{}) error {
		return nil
	}

	w := NewSimpleWorker(doWork)
	c.Assert(w.Wait(), gc.Equals, nil)
}

func (s *simpleWorkerSuite) TestKill(c *gc.C) {
	doWork := func(stopCh <-chan struct{}) error {
		<-stopCh
		return testError
	}

	w := NewSimpleWorker(doWork)
	w.Kill()
	c.Assert(w.Wait(), gc.Equals, testError)

	// test we can kill again without a panic
	w.Kill()
}