| 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 |
"testing" |
| 22 |
"time" |
| 23 |
|
| 24 |
. "gopkg.in/check.v1" |
| 25 |
|
| 26 |
"gitlab.com/ubports/development/core/lomiri-push-service/click" |
| 27 |
clickhelp "gitlab.com/ubports/development/core/lomiri-push-service/click/testing" |
| 28 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 29 |
) |
| 30 |
|
| 31 |
func Test(t *testing.T) { TestingT(t) } |
| 32 |
|
| 33 |
type runnerSuite struct { |
| 34 |
testlog *helpers.TestLogger |
| 35 |
app *click.AppId |
| 36 |
} |
| 37 |
|
| 38 |
var _ = Suite(&runnerSuite{}) |
| 39 |
|
| 40 |
func (s *runnerSuite) SetUpTest(c *C) { |
| 41 |
s.testlog = helpers.NewTestLogger(c, "error") |
| 42 |
s.app = clickhelp.MustParseAppId("com.example.test_test-app_0") |
| 43 |
} |
| 44 |
|
| 45 |
func (s *runnerSuite) TestTrivialPoolWorks(c *C) { |
| 46 |
notif := &Notification{RawSound: json.RawMessage(`"42"`), Tag: "foo"} |
| 47 |
|
| 48 |
triv := NewTrivialHelperPool(s.testlog) |
| 49 |
ch := triv.Start() |
| 50 |
in := &HelperInput{App: s.app, Payload: []byte(`{"message": {"m":42}, "notification": {"sound": "42", "tag": "foo"}}`)} |
| 51 |
triv.Run("klick", in) |
| 52 |
out := <-ch |
| 53 |
c.Assert(out, NotNil) |
| 54 |
c.Check(out.Message, DeepEquals, json.RawMessage(`{"m":42}`)) |
| 55 |
c.Check(out.Notification, DeepEquals, notif) |
| 56 |
c.Check(out.Input, DeepEquals, in) |
| 57 |
} |
| 58 |
|
| 59 |
func (s *runnerSuite) TestTrivialPoolWorksOnBadInput(c *C) { |
| 60 |
triv := NewTrivialHelperPool(s.testlog) |
| 61 |
ch := triv.Start() |
| 62 |
msg := []byte(`{card: 3}`) |
| 63 |
in := &HelperInput{App: s.app, Payload: msg} |
| 64 |
triv.Run("klick", in) |
| 65 |
out := <-ch |
| 66 |
c.Assert(out, NotNil) |
| 67 |
c.Check(out.Notification, IsNil) |
| 68 |
c.Check(out.Message, DeepEquals, json.RawMessage(msg)) |
| 69 |
c.Check(out.Input, DeepEquals, in) |
| 70 |
} |
| 71 |
|
| 72 |
func (s *runnerSuite) TestTrivialPoolDoesNotBlockEasily(c *C) { |
| 73 |
triv := NewTrivialHelperPool(s.testlog) |
| 74 |
triv.Start() |
| 75 |
msg := []byte(`this is a not your grandmother's json message`) |
| 76 |
in := &HelperInput{App: s.app, Payload: msg} |
| 77 |
flagCh := make(chan bool) |
| 78 |
go func() { |
| 79 |
// stuff several in there |
| 80 |
triv.Run("klick", in) |
| 81 |
triv.Run("klick", in) |
| 82 |
triv.Run("klick", in) |
| 83 |
flagCh <- true |
| 84 |
}() |
| 85 |
select { |
| 86 |
case <-flagCh: |
| 87 |
// whee |
| 88 |
case <-time.After(10 * time.Millisecond): |
| 89 |
c.Fatal("runner blocked too easily") |
| 90 |
} |
| 91 |
} |