Merge pull request #354 from ivanpi-mtv/pg-test-sh-fix-refactor

playground: Refactor playground-test to use pg_test_util.
diff --git a/client/content/playgrounds/code/fortune/ex0-go/src/client/client.go b/client/content/playgrounds/code/fortune/ex0-go/src/client/client.go
index 65725f6..140e270 100644
--- a/client/content/playgrounds/code/fortune/ex0-go/src/client/client.go
+++ b/client/content/playgrounds/code/fortune/ex0-go/src/client/client.go
@@ -6,19 +6,15 @@
 	"time"
 
 	_ "v.io/core/veyron/profiles"
-	"v.io/core/veyron2/rt"
+	"v.io/core/veyron2"
 
 	"fortune"
 )
 
 func main() {
-	// Create the runtime and context.
-	runtime, err := rt.New()
-	if err != nil {
-		panic(err)
-	}
-	defer runtime.Cleanup()
-	ctx := runtime.NewContext()
+	// Initialize Vanadium.
+	ctx, shutdown := veyron2.Init()
+	defer shutdown()
 
 	// Create a new stub that binds to address without
 	// using the name service.
@@ -28,6 +24,7 @@
 	// We do this in a loop to give the server time to start up.
 	var fortune string
 	for {
+		var err error
 		if fortune, err = stub.Get(ctx); err == nil {
 			break
 		}
diff --git a/client/content/playgrounds/code/fortune/ex0-go/src/server/server.go b/client/content/playgrounds/code/fortune/ex0-go/src/server/server.go
index ef1f782..3b0966e 100644
--- a/client/content/playgrounds/code/fortune/ex0-go/src/server/server.go
+++ b/client/content/playgrounds/code/fortune/ex0-go/src/server/server.go
@@ -10,7 +10,6 @@
 	vflag "v.io/core/veyron/security/flag"
 	"v.io/core/veyron2"
 	"v.io/core/veyron2/ipc"
-	"v.io/core/veyron2/rt"
 
 	"fortune"
 )
@@ -48,13 +47,9 @@
 
 // Main - Set everything up.
 func main() {
-	// Create the runtime and context.
-	runtime, err := rt.New()
-	if err != nil {
-		panic(err)
-	}
-	defer runtime.Cleanup()
-	ctx := runtime.NewContext()
+	// Initialize Vanadium.
+	ctx, shutdown := veyron2.Init()
+	defer shutdown()
 	log := veyron2.GetLogger(ctx)
 
 	// Create a new instance of the runtime's server functionality.
diff --git a/client/content/playgrounds/code/fortune/ex0-js/client.js b/client/content/playgrounds/code/fortune/ex0-js/client.js
index 6e3e8a2..7b61c14 100644
--- a/client/content/playgrounds/code/fortune/ex0-js/client.js
+++ b/client/content/playgrounds/code/fortune/ex0-js/client.js
@@ -2,7 +2,7 @@
 var context = veyron.context;
 
 /**
- * Create a Veyron runtime using the configuration defined in config.js,
+ * Create a Vanadium runtime using the configuration defined in config.js,
  * and bind it to the bakery/cookie/fortune service.
  */
 veyron.init(function(err, rt){
diff --git a/client/content/playgrounds/code/fortune/ex0-js/server.js b/client/content/playgrounds/code/fortune/ex0-js/server.js
index 03c49a3..f3ab264 100644
--- a/client/content/playgrounds/code/fortune/ex0-js/server.js
+++ b/client/content/playgrounds/code/fortune/ex0-js/server.js
@@ -37,7 +37,7 @@
  * 2) Publish the fortune service
  */
 
-// Create a Veyron runtime using the configuration
+// Create a Vanadium runtime using the configuration
 veyron.init().then(function(rt){
   // Serve the fortune server under a name. Serve returns a Promise object
   rt.serve('bakery/cookie/fortune', fortuneService).then(function() {
diff --git a/client/playground/client/embedded.js b/client/playground/client/embedded.js
index 59df4f7..838fa8b 100644
--- a/client/playground/client/embedded.js
+++ b/client/playground/client/embedded.js
@@ -193,21 +193,18 @@
   // that if both events are triggered, both are executed before the run is
   // ended by either.
   req.on('error', ifRunActive(function(err) {
-    console.log('Connection error: ' + err.message + '\n' + err.stack);
+    console.error('Connection error: ' + err.message + '\n' + err.stack);
     appendToConsole(makeEvent('syserr', 'Error connecting to server.'));
     process.nextTick(endRunIfActive);
   }));
 
-  req.on('close', ifRunActive(function() {
-    process.nextTick(endRunIfActive);
-  }));
+  // Holds partial prefix of next response line.
+  var partialLine = '';
 
   req.on('response', ifRunActive(function(res) {
     if (res.statusCode !== 0 && res.statusCode !== 200) {
       appendToConsole(makeEvent('syserr', 'HTTP status ' + res.statusCode));
     }
-    // Holds partial prefix of next line.
-    var partialLine = '';
     res.on('data', ifRunActive(function(chunk) {
       // Each complete line is one JSON Event.
       var eventsJson = (partialLine + chunk).split('\n');
@@ -217,20 +214,32 @@
         // Ignore empty lines.
         line = line.trim();
         if (line) {
+          var ev;
           try {
-            events.push(JSON.parse(line));
+            ev = JSON.parse(line);
           } catch (err) {
-            console.error(err);
+            console.error('Error parsing line: ' + line + '\n' + err.message);
             events.push(makeEvent('syserr', 'Error parsing server response.'));
             endRunIfActive();
             return false;
           }
+          events.push(ev);
         }
       });
       appendToConsole(events);
     }));
   }));
 
+  req.on('close', ifRunActive(function() {
+    // Sanity check: partialLine should be empty when connection is closed.
+    partialLine = partialLine.trim();
+    if (partialLine) {
+      console.error('Connection closed without newline after: ' + partialLine);
+      appendToConsole(makeEvent('syserr', 'Error parsing server response.'));
+    }
+    process.nextTick(endRunIfActive);
+  }));
+
   req.write(JSON.stringify(reqData));
   req.end();
 };