connector.test.js
3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import expect from 'expect.js';
import muk from 'muk';
import net from '../mock/fakeNet';
describe('connector.js', function() {
let C;
before(function() {
C = muk('../../src/lib/connector', {
'net': net
});
C.__set__('log', {
info: function() {},
error: function() {}
});
})
describe('init', function() {
it('new connector init ok', function(done) {
var connect = new C({
host: '1.1.1.1',
port: '123'
}, function(con) {
expect(con.connected).to.be(true);
done();
});
});
it('new connector error', function(done) {
var connect = new C({
host: '1.1.1.1',
port: '123'
}, function(con) {}, function(client, data, error) {
expect(error.message).to.be('mock error');
done();
});
connect.client.addEmit('error', {
message: 'mock error'
});
});
it('new connect after error then reconnect', function(done) {
var connect = new C({
host: '1.1.1.1',
port: '123'
}, function(con) {}, function() {});
connect.on('reconnect', function() {
done();
});
connect.client.addEmit('error', {
message: 'mock error'
});
});
});
describe('connector write then get data', function() {
it('on data expect ok', function(done) {
C.__set__('toHex', function() {});
C.__set__('spliteRet', function() {
return {
rets: ['mock']
}
});
C.__set__('locateOrder', function() {
return {
name: 'mock'
}
});
var connect = new C({
host: '1.1.1.1',
port: '123'
}, function(con) {}, function() {
return 'ok';
});
connect.on('mock', function(data) {
expect(data).to.be('ok');
done();
});
connect.client.write('test');
});
});
describe('sendOrder', function() {
it('send expect same back', function(done) {
var connect = new C({
host: '1.1.1.1',
port: '123'
}, function(con) {}, function() {});
C.__set__('spliteRet', function(mq) {
return {
rets: [],
mq: mq
};
});
let call = new Buffer('Mock', 'hex');
connect.client.on('data', function(data) {
expect(data).to.eql(call);
expect(connect.orderMq[0]).to.be('mo');
done();
});
connect.send('Mock');
});
})
after(function() {
muk.restore();
});
});