| 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 testing |
| 18 |
|
| 19 |
// Here, the bus.Endpoint implementation. |
| 20 |
|
| 21 |
import ( |
| 22 |
"errors" |
| 23 |
"fmt" |
| 24 |
|
| 25 |
"github.com/godbus/dbus/v5" |
| 26 |
|
| 27 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus" |
| 28 |
"gitlab.com/ubports/development/core/lomiri-push-service/testing/condition" |
| 29 |
"sync" |
| 30 |
"time" |
| 31 |
) |
| 32 |
|
| 33 |
type callArgs struct { |
| 34 |
Member string |
| 35 |
Args []interface{} |
| 36 |
} |
| 37 |
|
| 38 |
type testingEndpoint struct { |
| 39 |
dialCond condition.Interface |
| 40 |
callCond condition.Interface |
| 41 |
usedLck sync.Mutex |
| 42 |
used int |
| 43 |
retvals [][]interface{} |
| 44 |
watchSources map[string]chan []interface{} |
| 45 |
watchLck sync.RWMutex |
| 46 |
callArgs []callArgs |
| 47 |
callArgsLck sync.RWMutex |
| 48 |
} |
| 49 |
|
| 50 |
// Build a bus.Endpoint that calls OK() on its condition before returning |
| 51 |
// the provided return values. |
| 52 |
// |
| 53 |
// NOTE: Call() always returns the first return value; Watch() will provide |
| 54 |
// each of them in turn, irrespective of whether Call has been called. |
| 55 |
func NewMultiValuedTestingEndpoint(dialCond condition.Interface, callCond condition.Interface, retvalses ...[]interface{}) bus.Endpoint { |
| 56 |
return &testingEndpoint{dialCond: dialCond, callCond: callCond, retvals: retvalses, watchSources: make(map[string]chan []interface{})} |
| 57 |
} |
| 58 |
|
| 59 |
func NewTestingEndpoint(dialCond condition.Interface, callCond condition.Interface, retvals ...interface{}) bus.Endpoint { |
| 60 |
retvalses := make([][]interface{}, len(retvals)) |
| 61 |
for i, x := range retvals { |
| 62 |
retvalses[i] = []interface{}{x} |
| 63 |
} |
| 64 |
return &testingEndpoint{dialCond: dialCond, callCond: callCond, retvals: retvalses, watchSources: make(map[string]chan []interface{})} |
| 65 |
} |
| 66 |
|
| 67 |
// If SetWatchSource is called with a non-nil watchSource, it is used |
| 68 |
// instead of the default timeout and retvals to get values to send |
| 69 |
// over WatchSignal. Set it to nil again to restore default behaviour. |
| 70 |
func SetWatchSource(tc bus.Endpoint, member string, watchSource chan []interface{}) { |
| 71 |
tc.(*testingEndpoint).watchLck.Lock() |
| 72 |
tc.(*testingEndpoint).watchSources[member] = watchSource |
| 73 |
tc.(*testingEndpoint).watchLck.Unlock() |
| 74 |
} |
| 75 |
|
| 76 |
// GetCallArgs returns a list of the arguments for each Call() invocation. |
| 77 |
func GetCallArgs(tc bus.Endpoint) []callArgs { |
| 78 |
tc.(*testingEndpoint).callArgsLck.RLock() |
| 79 |
defer tc.(*testingEndpoint).callArgsLck.RUnlock() |
| 80 |
return tc.(*testingEndpoint).callArgs |
| 81 |
} |
| 82 |
|
| 83 |
type watchCancel struct { |
| 84 |
done chan struct{} |
| 85 |
cancelled chan struct{} |
| 86 |
lck sync.Mutex |
| 87 |
member string |
| 88 |
} |
| 89 |
|
| 90 |
// this waits for actual cancelllation for test convenience |
| 91 |
func (wc *watchCancel) Cancel() { |
| 92 |
wc.lck.Lock() |
| 93 |
defer wc.lck.Unlock() |
| 94 |
if wc.cancelled != nil { |
| 95 |
close(wc.cancelled) |
| 96 |
wc.cancelled = nil |
| 97 |
<-wc.done |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// See Endpoint's WatchSignal. This WatchSignal will check its condition to |
| 102 |
// decide whether to return an error, or provide each of its return values |
| 103 |
// or values from the previously set watchSource for member. |
| 104 |
func (tc *testingEndpoint) WatchSignal(member string, f func(...interface{}), d func()) (bus.Cancellable, error) { |
| 105 |
if tc.callCond.OK() { |
| 106 |
cancelled := make(chan struct{}) |
| 107 |
done := make(chan struct{}) |
| 108 |
go func() { |
| 109 |
tc.watchLck.RLock() |
| 110 |
source := tc.watchSources[member] |
| 111 |
tc.watchLck.RUnlock() |
| 112 |
if source == nil { |
| 113 |
tc.usedLck.Lock() |
| 114 |
idx := tc.used |
| 115 |
tc.used++ |
| 116 |
tc.usedLck.Unlock() |
| 117 |
source = make(chan []interface{}) |
| 118 |
go func() { |
| 119 |
Feed: |
| 120 |
for _, v := range tc.retvals[idx:] { |
| 121 |
select { |
| 122 |
case source <- v: |
| 123 |
case <-cancelled: |
| 124 |
break Feed |
| 125 |
} |
| 126 |
select { |
| 127 |
case <-time.After(10 * time.Millisecond): |
| 128 |
case <-cancelled: |
| 129 |
break Feed |
| 130 |
} |
| 131 |
} |
| 132 |
close(source) |
| 133 |
}() |
| 134 |
} |
| 135 |
Receive: |
| 136 |
for { |
| 137 |
select { |
| 138 |
case v, ok := <-source: |
| 139 |
if !ok { |
| 140 |
break Receive |
| 141 |
} |
| 142 |
f(v...) |
| 143 |
case <-cancelled: |
| 144 |
break Receive |
| 145 |
} |
| 146 |
} |
| 147 |
d() |
| 148 |
close(done) |
| 149 |
}() |
| 150 |
return &watchCancel{cancelled: cancelled, done: done, member: member}, nil |
| 151 |
} else { |
| 152 |
return nil, errors.New("no way") |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
// See Endpoint's WatchProperties. |
| 157 |
func (tc *testingEndpoint) WatchProperties(f func(map[string]dbus.Variant, []string), d func()) (bus.Cancellable, error) { |
| 158 |
translate := func(vals ...interface{}) { |
| 159 |
changed := vals[0].(map[string]dbus.Variant) |
| 160 |
invalidated := vals[1].([]string) |
| 161 |
f(changed, invalidated) |
| 162 |
} |
| 163 |
return tc.WatchSignal("PropertiesChanged", translate, d) |
| 164 |
} |
| 165 |
|
| 166 |
// See Endpoint's Call. This Call will check its condition to decide whether |
| 167 |
// to return an error, or the first of its return values |
| 168 |
func (tc *testingEndpoint) Call(member string, args []interface{}, rvs ...interface{}) error { |
| 169 |
tc.callArgsLck.Lock() |
| 170 |
defer tc.callArgsLck.Unlock() |
| 171 |
|
| 172 |
tc.callArgs = append(tc.callArgs, callArgs{member, args}) |
| 173 |
if tc.callCond.OK() { |
| 174 |
expected := len(rvs) |
| 175 |
var provided int |
| 176 |
tc.usedLck.Lock() |
| 177 |
idx := tc.used |
| 178 |
tc.used++ |
| 179 |
tc.usedLck.Unlock() |
| 180 |
if len(tc.retvals) <= idx { |
| 181 |
if expected != 0 { |
| 182 |
panic("No return values provided!") |
| 183 |
} |
| 184 |
provided = 0 |
| 185 |
} else { |
| 186 |
provided = len(tc.retvals[idx]) |
| 187 |
} |
| 188 |
if provided != expected { |
| 189 |
return errors.New("provided/expected return vals mismatch") |
| 190 |
} |
| 191 |
if provided != 0 { |
| 192 |
if err := dbus.Store(tc.retvals[idx], rvs...); err != nil { |
| 193 |
return err |
| 194 |
} |
| 195 |
} |
| 196 |
return nil |
| 197 |
} else { |
| 198 |
return errors.New("no way") |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
// See Endpoint's GetProperty. This one is just another name for Call. |
| 203 |
func (tc *testingEndpoint) GetProperty(property string) (interface{}, error) { |
| 204 |
var res interface{} |
| 205 |
err := tc.Call(property, bus.Args(), &res) |
| 206 |
if err != nil { |
| 207 |
return nil, err |
| 208 |
} |
| 209 |
return res, err |
| 210 |
} |
| 211 |
|
| 212 |
// See Endpoint's SetProperty. This one does nothing beyond |
| 213 |
// registering being called. |
| 214 |
func (tc *testingEndpoint) SetProperty(property string, suffix string, value interface{}) error { |
| 215 |
tc.callArgsLck.Lock() |
| 216 |
defer tc.callArgsLck.Unlock() |
| 217 |
|
| 218 |
args := callArgs{ |
| 219 |
Member: "::SetProperty", |
| 220 |
Args: []interface{}{property, suffix, value}, |
| 221 |
} |
| 222 |
tc.callArgs = append(tc.callArgs, args) |
| 223 |
|
| 224 |
return nil |
| 225 |
} |
| 226 |
|
| 227 |
// See Endpoint's Dial. This one will check its dialCondition to |
| 228 |
// decide whether to return an error or not. |
| 229 |
func (endp *testingEndpoint) Dial() error { |
| 230 |
if endp.dialCond.OK() { |
| 231 |
return nil |
| 232 |
} else { |
| 233 |
return errors.New("dialCond said No.") |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
// Advanced stringifobabulation |
| 238 |
func (endp *testingEndpoint) String() string { |
| 239 |
return fmt.Sprintf("&testingEndpoint{dialCond:(%s) callCond:(%s) retvals:(%#v)", |
| 240 |
endp.dialCond, endp.callCond, endp.retvals) |
| 241 |
} |
| 242 |
|
| 243 |
// see Endpoint's Close. This one does nothing beyond registering |
| 244 |
// being called. |
| 245 |
func (tc *testingEndpoint) Close() { |
| 246 |
tc.callArgsLck.Lock() |
| 247 |
defer tc.callArgsLck.Unlock() |
| 248 |
|
| 249 |
args := callArgs{Member: "::Close"} |
| 250 |
tc.callArgs = append(tc.callArgs, args) |
| 251 |
} |
| 252 |
|
| 253 |
func (tc *testingEndpoint) GrabName(allowReplacement bool) <-chan error { |
| 254 |
tc.callArgsLck.Lock() |
| 255 |
defer tc.callArgsLck.Unlock() |
| 256 |
|
| 257 |
args := callArgs{Member: "::GrabName"} |
| 258 |
args.Args = append(args.Args, allowReplacement) |
| 259 |
tc.callArgs = append(tc.callArgs, args) |
| 260 |
|
| 261 |
return nil |
| 262 |
} |
| 263 |
|
| 264 |
func (tc *testingEndpoint) WatchMethod(dispatch bus.DispatchMap, suffix string) error { |
| 265 |
tc.callArgsLck.Lock() |
| 266 |
defer tc.callArgsLck.Unlock() |
| 267 |
|
| 268 |
args := callArgs{Member: "::WatchMethod"} |
| 269 |
args.Args = append(args.Args, dispatch) |
| 270 |
tc.callArgs = append(tc.callArgs, args) |
| 271 |
return nil |
| 272 |
} |
| 273 |
|
| 274 |
func (tc *testingEndpoint) Signal(member string, suffix string, args []interface{}) error { |
| 275 |
tc.callArgsLck.Lock() |
| 276 |
defer tc.callArgsLck.Unlock() |
| 277 |
|
| 278 |
callargs := callArgs{Member: "::Signal"} |
| 279 |
callargs.Args = append(callargs.Args, member, suffix, args) |
| 280 |
tc.callArgs = append(tc.callArgs, callargs) |
| 281 |
return nil |
| 282 |
} |
| 283 |
|
| 284 |
// ensure testingEndpoint implements bus.Endpoint |
| 285 |
var _ bus.Endpoint = (*testingEndpoint)(nil) |