1
// Copyright 2011, 2012, 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
9
gc "launchpad.net/gocheck"
11
jc "launchpad.net/juju-core/testing/checkers"
14
// NotifyAsserterC gives helper functions for making assertions about how a
15
// channel operates (whether we get a receive event or not, whether it is
17
type NotifyAsserterC struct {
18
// C is a gocheck C structure for doing assertions
20
// Chan is the channel we want to receive on
22
// Precond will be called before waiting on the channel, can be nil
26
// AssertReceive will ensure that we get an event on the channel and the
27
// channel is not closed.
28
func (a *NotifyAsserterC) AssertReceive() {
33
case _, ok := <-a.Chan:
34
a.C.Assert(ok, jc.IsTrue)
35
case <-time.After(LongWait):
36
a.C.Fatalf("timed out waiting for channel message")
40
// AssertOneReceive checks that we have exactly one message, and no more
41
func (a *NotifyAsserterC) AssertOneReceive() {
46
// AssertClosed ensures that we get a closed event on the channel
47
func (a *NotifyAsserterC) AssertClosed() {
52
case _, ok := <-a.Chan:
53
a.C.Assert(ok, jc.IsFalse)
54
case <-time.After(LongWait):
55
a.C.Fatalf("timed out waiting for channel to close")
59
// Assert that we fail to receive on the channel after a short wait.
60
func (a *NotifyAsserterC) AssertNoReceive() {
63
a.C.Fatalf("unexpected receive")
64
case <-time.After(ShortWait):
68
// ContentAsserterC is like NotifyAsserterC in that it checks the behavior of a
69
// channel. The difference is that we expect actual content on the channel, so
70
// callers need to put that into and out of an 'interface{}'
71
// TODO go1.1: We can use reflect.Select and reflect.Receive sort of
72
// functionality in order to avoid having to write a helper function to
73
// curry our requests into interface{} types.
75
type ContentAsserterC struct {
76
// C is a gocheck C structure for doing assertions
78
// Chan is the channel we want to receive on
79
Chan <-chan interface{}
80
// Precond will be called before waiting on the channel, can be nil
84
// AssertReceive will ensure that we get an event on the channel and the
85
// channel is not closed. It will return the content received
86
func (a *ContentAsserterC) AssertReceive() interface{} {
91
case content, ok := <-a.Chan:
92
a.C.Assert(ok, jc.IsTrue)
94
case <-time.After(LongWait):
95
a.C.Fatalf("timed out waiting for channel message")
100
// AssertOneReceive checks that we have exactly one message, and no more
101
func (a *ContentAsserterC) AssertOneReceive() interface{} {
102
res := a.AssertReceive()
107
// AssertClosed ensures that we get a closed event on the channel
108
func (a *ContentAsserterC) AssertClosed() {
109
if a.Precond != nil {
113
case _, ok := <-a.Chan:
114
a.C.Assert(ok, jc.IsFalse)
115
case <-time.After(LongWait):
116
a.C.Fatalf("timed out waiting for channel to close")
120
// Assert that we fail to receive on the channel after a short wait.
121
func (a *ContentAsserterC) AssertNoReceive() {
123
case content := <-a.Chan:
124
a.C.Fatalf("unexpected receive: %#v", content)
125
case <-time.After(ShortWait):