diff --git a/spec/support/jasmine.json b/spec/support/jasmine.json new file mode 100644 index 0000000..3a079a7 --- /dev/null +++ b/spec/support/jasmine.json @@ -0,0 +1,10 @@ +{ + "spec_dir": "spec", + "spec_files": [ + + "/support/jasmineTest/clientTestCases.spec.js", + "/support/jasmineTest/serverTestCases.spec.js" + + ] + +} diff --git a/spec/support/jasmineTest/clientTestCases.spec.js b/spec/support/jasmineTest/clientTestCases.spec.js new file mode 100644 index 0000000..3dd58b0 --- /dev/null +++ b/spec/support/jasmineTest/clientTestCases.spec.js @@ -0,0 +1,439 @@ +var ipc = require('../../../../node-ipc'); + + ipc.config.id ='testClient'; + ipc.config.retry = 600; + + +describe('Test Cases for client: ', + function(){ + + it( + 'Verify retry attempts by Unix client to connect to the Unix server as per the value set in "maxRetries" parameter.', + function(done){ + + var retryAttempt = 3; //variable created to count the attempt made by client to connect to the server. + ipc.config.maxRetries = 3; + ipc.config.silent= true; + + ipc.connectTo( + 'fakeworld', + function(){ + + ipc.of.fakeworld.on( + 'disconnect', + function(){ + + if(ipc.of.fakeworld.retriesRemaining == 0){ + + expect(retryAttempt).toBe(ipc.of.fakeworld.retriesRemaining); + expect(ipc.of.fakeworld.socket.destroyed).toBe(true); + + } + else if(ipc.of.fakeworld.retriesRemaining < 0){ + + expect(retryAttempt).not.toBeLessThan(0); + expect(ipc.of.fakeworld.retriesRemaining).not.toBeLessThan(0); + + + ipc.of.fakeworld.on( + 'error', + function(err){ + console.log('Error is: ', err); + ipc.disconnect('fakeworld'); + } + ); + + } + + retryAttempt--; + } + ); + } + ); + + // Wait time is added to verify the fail case scenario of additional retry attempt by client than expected. + setTimeout( + function(){ + ipc.disconnect('fakeworld'); + done(); + },2500 + ); + + + } + ); + + it( + 'Verify Unix client does not connect to the unix server when "stopRetrying" value is set to true.', + function(done){ + + var retryAttempt = 3; //variable created to count the attempt made by client to connect to the server. + ipc.config.maxRetries = 3; + ipc.config.stopRetrying = true; + + + ipc.connectTo( + 'fakeworld', + function(){ + ipc.of.fakeworld.on( + 'disconnect', + function(){ + + if(ipc.of.fakeworld.retriesRemaining == 3){ + + expect(retryAttempt).toBe(ipc.of.fakeworld.retriesRemaining); + expect(ipc.of.fakeworld.socket.destroyed).toBe(true); + + } + else if(ipc.of.fakeworld.retriesRemaining < 3){ + + expect(retryAttempt).not.toBeLessThan(3); + expect(ipc.of.fakeworld.retriesRemaining).not.toBeLessThan(3); + + + ipc.of.fakeworld.on( + 'error', + function(err){ + console.log('Error is: ', err); + ipc.disconnect('fakeworld'); + } + ); + + } + + retryAttempt--; + } + ); + } + ); + + // Wait time is added to verify the fail case scenario of additional retry attempt by client than expected. + setTimeout( + function(){ + ipc.disconnect('fakeworld'); + done(); + },700 + ); + +// +// retryAttempt--; +// if(ipc.of.fakeworld.retriesRemaining < 3){ +// +// expect(retryAttempt).not.toBeLessThan(3); +// +// ipc.of.fakeworld.on( +// 'error', +// function(err){ +// console.log('Error is: ', err); +// ipc.disconnect('fakeworld'); +// } +// ); +// done(); +// } +// } +// ); +// } +// ); +// +// setTimeout( +// function(){ +// expect(retryAttempt).toBe(ipc.of.fakeworld.retriesRemaining); +// expect(ipc.of.fakeworld.retriesRemaining).toBe(ipc.config.maxRetries); +// expect(ipc.of.fakeworld.socket.destroyed).toBe(true); +// done(); +// },700 +// ); +// + + } + ); + + + it( + 'Verify unix client connects to "unixServer" and receives message.', + function(done){ + ipc.connectTo( + 'unixServer', + '/tmp/app.unixServer', + function(){ + ipc.of.unixServer.on( + 'connect', + function(){ + ipc.of.unixServer.emit( + 'message', + { + id : ipc.config.id, + message : 'Hello from Client.' + } + ); + + ipc.of.unixServer.on( + 'message', + function(data){ + + expect(data.id).toBe('unixServer'); + expect(data.message).toBe('I am unix server!'); + ipc.disconnect('unixServer'); + done(); + } + ); + + ipc.of.unixServer.on( + 'error', + function(err){ + console.log('Error is: ', err); done(); + // ipc.disconnect('unixServer'); + } + ); + + } + ); + } + ); + } + ); + + // UDP Test Cases // + it( + 'Verify UDP server "testClient" connects to UDP server named "udpServer" and receives message.', + function(done){ + ipc.serveNet( + 8001, + 'udp4', + function(){ + ipc.server.on( + 'message', + function(data,socket){ + expect(data.id).toBe('udpServer'); + expect(data.message).toBe('I am UDP server!'); + done(); + } + ); + + ipc.server.emit( + { + address : 'localhost', + port : ipc.config.networkPort + }, + 'message', + { + id : ipc.config.id, + message : 'I am client' + } + ); + + ipc.server.on( + 'error', + function(err){ + console.log('Error is: ', err); + + } + ); + } + ); + ipc.server.start(); + } + ); + + xit( + 'Verify UDP server of type udp6 connects to UDP server named "udp6Server" and receives message.', + function(done){ + + ipc.serveNet( + 8001, + 'udp6', + function(){ + ipc.server.on( + 'message', + function(data,socket){ + expect(data.id).toBe('udp6Server'); + expect(data.message).toBe('I am UDP6 server!'); + done(); + } + ); + + ipc.server.emit( + { + address : 'localhost', + port : ipc.config.networkPort + }, + 'message', + { + id : ipc.config.id, + message : 'I am testClient' + } + ); + + ipc.server.on( + 'error', + function(err){ + console.log('Error is: ', err); + + } + ); + } + ); + ipc.server.start(); + } + ); + + + // TCP Client test cases + + it( + 'Verify retry attempts by TCP client to connect to the server as per the value set in "maxRetries" parameter.', + function(done){ + + var tcpRetryAttempt = 3; //variable created to count the attempt made by client to connect to the server. + ipc.config.maxRetries = 3; + ipc.config.stopRetrying = false; + ipc.config.silent= false; + + ipc.connectToNet( + 'tcpFakeServer', + 8002, + function(){ + + ipc.of.tcpFakeServer.on( + 'disconnect', + function(){ + + if(ipc.of.tcpFakeServer.retriesRemaining == 0){ + + expect(tcpRetryAttempt).toBe(ipc.of.tcpFakeServer.retriesRemaining); + expect(ipc.of.tcpFakeServer.socket.destroyed).toBe(true); + + + } + else if(ipc.of.tcpFakeServer.retriesRemaining < 0){ + + expect(tcpRetryAttempt).not.toBeLessThan(0); + expect(ipc.of.tcpFakeServer.retriesRemaining).not.toBeLessThan(0); + + + ipc.of.tcpFakeServer.on( + 'error', + function(err){ + console.log('Error is: ', err); + ipc.disconnect('tcpFakeServer'); + } + ); + + } + + tcpRetryAttempt--; + } + ); + } + ); + + // Wait time is added to verify the fail case scenario of additional retry attempt by client than expected. + setTimeout( + function(){ + ipc.disconnect('tcpFakeServer'); + done(); + },2500 + ); + + } + ); + + it( + 'Verify TCP client does not connect to the TCPserver when "stopRetrying" value is set to true.', + function(done){ + + var tcpRetryAttempt = 3; //variable created to count the attempt made by client to connect to the server. + ipc.config.maxRetries = 3; + ipc.config.stopRetrying = true; + + ipc.connectToNet( + 'tcpFakeServer', + 8002, + function(){ + ipc.of.tcpFakeServer.on( + 'disconnect', + function(){ + + if(ipc.of.tcpFakeServer.retriesRemaining == 3){ + + expect(tcpRetryAttempt).toBe(ipc.of.tcpFakeServer.retriesRemaining); + expect(ipc.of.tcpFakeServer.socket.destroyed).toBe(true); + + } + else if(ipc.of.tcpFakeServer.retriesRemaining < 3){ + + expect(tcpRetryAttempt).not.toBeLessThan(3); + expect(ipc.of.tcpFakeServer.retriesRemaining).not.toBeLessThan(3); + + + ipc.of.tcpFakeServer.on( + 'error', + function(err){ + console.log('Error is: ', err); + ipc.disconnect('tcpFakeServer'); + } + ); + + } + + tcpRetryAttempt--; + } + ); + } + ); + + // Wait time is added to verify the fail case scenario of additional retry attempt by client than expected. + setTimeout( + function(){ + ipc.disconnect('tcpFakeServer'); + done(); + },700 + ); + } + ); + + it( + 'Verify TCP client connects to server named "tcpServer" and receives message.', + function(done){ + ipc.connectToNet( + 'tcpServer', + 8300, + function(){ + ipc.of.tcpServer.on( + 'connect', + function(){ + + ipc.of.tcpServer.emit( + 'message', + { + id : ipc.config.id, + message : 'Hello from testClient.' + } + ); + + ipc.of.tcpServer.on( + 'message', + function(data,socket){ + + expect(data.id).toBe('tcpServer'); + expect(data.message).toBe('I am TCP server!'); + ipc.disconnect('tcpServer'); + done(); + } + ); + + } + ); + } + ); + } + ); + + + + /// End test cases + + + } +); + diff --git a/spec/support/jasmineTest/rawBufferClientAscii.spec.js b/spec/support/jasmineTest/rawBufferClientAscii.spec.js new file mode 100644 index 0000000..623e004 --- /dev/null +++ b/spec/support/jasmineTest/rawBufferClientAscii.spec.js @@ -0,0 +1,61 @@ +var ipc = require('../../../../node-ipc'); + + ipc.config.id ='testClient'; + ipc.config.retry = 600; + + +describe('Raw Buffer tests: ', + function(){ + + it( + 'Verify data in hex is received from the server.', + function(done){ + + ipc.config.rawBuffer=true; + ipc.config.encoding='hex'; + + ipc.connectToNet( + 'rawBufferWorldAscii', + 8000, + function(){ + ipc.of.rawBufferWorldAscii.on( + 'connect', + function(){ + ipc.log('## connected to world ##'.rainbow, ipc.config.delay); + + } + ); + + ipc.of.rawBufferWorldAscii.on( + 'data', + function(data){ + console.log('obtained data is: ',data); + //expect(data).toBe('Buffer 31 30 30 61 30 31'); + done(); + //ipc.log('got a message from world : '.debug, data,data.toString()); + } + ); + + + ipc.of.rawBufferWorldAscii.on( + 'error', + function(err){ + console.log('Error is: ', err); + + } + ); + + } + ); + + } + ); + + + + + + + } +); + diff --git a/spec/support/jasmineTest/rawBufferServerAscii.js b/spec/support/jasmineTest/rawBufferServerAscii.js new file mode 100644 index 0000000..d72a592 --- /dev/null +++ b/spec/support/jasmineTest/rawBufferServerAscii.js @@ -0,0 +1,39 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'rawBufferWorldAscii'; +ipc.config.retry= 1500; + +ipc.config.rawBuffer=true; +ipc.config.encoding='hex'; + + + +ipc.serveNet( + function(){ + ipc.server.on( + 'connect', + function(socket){ + ipc.server.emit( + socket, + [0x10,0x0A,0x01] + //'hello' + ); + } + ); + + + + } +); + + +ipc.server.start(); + + diff --git a/spec/support/jasmineTest/serverTestCases.spec.js b/spec/support/jasmineTest/serverTestCases.spec.js new file mode 100644 index 0000000..323f248 --- /dev/null +++ b/spec/support/jasmineTest/serverTestCases.spec.js @@ -0,0 +1,110 @@ +var ipc = require('../../../../node-ipc'); + + ipc.config.id ='testWorld'; + ipc.config.retry = 1000; + + +describe('Test Cases for server: ', + function(){ + // Unix server verification // + it( + 'Verify unix server detects only 1 client out of 2 clients and receives message.', + function(done){ + console.log('ENTERED TEST 2- unixServer.'); + + var clientCounter =0; + ipc.config.maxConnections=1; + ipc.config.networkPort='/tmp/app.testWorld'; + + ipc.serve( + function(){ + ipc.server.on( + 'message', + function(data,socket){ + + clientCounter++; + expect(data.id).toBe('unixClient'); + expect(data.message).toBe('I am unix client.'); + + } + ); + + setTimeout( + function(){ + expect(clientCounter).toBe(1); + + done(); + },2000 + ); + } + ); + + ipc.server.start(); + + } + ); + + // TCP server verification // + it( + 'Verify TCP server detects only 1 client out of 2 clients and receives message.', + function(done){ + + console.log('ENTERED TEST 2- tcpServer.'); + var clientCounter =0; + ipc.config.id ='testWorld'; + ipc.config.retry = 1000; + + ipc.config.maxConnections=1; + ipc.config.networkPort=8500; + + ipc.serveNet( + function(){ + ipc.server.on( + 'app.message', + function(data,socket){ + + clientCounter++; + + expect(data.id).toBe('tcpClient'); + expect(data.message).toBe('I am TCP client.'); + + } + ); + + setTimeout( + function(){ + expect(clientCounter).toBe(1); + + done(); + },2000 + ); + } + ); + + ipc.server.start(); + + } + ); + + + + + + + + + + + + + + + + + + + + + } +); + diff --git a/spec/support/jasmineTest/tcpClient.js b/spec/support/jasmineTest/tcpClient.js new file mode 100644 index 0000000..a836c12 --- /dev/null +++ b/spec/support/jasmineTest/tcpClient.js @@ -0,0 +1,38 @@ +var ipc=require('../../../node-ipc'); + +ipc.config.id = 'tcpClient'; +ipc.config.retry= 600; + + +ipc.connectToNet( + 'tcpClient', + 8500, + function(){ + ipc.of.tcpClient.on( + 'connect', + function(){ + + ipc.of.tcpClient.emit( + 'app.message', + { + id : ipc.config.id, + message : 'I am TCP client.' + } + ) + } + ); + ipc.of.tcpClient.on( + 'disconnect', + function(){ + ipc.log('disconnected from world'.notice); + } + ); + ipc.of.tcpClient.on( + 'app.message', + function(data){ + ipc.log('got a message from world : '.debug, data.message); + } + ); + + } +); diff --git a/spec/support/jasmineTest/tcpServer.js b/spec/support/jasmineTest/tcpServer.js new file mode 100644 index 0000000..f4d28ab --- /dev/null +++ b/spec/support/jasmineTest/tcpServer.js @@ -0,0 +1,35 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'tcpServer'; +ipc.config.retry= 1500; +ipc.config.networkPort =8300; + + +ipc.serveNet( + function(){ + ipc.server.on( + 'message', + function(data,socket){ + ipc.server.emit( + socket, + 'message', + { + id : ipc.config.id, + message : 'I am TCP server!' + + } + ); + } + ); + } +); + + +ipc.server.start(); diff --git a/spec/support/jasmineTest/tcpSocketClient.spec.js b/spec/support/jasmineTest/tcpSocketClient.spec.js new file mode 100644 index 0000000..66c9de7 --- /dev/null +++ b/spec/support/jasmineTest/tcpSocketClient.spec.js @@ -0,0 +1,133 @@ +var ipc = require('../../../../node-ipc'); + + ipc.config.id ='testClient'; + ipc.config.retry = 600; + + +describe('Unix Socket verification of client', + function(){ + + it( + 'Verify retry attempts by TCP client to connect to the server as per the value set in "maxRetries" parameter.', + function(done){ + + var retryAttempt = 3; //variable created to count the attempt made by client to connect to the server. + ipc.config.maxRetries = 3; + //ipc.config.silent= true; + + ipc.connectToNet( + 'fakeworld', + 8001, + function(){ + ipc.of.fakeworld.on( + 'disconnect', + function(){ + + if(ipc.of.fakeworld.retriesRemaining == 1){ + expect(retryAttempt).toBe(ipc.of.fakeworld.retriesRemaining); + done(); + } + retryAttempt--; + } + ); + + ipc.of.fakeworld.on( + 'error', + function(err){ + console.log('Error is: ', err); + + } + ); + + } + ); + + } + ); + + it( + 'Verify TCP client does not connect to the TCPserver when "stopRetrying" value is set to true.', + function(done){ + + var retryAttempt = 3; //variable created to count the attempt made by client to connect to the server. + ipc.config.maxRetries = 3; + ipc.config.stopRetrying = true; + + ipc.connectToNet( + 'fakeworld', + 8001, + function(){ + ipc.of.fakeworld.on( + 'disconnect', + function(){ + + retryAttempt--; + console.log('var value of retryAttempt: ',retryAttempt); + } + ); + + setTimeout( + function(){ + expect(retryAttempt).toBe(ipc.of.fakeworld.retriesRemaining); + expect(ipc.of.fakeworld.retriesRemaining).toBe(ipc.config.maxRetries); + + done(); + },10 + ); + + ipc.of.fakeworld.on( + 'error', + function(err){ + console.log('Error is: ', err); + + } + ); + + } + ); + } + ); + + + it( + 'Verify TCP client connects to server named "world" and receives message.', + function(done){ + ipc.connectToNet( + 'world', + function(){ + ipc.of.world.on( + 'connect', + function(){ + + ipc.of.world.emit( + 'app.message', + { + id : ipc.config.id, + message : 'Hello from Client.' + } + ); + + ipc.of.world.on( + 'app.message', + function(data,socket){ + console.log('data from world: ', data.id, data.message); + + expect(data.id).toBe('world'); + expect(data.message).toBe('I am world!'); + done(); + } + ); + + } + ); + } + ); + } + ); + + + + + } +); + diff --git a/spec/support/jasmineTest/tcpSocketServer.spec.js b/spec/support/jasmineTest/tcpSocketServer.spec.js new file mode 100644 index 0000000..b0e9e5c --- /dev/null +++ b/spec/support/jasmineTest/tcpSocketServer.spec.js @@ -0,0 +1,102 @@ +var ipc = require('../../../../node-ipc'); + + ipc.config.id ='testWorld'; + ipc.config.retry = 1000; + + +describe('Unix Socket verification of server', + function(){ + + it( + 'Verify server detects only 1 client out of 2 clients and receives message.', + function(done){ + + var clientCounter =0; + ipc.config.maxConnections=1; + + ipc.serveNet( + function(){ + ipc.server.on( + 'app.message', + function(data,socket){ + + clientCounter++; + + expect(data.id).toBe('client1'); + expect(data.message).toBe('I am client1'); + + } + ); + + setTimeout( + function(){ + expect(clientCounter).toBe(1); + + done(); + },2000 + ); + } + ); + + ipc.server.start(); + + } + ); + + /* + xit( + 'Verify server detects clients named "client1" and receives message.', + function(done){ + ipc.serve( + function(){ + ipc.server.on( + 'app.message', + function(data,socket){ + + console.log('Client connected is: ', data.id); + expect(data.id).toBe('client1'); + expect(data.message).toBe('I am client1'); + done(); + } + ); + } + ); + ipc.server.start(); + + } + ); + + xit( + 'Verify server receives disconnection from the connected client.', + function(done){ + + ipc.serve( + function(){ + ipc.server.on( + 'app.message', + function(data,socket){ + console.log('Client connected is: ', data.id); + + ipc.server.on( + 'close', + function(){ + console.log('Client closed: ', data.id); + done(); + } + ); + + } + ); + + + } + ); + + ipc.server.start(); + + } + ); + */ + } +); + diff --git a/spec/support/jasmineTest/udp4Server.js b/spec/support/jasmineTest/udp4Server.js new file mode 100644 index 0000000..ac89028 --- /dev/null +++ b/spec/support/jasmineTest/udp4Server.js @@ -0,0 +1,48 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * Since there is no client relationship + * with UDP sockets sockets are not kept + * open. + * + * This means the order sockets are opened + * is important. + * + * Start World first. Then you can start + * hello or goodbye in any order you + * choose. + * + ***************************************/ + +ipc.config.id = 'udpServer'; +ipc.config.retry= 1500; + + +ipc.serveNet( + 'udp4', + function(){ + + ipc.server.on( + 'message', + function(data,socket){ + + ipc.server.emit( + socket, + 'message', + { + id : ipc.config.id, + message : 'I am UDP server!' + + } + ); + + } + ); + + // console.log(ipc.server); + } +); + + +ipc.server.start(); diff --git a/spec/support/jasmineTest/udp6Server.js b/spec/support/jasmineTest/udp6Server.js new file mode 100644 index 0000000..4d76d30 --- /dev/null +++ b/spec/support/jasmineTest/udp6Server.js @@ -0,0 +1,47 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * Since there is no client relationship + * with UDP sockets sockets are not kept + * open. + * + * This means the order sockets are opened + * is important. + * + * Start World first. Then you can start + * hello or goodbye in any order you + * choose. + * + ***************************************/ + +ipc.config.id = 'udpServer'; +ipc.config.retry= 1500; + + +ipc.serveNet( + 'udp6', + function(){ + + ipc.server.on( + 'message', + function(data,socket){ + + ipc.server.emit( + socket, + 'message', + { + id : ipc.config.id, + message : 'I am UDP server!' + + } + ); + + } + ); + + } +); + + +ipc.server.start(); diff --git a/spec/support/jasmineTest/unixClient.js b/spec/support/jasmineTest/unixClient.js new file mode 100644 index 0000000..b549362 --- /dev/null +++ b/spec/support/jasmineTest/unixClient.js @@ -0,0 +1,38 @@ +var ipc=require('../../../node-ipc'); + +ipc.config.id = 'unixClient'; +ipc.config.retry= 600; + + +ipc.connectTo( + 'testWorld', + '/tmp/app.testWorld', + function(){ + ipc.of.testWorld.on( + 'connect', + function(){ + + ipc.of.testWorld.emit( + 'message', + { + id : ipc.config.id, + message : 'I am unix client.' + } + ) + } + ); + ipc.of.testWorld.on( + 'disconnect', + function(){ + ipc.log('disconnected from testWorld'.notice); + } + ); + ipc.of.testWorld.on( + 'message', + function(data){ + ipc.log('got a message from testWorld : '.debug, data); + } + ); + + } +); diff --git a/spec/support/jasmineTest/unixServer.js b/spec/support/jasmineTest/unixServer.js new file mode 100644 index 0000000..c64e45b --- /dev/null +++ b/spec/support/jasmineTest/unixServer.js @@ -0,0 +1,34 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'unixServer'; +ipc.config.retry= 1500; + +ipc.serve( + function(){ + ipc.server.on( + 'message', + function(data,socket){ + + ipc.server.emit( + socket, + 'message', + { + id : ipc.config.id, + message : 'I am unix server!' + + } + ); + } + ); + } +); + + +ipc.server.start();