forked from falconair/nodefix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestcaserunner.js
176 lines (144 loc) · 5.76 KB
/
testcaserunner.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var fs = require('fs');
var net = require('net');
var fix = require('./fix.js');
var fixutil = require('./fixutils.js');
var _ = require('underscore');
_.mixin(require('underscore.string'));
var file = process.argv[2];
fs.readFile(file,encoding='UTF8', function (err, data) {
if (err) throw err;
var self = this;
var lines = data.split('\n');
var commandQ = new Queue();
var fixServer = null;
var fixClient = null;
_.each(lines,function(str){
var c = str.charAt(0);
if(c==='i' || c==='e' || c==='I' || c==='E'){
commandQ.queue(str);
}
});
var str = commandQ.dequeue();
processCommand(str);
function processCommand(str){
console.log("Processing "+str);
var direction = str.charAt(0);
var msg = _.trim(str.substr(1,str.length));
if(direction=== '#'){ return ;}
//initiate connection
if(direction=== 'i'){
self.fixServer = fix.createServer({},function(session){
//console.log("EVENT connect");
//session.on("end", function(sender,target){ console.log("EVENT end"); });
//session.on("logon", function(sender, target){ console.log("EVENT logon: "+ sender + ", " + target); });
//session.on("incomingmsg", function(sender,target,msg){ console.log("Server incomingmsg: "+ JSON.stringify(msg)); });
//session.on("outgoingmsg", function(sender,target,msg){ console.log("Server outgoingmsg: "+ JSON.stringify(msg)); });
});
self.fixServer.listen(1234, "localhost", function(){
//'listen' callback
//start fix client
self.fixClient = fix.createClient("FIX.4.2", "initiator", "acceptor",{sendHeartbeats:false});
self.fixClient.connect(1234,"localhost");
self.fixClient.on("connect", function(){
console.log("Client connected");
if(!_.startsWith(commandQ.peek(), "E")){
processCommand(commandQ.dequeue());
}
});
self.fixClient.on("incomingmsg", function(sender, target,msg){
//console.log("Client incomingmsg:"+JSON.stringify(msg));
var expectedRaw = commandQ.dequeue();
console.log("Processing "+expectedRaw);
if(!_.startsWith(expectedRaw,"E")){
console.log("ERROR: expected an 'E' command but received: "+expectedRaw);
return;//throw error
}
var expected = _.trim(expectedRaw.substr(1,expectedRaw.length));
var expectedMap = fixutil.convertToMap(expected);
var errorlist = compareMapFIX(msg, expectedMap);
if(errorlist.length > 0){
console.log("ERROR: "+JSON.stringify(errorlist));
return;//throw error
}
if(!_.startsWith(commandQ.peek(), "E")){
processCommand(commandQ.dequeue());
}
});
});
}
//expected disconnect
if(direction=== 'e'){
self.fixClient.logoff();
self.fixServer.logoff();
}
//msgs sent to fix engine
if(direction === 'I'){
var map = fixutil.convertToMap(msg);
//var fixmap = fixutil.convertRawToFIX(map);
self.fixClient.write(map);
if(!_.startsWith(commandQ.peek(), "E")){
processCommand(commandQ.dequeue());
}
}
//msgs expected from fix engine
if(direction === 'E'){
//expectedQ.queue(str);
}
};
});
//compare FIX messages
function compareStringFIX(fixActual, fixExpected){
var actual = fixutil.convertToMap(fixActual);
var expected = fixutil.convertToMap(fixExpected);
compareMapFIX(actual, expected);
}
function compareMapFIX(actual, expected){
var errorlist = new Queue();
//remove time sensitive keys
delete actual[9];
delete actual[10];
delete actual[52];
delete expected[9];
delete expected[10];
delete expected[52];
var isequal = _.isEqual(actual,expected);
if(!isequal){
console.log("errors found:\n Expected msg:"+JSON.stringify(expected)+"\n Actual msg :"+JSON.stringify(actual));
_.each(actual, function(val, tag){
var tagmatches = expected[tag] === val;
if(!tagmatches){
console.log(" Tag "+tag+" expected value "+expected[tag]+" but received "+val);
var errorobj = {actualMsg:actual, expectedMsg:expected, tag:tag, actualTagVal:val, expectedTagVal:expected[tag]};
errorlist.queue(errorobj);
}
});
}
return errorlist;
}
//Queue data structure from wikipedia
//http://en.wikipedia.org/wiki/Queue_(data_structure)#Example_Javascript_code
function Queue(){
this.data = [];
this.head = 0;
this.count = 0;
this.size = function(){
return this.count < 0 ? 0 : this.count;
}
this.queue = function(obj){
this.data[this.count++] = obj;
}
this.dequeue = function(){
//Amortizes the shrink frequency of the queue
if(--this.count < this.data.length*0.9){
this.data = this.data.slice(this.head);
this.head = 0;
}
return this.data[this.head++];
}
this.peek = function(){
return this.data[this.head];
}
this.isEmpty = function(){
return this.count <= 0;
}
}