Browse files

More tests and buffer builder fixes

  • Loading branch information...
1 parent 52c40ed commit 912ce7f067ada2598fd5aa89c89ef11f6db17e11 @cretz committed Dec 3, 2011
Showing with 69 additions and 28 deletions.
  1. +1 −0 .gitignore
  2. +6 −4 Cakefile
  3. +10 −3 src/buffer-builder.coffee
  4. +1 −1 src/packet.coffee
  5. +25 −14 src/tds-client.coffee
  6. +26 −6 test/tds-client.test.coffee
View
1 .gitignore
@@ -1,2 +1,3 @@
coffee.bat
node_modules
+temp
View
10 Cakefile
@@ -1,13 +1,14 @@
fs = require 'fs'
path = require 'path'
+util = require 'util'
{ spawn, exec } = require 'child_process'
run = (cmd, args, cb) ->
proc = spawn cmd, args
- proc.stderr.on 'data', (buffer) -> process.stderr.write buffer
- proc.stdout.on 'data', (buffer) -> process.stdout.write buffer
+ proc.stderr.pipe process.stderr, end: false
+ proc.stdout.pipe process.stdout, end: false
proc.on 'exit', (status) ->
- process.exit(1) if status != 0
+ process.kill(1) if status != 0
cb() if typeof cb is 'function'
compile = (includeTests, cb) ->
@@ -35,4 +36,5 @@ task 'compile', 'compile JS', ->
task 'test', 'test node-tds', ->
invoke 'clean'
compile false, ->
- run 'node', ['node_modules/mocha/bin/mocha', '-R', 'spec'], -> 'Tests complete'
+ run 'node', ['node_modules/mocha/bin/mocha', '-t', '100s', '-c', '-R', 'spec'],
+ -> 'Tests complete'
View
13 src/buffer-builder.coffee
@@ -51,14 +51,14 @@ class exports.BufferBuilder
@
insertUInt16BE: (int, position) ->
- @length++
+ @length += 2
@_values.splice position, 0, type: 'uint16BE', value: int
@
toBuffer: ->
buff = new Buffer @length
offset = 0
- for value in values
+ for value in @_values
switch value.type
# please keep in alphabetical order
when 'byte'
@@ -73,9 +73,16 @@ class exports.BufferBuilder
offset += 4
when 'string'
buff.write value.value, offset, value.length, value.encoding
- offset += value.length
+ offset += value.length
+ when 'uint16BE'
+ buff.writeUInt16BE value.value, offset
+ offset += 2
+ when 'uint16LE'
+ buff.writeUInt16LE value.value, offset
+ offset += 2
when 'uint32LE'
buff.writeUInt32LE value.value, offset
offset += 4
else
throw new Error 'Unrecognized type: ' + value.type
+ buff
View
2 src/packet.coffee
@@ -16,7 +16,7 @@ class exports.Packet
toBuffer: (builder, context) -> throw new Error 'Unimplemented'
- insertPacketHeader: (builder) ->
+ insertPacketHeader: (builder, endOfMessage = true) ->
# packet type
builder.insertByte @type, 0
# status
View
39 src/tds-client.coffee
@@ -27,7 +27,7 @@ class exports.TdsClient
state: TdsConstants.statesByName['INITIAL']
constructor: (@_handler) ->
- if @_handler? then throw new Error 'Handler required'
+ if not @_handler? then throw new Error 'Handler required'
connect: (config) ->
if @state isnt TdsConstants.statesByName['INITIAL']
@@ -37,19 +37,20 @@ class exports.TdsClient
try
@_preLoginConfig = config
# create socket
- @_socket = new Socket
+ @_socket = new Socket()
# attach listeners
@_socket.on 'connect', @_socketConnect
@_socket.on 'error', @_socketError
- @_socket.on 'data', @_socketData
- @_socket.on 'end', @_socketEnd
+ @_socket.on 'data', @_socketData
+ @_socket.on 'end', @_socketEnd
+ @_socket.on 'close', @_socketClose
# attempt connect
@_socket.connect config.port ? 1433, config.host ? 'localhost'
catch err
- if @logError then console.error 'Error connecting: ', err
+ if @logError then console.error 'Error connecting: ' + err
@state = TdsConstants.statesByName['INITIAL']
- @_handler.error? err
- end()
+ @_handler?.error? err
+ @end()
login: (config) ->
if @state isnt TdsConstants.statesByName['CONNECTED']
@@ -83,24 +84,24 @@ class exports.TdsClient
if @logError then console.error 'Error executing: ', err
@_handler.error? err
- _socketConnect: ->
+ _socketConnect: =>
if @logDebug then console.log 'Connection established, pre-login commencing'
try
# create new stream
@_stream = new BufferStream
# do prelogin
prelogin = new PreLoginPacket
- for key, value of config
+ for key, value of @_preLoginConfig
if prelogin.hasOwnProperty key
prelogin[key] = value
@_sendPacket prelogin
catch err
if @logError then console.error 'Error on pre-login: ', err
@state = TdsConstants.statesByName['INITIAL']
- @_handler.error? err
+ @_handler?.error? err
@end()
- _socketError: (error) ->
+ _socketError: (error) =>
if @logError then console.error 'Error in socket: ', error
@_handler.error? err
@end()
@@ -118,7 +119,7 @@ class exports.TdsClient
when SqlBatchPacket.type then SqlBatchPacket
else throw new Error 'Unrecognized type: ' + header.type
- _socketData: (data) ->
+ _socketData: (data) =>
if @logDebug then console.log 'Received %d bytes', data.length
header = null
packet = null
@@ -177,13 +178,23 @@ class exports.TdsClient
if @logError then console.error 'Error reading stream: ', err
throw err
- _socketEnd: ->
+ _socketEnd: =>
if @logDebug then console.log 'Socket ended remotely'
@_socket = null
@state = TdsConstants.statesByName['INITIAL']
+ @_handler?.end?()
+
+ _socketClose: =>
+ if @logDebug then console.log 'Socket closed'
+ @_socket = null
+ @state = TdsConstants.statesByName['INITIAL']
_sendPacket: (packet) ->
- @_socket.write packet.toBuffer(new BufferBuilder, @)
+ if @logDebug then console.log 'Sending packet: %s', packet.name
+ builder = packet.toBuffer new BufferBuilder, @
+ buff = builder.toBuffer()
+ if @logDebug then console.log 'Packet size: %d', buff.length
+ @_socket.write buff
end: ->
if @logDebug then console.log 'Ending socket'
View
32 test/tds-client.test.coffee
@@ -1,23 +1,43 @@
fs = require 'fs'
path = require 'path'
+assert = require 'assert'
{ spawn, exec } = require 'child_process'
{TdsClient} = require '../lib/tds-client'
startSqlServer = (done) ->
console.log 'Starting SQL server'
proc = spawn 'net', ['start', 'SQL Server (SQLEXPRESS)']
- proc.stderr.on 'data', (buffer) -> console.log buffer.toString()
- proc.stdout.on 'data', (buffer) -> console.log buffer.toString()
+ proc.stderr.on 'data', (buffer) -> process.stderr.write buffer
+ proc.stdout.on 'data', (buffer) -> process.stdout.write buffer
proc.on 'exit', (status) ->
done()
describe 'TdsClient', ->
- before (done) ->
- startSqlServer done
+ #before (done) ->
+ # startSqlServer done
+
+ describe '#constructor()', ->
+ it 'should throw an error without a handler', ->
+ try
+ new TdsClient()
+ throw new Error 'Error should have been thrown'
describe '#connect()', ->
it 'should connect without errors', (done) ->
- console.log 'TODO: these tests'
- done()
+ succeeded = false
+ client = new TdsClient
+ error: (err) ->
+ done err
+ connect: (packet) ->
+ succeeded = true
+ end: ->
+ if succeeded then done()
+ throw new Error 'Never connected'
+ client.logError = client.logDebug = true
+ client.connect
+ #TODO move this in a conf
+ host: '192.168.56.1'
+
+ it 'should return an error on invalid host/port'

0 comments on commit 912ce7f

Please sign in to comment.