forked from juju/juju
/
instance.go
70 lines (57 loc) · 1.72 KB
/
instance.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package fakeobserver
import (
"net/http"
"runtime"
"strings"
"github.com/juju/names/v4"
"github.com/juju/testing"
"github.com/DavinZhang/juju/rpc"
)
// Instance is a fake Observer used for testing.
type Instance struct {
testing.Stub
}
// Join implements Observer.
func (f *Instance) Join(req *http.Request, connectionID uint64) {
f.AddCall(funcName(), req, connectionID)
}
// Leave implements Observer.
func (f *Instance) Leave() {
f.AddCall(funcName())
}
// Login implements Observer.
func (f *Instance) Login(entity names.Tag, model names.ModelTag, fromController bool, userData string) {
f.AddCall(funcName(), entity, model, fromController, userData)
}
// RPCObserver implements Observer.
func (f *Instance) RPCObserver() rpc.Observer {
// Stash the instance away in the call so that we can check calls
// on it later.
result := &RPCInstance{}
f.AddCall(funcName(), result)
return result
}
// RPCInstance is a fake RPCObserver used for testing.
type RPCInstance struct {
testing.Stub
}
// ServerReply implements Observer.
func (f *RPCInstance) ServerReply(req rpc.Request, hdr *rpc.Header, body interface{}) {
f.AddCall(funcName(), req, hdr, body)
}
// ServerRequest implements Observer.
func (f *RPCInstance) ServerRequest(hdr *rpc.Header, body interface{}) {
f.AddCall(funcName(), hdr, body)
}
// funcName returns the name of the function/method that called
// funcName() It panics if this is not possible.
func funcName() string {
if pc, _, _, ok := runtime.Caller(1); ok == false {
panic("could not find function name")
} else {
parts := strings.Split(runtime.FuncForPC(pc).Name(), ".")
return parts[len(parts)-1]
}
}