var common     = require('../../common');
var assert     = require('assert');

var rows = undefined;
var fields = undefined;
var err;
var server; 

function test() {
  var connection = common.createConnection({port: 3307});
  //var connection = require('mysql').createConnection({port: 3307});
  connection.query('SELECT 123', function(err, _rows, _fields) {
    if (err) throw err;

    rows = _rows;
    fields = _fields;
    connection.on('error', function(_err) {
      err = _err;
    });
    server.connections.forEach(function(conn) { conn.stream.end() } );
    server._server.close(function() {
      assert.equal(err.code, 'PROTOCOL_CONNECTION_LOST'); 
    });

    // TODO: tests for 'commands after close' behavior
    //  connection.query('SELECT 123', function(err, a, b) {
    //    console.log(err, a, b);
    //  });
  });

  // TODO: test connection.end() etc where we expect disconnect to happen
}
server = common.createServer(test);

process.on('exit', function() {
  assert.deepEqual(rows, [{1: 1}]);
  assert.equal(fields[0].name, '1');
});

@@ -84,44 +84,20 @@
 rows
- = undefined;%0Avar fields = undefined
+;%0Avar fields
 ;%0Ava
@@ -114,17 +114,16 @@
  server;
- 
 %0A%0Afuncti
@@ -196,78 +196,8 @@
 %7D);%0A
-  //var connection = require('mysql').createConnection(%7Bport: 3307%7D);%0A
   co
@@ -457,16 +457,17 @@
 am.end()
+;
  %7D );%0A  
@@ -559,17 +559,16 @@
 _LOST');
- 
 %0A    %7D);
@@ -568,19 +568,22 @@
     %7D);%0A
-%0A
   
+%7D);%0A
   // TOD
@@ -593,233 +593,393 @@
 test
-s for 'commands after close' behavior%0A    //  connection.query('SELECT 123', function(err, a, b) %7B%0A    //    console.log(err, a, b);%0A    //  %7D);%0A  %7D);%0A%0A  // TODO: test connection.end() etc where we expect disconnect to happen
+ connection.end() etc where we expect disconnect to happen%0A%7D%0A%0Afunction serverHandler(conn) %7B%0A  conn.on('query', function(q) %7B%0A    conn.writeTextResult(%5B %7B '1': '1' %7D %5D, %5B %7B catalog: 'def',%0A     schema: '',%0A     table: '',%0A     orgTable: '',%0A     name: '1',%0A     orgName: '',%0A     characterSet: 63,%0A     columnLength: 1,%0A     columnType: 8,%0A     flags: 129,%0A     decimals: 0 %7D %5D);%0A  %7D);
 %0A%7D%0As
@@ -1010,16 +1010,31 @@
 ver(test
+, serverHandler
 );%0A%0Aproc

fix new mock server api change