github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"strings"
    10  
    11  	"github.com/juju/testing"
    12  	"gopkg.in/juju/names.v2"
    13  
    14  	"github.com/juju/juju/rpc"
    15  )
    16  
    17  // Instance is a fake Observer used for testing.
    18  type Instance struct {
    19  	testing.Stub
    20  }
    21  
    22  // Join implements Observer.
    23  func (f *Instance) Join(req *http.Request, connectionID uint64) {
    24  	f.AddCall(funcName(), req, connectionID)
    25  }
    26  
    27  // Leave implements Observer.
    28  func (f *Instance) Leave() {
    29  	f.AddCall(funcName())
    30  }
    31  
    32  // Login implements Observer.
    33  func (f *Instance) Login(entity names.Tag, model names.ModelTag, fromController bool, userData string) {
    34  	f.AddCall(funcName(), entity, model, fromController, userData)
    35  }
    36  
    37  // RPCObserver implements Observer.
    38  func (f *Instance) RPCObserver() rpc.Observer {
    39  	// Stash the instance away in the call so that we can check calls
    40  	// on it later.
    41  	result := &RPCInstance{}
    42  	f.AddCall(funcName(), result)
    43  	return result
    44  }
    45  
    46  // RPCInstance is a fake RPCObserver used for testing.
    47  type RPCInstance struct {
    48  	testing.Stub
    49  }
    50  
    51  // ServerReply implements Observer.
    52  func (f *RPCInstance) ServerReply(req rpc.Request, hdr *rpc.Header, body interface{}) {
    53  	f.AddCall(funcName(), req, hdr, body)
    54  }
    55  
    56  // ServerRequest implements Observer.
    57  func (f *RPCInstance) ServerRequest(hdr *rpc.Header, body interface{}) {
    58  	f.AddCall(funcName(), hdr, body)
    59  }
    60  
    61  // funcName returns the name of the function/method that called
    62  // funcName() It panics if this is not possible.
    63  func funcName() string {
    64  	if pc, _, _, ok := runtime.Caller(1); ok == false {
    65  		panic("could not find function name")
    66  	} else {
    67  		parts := strings.Split(runtime.FuncForPC(pc).Name(), ".")
    68  		return parts[len(parts)-1]
    69  	}
    70  }