github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/observer/fakeobserver/instance.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package fakeobserver
     5  
     6  import (
     7  	"net/http"
     8  	"runtime"
     9  
    10  	"gopkg.in/juju/names.v2"
    11  
    12  	"strings"
    13  
    14  	"github.com/juju/juju/rpc"
    15  	"github.com/juju/testing"
    16  )
    17  
    18  // Instance is a fake Observer used for testing.
    19  type Instance struct {
    20  	testing.Stub
    21  }
    22  
    23  // Join implements Observer.
    24  func (f *Instance) Join(req *http.Request, connectionID uint64) {
    25  	f.AddCall(funcName(), req, connectionID)
    26  }
    27  
    28  // Leave implements Observer.
    29  func (f *Instance) Leave() {
    30  	f.AddCall(funcName())
    31  }
    32  
    33  // Login implements Observer.
    34  func (f *Instance) Login(entity names.Tag, model names.ModelTag, fromController bool, userData string) {
    35  	f.AddCall(funcName(), entity, model, fromController, userData)
    36  }
    37  
    38  // RPCObserver implements Observer.
    39  func (f *Instance) RPCObserver() rpc.Observer {
    40  	f.AddCall(funcName())
    41  	return &RPCInstance{}
    42  }
    43  
    44  // RPCInstance is a fake RPCObserver used for testing.
    45  type RPCInstance struct {
    46  	testing.Stub
    47  }
    48  
    49  // ServerReply implements Observer.
    50  func (f *RPCInstance) ServerReply(req rpc.Request, hdr *rpc.Header, body interface{}) {
    51  	f.AddCall(funcName(), req, hdr, body)
    52  }
    53  
    54  // ServerRequest implements Observer.
    55  func (f *RPCInstance) ServerRequest(hdr *rpc.Header, body interface{}) {
    56  	f.AddCall(funcName(), hdr, body)
    57  }
    58  
    59  // funcName returns the name of the function/method that called
    60  // funcName() It panics if this is not possible.
    61  func funcName() string {
    62  	if pc, _, _, ok := runtime.Caller(1); ok == false {
    63  		panic("could not find function name")
    64  	} else {
    65  		parts := strings.Split(runtime.FuncForPC(pc).Name(), ".")
    66  		return parts[len(parts)-1]
    67  	}
    68  }