veyron2: Add BackgroundContext.

Also remove InitForTest, we allow calling Init as long as it's totally serial.

Change-Id: I8951d5dbf6234839f0447ec07f048f4053ad88d8
diff --git a/runtimes/google/rt/rtx_test.go b/runtimes/google/rt/rtx_test.go
index 9073df7..3e72a03 100644
--- a/runtimes/google/rt/rtx_test.go
+++ b/runtimes/google/rt/rtx_test.go
@@ -32,7 +32,7 @@
 }
 
 func TestInitX(t *testing.T) {
-	ctx, shutdown := veyron2.InitForTest()
+	ctx, shutdown := veyron2.Init()
 	defer shutdown()
 
 	l := veyron2.GetLogger(ctx)
@@ -58,7 +58,7 @@
 }
 
 func childX(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
-	ctx, shutdown := veyron2.InitForTest()
+	ctx, shutdown := veyron2.Init()
 	defer shutdown()
 
 	logger := veyron2.GetLogger(ctx)
@@ -124,7 +124,7 @@
 }
 
 func principalX(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
-	ctx, shutdown := veyron2.InitForTest()
+	ctx, shutdown := veyron2.Init()
 	defer shutdown()
 
 	p := veyron2.GetPrincipal(ctx)
@@ -138,7 +138,7 @@
 // Runner runs a principal as a subprocess and reports back with its
 // own security info and it's childs.
 func runnerX(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
-	ctx, shutdown := veyron2.InitForTest()
+	ctx, shutdown := veyron2.Init()
 	defer shutdown()
 
 	p := veyron2.GetPrincipal(ctx)
diff --git a/runtimes/google/rt/runtimex.go b/runtimes/google/rt/runtimex.go
index 0ff2d36..c2a5ac3 100644
--- a/runtimes/google/rt/runtimex.go
+++ b/runtimes/google/rt/runtimex.go
@@ -49,6 +49,7 @@
 	listenSpecKey
 	protocolsKey
 	publisherKey
+	backgroundKey
 )
 
 // TODO(suharshs,mattr): Panic instead of flagsOnce after the transition to veyron.Init is completed.
@@ -194,6 +195,8 @@
 	// Initialize the config publisher.
 	ctx = context.WithValue(ctx, publisherKey, config.NewPublisher())
 
+	ctx = r.SetBackgroundContext(ctx)
+
 	// TODO(suharshs,mattr): Go through the rt.Cleanup function and make sure everything
 	// gets cleaned up.
 
@@ -509,3 +512,23 @@
 	publisher, _ := ctx.Value(publisherKey).(*config.Publisher)
 	return publisher
 }
+
+func (*RuntimeX) SetBackgroundContext(ctx *context.T) *context.T {
+	// Note we add an extra context with a nil value here.
+	// This prevents users from travelling back through the
+	// chain of background contexts.
+	ctx = context.WithValue(ctx, backgroundKey, nil)
+	return context.WithValue(ctx, backgroundKey, ctx)
+}
+
+func (*RuntimeX) GetBackgroundContext(ctx *context.T) *context.T {
+	bctx, _ := ctx.Value(backgroundKey).(*context.T)
+	if bctx == nil {
+		// There should always be a background context.  If we don't find
+		// it, that means that the user passed us the background context
+		// in hopes of following the chain.  Instead we just give them
+		// back what they sent in, which is correct.
+		return ctx
+	}
+	return bctx
+}
diff --git a/runtimes/google/rt/runtimex_test.go b/runtimes/google/rt/runtimex_test.go
index d74041d..ab1eba5 100644
--- a/runtimes/google/rt/runtimex_test.go
+++ b/runtimes/google/rt/runtimex_test.go
@@ -121,3 +121,19 @@
 		}
 	}
 }
+
+func TestBackgroundContext(t *testing.T) {
+	r, ctx, shutdown := InitForTest(t)
+	defer shutdown()
+
+	bgctx := r.GetBackgroundContext(ctx)
+
+	if bgctx == ctx {
+		t.Error("The background context should not be the same as the context")
+	}
+
+	bgctx2 := r.GetBackgroundContext(bgctx)
+	if bgctx != bgctx2 {
+		t.Error("Calling GetBackgroundContext a second time should return the same context.")
+	}
+}