veyron/services/mounttable: Remove dependence on rt.R().

Change-Id: If24d6546e00e7cff21ad08da8c5e7c687b5c765c
diff --git a/runtimes/google/naming/namespace/all_test.go b/runtimes/google/naming/namespace/all_test.go
index c85a1d1..4043d3a 100644
--- a/runtimes/google/naming/namespace/all_test.go
+++ b/runtimes/google/naming/namespace/all_test.go
@@ -34,6 +34,26 @@
 	stream.RegisterProtocol("ws", websocket.Dial, nil)
 }
 
+func createRuntimes(t *testing.T) (sr, r veyron2.Runtime, cleanup func()) {
+	var err error
+	// Create a runtime for the server.
+	sr, err = rt.New()
+	if err != nil {
+		t.Fatalf("Could not initialize runtime: %v", err)
+	}
+
+	// We use a different runtime for the client side.
+	r, err = rt.New()
+	if err != nil {
+		t.Fatalf("Could not initialize runtime: %v", err)
+	}
+
+	return sr, r, func() {
+		sr.Cleanup()
+		r.Cleanup()
+	}
+}
+
 func boom(t *testing.T, f string, v ...interface{}) {
 	t.Logf(f, v...)
 	t.Fatal(string(debug.Stack()))
@@ -294,10 +314,9 @@
 // TestNamespaceCommon tests common use of the Namespace library
 // against a root mount table and some mount tables mounted on it.
 func TestNamespaceCommon(t *testing.T) {
-	// We need the default runtime for the server-side mounttable code
-	// which references rt.R() to create new endpoints
-	rt.Init()
-	r, _ := rt.New() // We use a different runtime for the client side.
+	_, r, cleanup := createRuntimes(t)
+	defer cleanup()
+
 	root, mts, jokes, stopper := createNamespace(t, r)
 	defer stopper()
 	ns := r.Namespace()
@@ -328,8 +347,9 @@
 // TestNamespaceDetails tests more detailed use of the Namespace library,
 // including the intricacies of // meaning and placement.
 func TestNamespaceDetails(t *testing.T) {
-	sr := rt.Init()
-	r, _ := rt.New() // We use a different runtime for the client side.
+	sr, r, cleanup := createRuntimes(t)
+	defer cleanup()
+
 	root, mts, _, stopper := createNamespace(t, sr)
 	defer stopper()
 
@@ -381,8 +401,9 @@
 
 // TestNestedMounts tests some more deeply nested mounts
 func TestNestedMounts(t *testing.T) {
-	sr := rt.Init()
-	r, _ := rt.New() // We use a different runtime for the client side.
+	sr, r, cleanup := createRuntimes(t)
+	defer cleanup()
+
 	root, mts, _, stopper := createNamespace(t, sr)
 	runNestedMountTables(t, sr, mts)
 	defer stopper()
@@ -405,8 +426,9 @@
 
 // TestServers tests invoking RPCs on simple servers
 func TestServers(t *testing.T) {
-	sr := rt.Init()
-	r, _ := rt.New() // We use a different runtime for the client side.
+	sr, r, cleanup := createRuntimes(t)
+	defer cleanup()
+
 	root, mts, jokes, stopper := createNamespace(t, sr)
 	defer stopper()
 	ns := r.Namespace()
@@ -429,8 +451,9 @@
 
 // TestGlob tests some glob patterns.
 func TestGlob(t *testing.T) {
-	sr := rt.Init()
-	r, _ := rt.New() // We use a different runtime for the client side.
+	sr, r, cleanup := createRuntimes(t)
+	defer cleanup()
+
 	root, mts, _, stopper := createNamespace(t, sr)
 	runNestedMountTables(t, sr, mts)
 	defer stopper()
@@ -499,8 +522,9 @@
 
 // TestGlobEarlyStop tests that Glob doesn't query terminal servers with finished patterns.
 func TestGlobEarlyStop(t *testing.T) {
-	sr := rt.Init()
-	r, _ := rt.New() // We use a different runtime for the client side.
+	sr, r, cleanup := createRuntimes(t)
+	defer cleanup()
+
 	root, mts, _, stopper := createNamespace(t, sr)
 	runNestedMountTables(t, sr, mts)
 	defer stopper()
@@ -538,9 +562,8 @@
 }
 
 func TestCycles(t *testing.T) {
-	sr := rt.Init()
-	r, _ := rt.New() // We use a different runtime for the client side.
-	defer r.Cleanup()
+	sr, r, cleanup := createRuntimes(t)
+	defer cleanup()
 
 	root, _, _, stopper := createNamespace(t, sr)
 	defer stopper()
@@ -592,9 +615,10 @@
 	// that's annoying because the stub compiler has some blocking bugs and the
 	// Unresolve functionality is partially implemented in the stubs.
 	t.Skip()
-	sr := rt.Init()
-	r, _ := rt.New() // We use a different runtime for the client side.
-	defer r.Cleanup()
+
+	sr, r, cleanup := createRuntimes(t)
+	defer cleanup()
+
 	root, mts, jokes, stopper := createNamespace(t, sr)
 	runNestedMountTables(t, sr, mts)
 	defer stopper()
@@ -609,9 +633,9 @@
 // TestGoroutineLeaks tests for leaking goroutines - we have many:-(
 func TestGoroutineLeaks(t *testing.T) {
 	t.Skip()
-	sr := rt.Init()
-	r, _ := rt.New() // We use a different runtime for the client side.
-	defer r.Cleanup()
+	sr, _, cleanup := createRuntimes(t)
+	defer cleanup()
+
 	_, _, _, stopper := createNamespace(t, sr)
 	defer func() {
 		vlog.Infof("%d goroutines:", runtime.NumGoroutine())
@@ -624,8 +648,9 @@
 }
 
 func TestBadRoots(t *testing.T) {
-	r, _ := rt.New()
-	defer r.Cleanup()
+	_, r, cleanup := createRuntimes(t)
+	defer cleanup()
+
 	if _, err := namespace.New(r); err != nil {
 		t.Errorf("namespace.New should not have failed with no roots")
 	}
@@ -643,10 +668,8 @@
 }
 
 func TestRootBlessing(t *testing.T) {
-	// We need the default runtime for the server-side mounttable code
-	// which references rt.R() to create new endpoints
-	cr := rt.Init()
-	r, _ := rt.New() // We use a different runtime for the client side.
+	r, cr, cleanup := createRuntimes(t)
+	defer cleanup()
 
 	proot, err := vsecurity.NewPrincipal()
 	if err != nil {
diff --git a/services/mounttable/lib/mounttable.go b/services/mounttable/lib/mounttable.go
index 719a321..ed5d57b 100644
--- a/services/mounttable/lib/mounttable.go
+++ b/services/mounttable/lib/mounttable.go
@@ -11,9 +11,9 @@
 
 	"veyron.io/veyron/veyron/lib/glob"
 
+	"veyron.io/veyron/veyron2"
 	"veyron.io/veyron/veyron2/ipc"
 	"veyron.io/veyron/veyron2/naming"
-	"veyron.io/veyron/veyron2/rt"
 	"veyron.io/veyron/veyron2/security"
 	"veyron.io/veyron/veyron2/services/mounttable"
 	"veyron.io/veyron/veyron2/services/security/access"
@@ -270,7 +270,8 @@
 
 	// Make sure the server name is reasonable.
 	epString, _ := naming.SplitAddressName(server)
-	_, err := rt.R().NewEndpoint(epString)
+	runtime := veyron2.RuntimeFromContext(context)
+	_, err := runtime.NewEndpoint(epString)
 	if err != nil {
 		return fmt.Errorf("malformed address %q for mounted server %q", epString, server)
 	}
diff --git a/services/mounttable/lib/mounttable_test.go b/services/mounttable/lib/mounttable_test.go
index 7ec967c..ee79761 100644
--- a/services/mounttable/lib/mounttable_test.go
+++ b/services/mounttable/lib/mounttable_test.go
@@ -452,8 +452,11 @@
 func init() {
 	testutil.Init()
 	// Create the runtime for each of the three "processes"
-	rootRT = rt.Init()
+
 	var err error
+	if rootRT, err = rt.New(); err != nil {
+		panic(err)
+	}
 	if aliceRT, err = rt.New(); err != nil {
 		panic(err)
 	}
diff --git a/services/mounttable/lib/neighborhood.go b/services/mounttable/lib/neighborhood.go
index 7d649f9..278b0f4 100644
--- a/services/mounttable/lib/neighborhood.go
+++ b/services/mounttable/lib/neighborhood.go
@@ -9,9 +9,9 @@
 	"veyron.io/veyron/veyron/lib/glob"
 	"veyron.io/veyron/veyron/lib/netconfig"
 
+	"veyron.io/veyron/veyron2"
 	"veyron.io/veyron/veyron2/ipc"
 	"veyron.io/veyron/veyron2/naming"
-	"veyron.io/veyron/veyron2/rt"
 	"veyron.io/veyron/veyron2/security"
 	"veyron.io/veyron/veyron2/services/mounttable"
 	verror "veyron.io/veyron/veyron2/verror2"
@@ -37,10 +37,10 @@
 	nh    *neighborhood
 }
 
-func getPort(address string) uint16 {
+func getPort(r veyron2.Runtime, address string) uint16 {
 	epAddr, _ := naming.SplitAddressName(address)
 
-	ep, err := rt.R().NewEndpoint(epAddr)
+	ep, err := r.NewEndpoint(epAddr)
 	if err != nil {
 		return 0
 	}
@@ -62,14 +62,14 @@
 	return uint16(port)
 }
 
-func newNeighborhoodServer(host string, addresses []string, loopback bool) (*neighborhood, error) {
+func newNeighborhoodServer(r veyron2.Runtime, host string, addresses []string, loopback bool) (*neighborhood, error) {
 	// Create the TXT contents with addresses to announce. Also pick up a port number.
 	var txt []string
 	var port uint16
 	for _, addr := range addresses {
 		txt = append(txt, addressPrefix+addr)
 		if port == 0 {
-			port = getPort(addr)
+			port = getPort(r, addr)
 		}
 	}
 	if txt == nil {
@@ -113,13 +113,13 @@
 }
 
 // NewLoopbackNeighborhoodServer creates a new instance of a neighborhood server on loopback interfaces for testing.
-func NewLoopbackNeighborhoodServer(host string, addresses ...string) (*neighborhood, error) {
-	return newNeighborhoodServer(host, addresses, true)
+func NewLoopbackNeighborhoodServer(r veyron2.Runtime, host string, addresses ...string) (*neighborhood, error) {
+	return newNeighborhoodServer(r, host, addresses, true)
 }
 
 // NewNeighborhoodServer creates a new instance of a neighborhood server.
-func NewNeighborhoodServer(host string, addresses ...string) (*neighborhood, error) {
-	return newNeighborhoodServer(host, addresses, false)
+func NewNeighborhoodServer(r veyron2.Runtime, host string, addresses ...string) (*neighborhood, error) {
+	return newNeighborhoodServer(r, host, addresses, false)
 }
 
 // Lookup implements ipc.Dispatcher.Lookup.
diff --git a/services/mounttable/lib/neighborhood_test.go b/services/mounttable/lib/neighborhood_test.go
index 058b60a..eb507b9 100644
--- a/services/mounttable/lib/neighborhood_test.go
+++ b/services/mounttable/lib/neighborhood_test.go
@@ -49,7 +49,7 @@
 	serverName := fmt.Sprintf("nhtest%d", os.Getpid())
 
 	// Add neighborhood server.
-	nhd, err := NewLoopbackNeighborhoodServer(serverName, addresses...)
+	nhd, err := NewLoopbackNeighborhoodServer(rootRT, serverName, addresses...)
 	if err != nil {
 		boom(t, "Failed to create neighborhood server: %s\n", err)
 	}
diff --git a/services/mounttable/mounttabled/mounttable.go b/services/mounttable/mounttabled/mounttable.go
index 3286652..2cbf037 100644
--- a/services/mounttable/mounttabled/mounttable.go
+++ b/services/mounttable/mounttabled/mounttable.go
@@ -23,7 +23,10 @@
 )
 
 func main() {
-	r := rt.Init()
+	r, err := rt.New()
+	if err != nil {
+		vlog.Fatalf("Could not initialize runtime: %v", err)
+	}
 	defer r.Cleanup()
 
 	mtServer, err := r.NewServer(options.ServesMountTable(true))
@@ -72,7 +75,7 @@
 
 		myObjectName := naming.JoinAddressName(mtEndpoint.String(), "")
 
-		nh, err := mounttable.NewNeighborhoodServer(*nhName, myObjectName)
+		nh, err := mounttable.NewNeighborhoodServer(r, *nhName, myObjectName)
 		if err != nil {
 			vlog.Errorf("NewNeighborhoodServer failed: %v", err)
 			os.Exit(1)