Inital Commit
[oweals/finalsclub.git] / node_modules / mysql / test / slow / test-client-connect.js
1 var common = require('../common');
2 var assert = require('assert');
3 var test = common.fastOrSlow.slow();
4
5 test.before(function() {
6   this.client = common.createClient();
7 });
8
9 test.after(function(done) {
10   var connected = this.client.connected;
11   this.client.end(function(err) {
12     (connected)
13       ? assert.equal(err, null)
14       : assert.notEqual(err, null);
15
16     done();
17   });
18 });
19
20 test('Client tries to connect automatically', function(done) {
21   this.client.query('SELECT 1', function(err, results) {
22     assert.deepEqual(results, [{1: 1}]);
23     done(err);
24   });
25 });
26
27 test('Connection errors are delegated to callback', function(done) {
28   // Port number outside of range -> triggers connection error
29   this.client.port = 999999999;
30
31   var callbacks = [];
32   this.client.query('SELECT 1', function(err) {
33     assert.ok(err);
34     callbacks.push(1);
35   });
36
37   this.client.query('SELECT 2', function(err) {
38     callbacks.push(2);
39     assert.ok(err);
40
41     assert.deepEqual(callbacks, [1, 2]);
42     done(null);
43   });
44 });
45
46 test('Bad credentials', function(done) {
47   this.client.password = 'thispassworddoesnotreallywork';
48
49   var callbacks = [];
50   this.client.query('SELECT 1', function(err) {
51     assert.ok(err);
52     callbacks.push(1);
53   });
54
55   this.client.query('SELECT 2', function(err) {
56     assert.ok(err);
57     callbacks.push(2);
58
59     assert.deepEqual(callbacks, [1, 2]);
60     done();
61   });
62 });
63
64 test('Reconnecting a closed client works', function(done) {
65   var cbs = [];
66   this.client.query('SELECT 1', function() {
67     cbs.push(1);
68   });
69
70   this.client.end(function() {
71     cbs.push(2);
72   });
73
74   this.client.query('SELECT 1', function(err) {
75     assert.deepEqual(cbs, [1, 2]);
76     done(err);
77   });
78 });
79
80 test('Reconnect on timeout', {timeout: 2000}, function(done) {
81   // Not sure if we need all 3 of these, but they do the trick
82   this.client.query('SET interactive_timeout = 1');
83   this.client.query('SET wait_timeout = 1');
84   this.client.query('SET net_read_timeout = 1');
85
86   var self = this;
87   this.client._socket.on('end', function() {
88     assert.equal(self.client.connected, false);
89
90     self.client.query('SELECT 1', function(err) {
91       done(err);
92     });
93   });
94 });