From d9f3237fe3ae0794ee06fe1320e6185dc5b6a4cb Mon Sep 17 00:00:00 2001 From: mloy Date: Thu, 18 Dec 2014 12:57:21 +0100 Subject: [PATCH 1/4] log name or id of peer that caused the problem --- src/jet/daemon.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/jet/daemon.lua b/src/jet/daemon.lua index f41af93..a1ad7aa 100644 --- a/src/jet/daemon.lua +++ b/src/jet/daemon.lua @@ -428,7 +428,7 @@ local create_daemon = function(options) }) end elseif not ok then - log('sync '..message.method..' failed',jencode(result)) + log('sync call failed('..(peer.name or peer.id)..'):',jencode(message),jencode(result)) end end return sc @@ -451,7 +451,7 @@ local create_daemon = function(options) }) end elseif not ok then - log('async '..message.method..' failed:',jencode(err)) + log('async call failed('..(peer.name or peer.id)..'):',jencode(message),jencode(err)) end end return ac From bf6bb11ae7e104641173ddcd6854825579936aba Mon Sep 17 00:00:00 2001 From: mloy Date: Thu, 18 Dec 2014 13:55:23 +0100 Subject: [PATCH 2/4] remove whitespaces --- src/jet/daemon.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jet/daemon.lua b/src/jet/daemon.lua index a1ad7aa..f75ffd5 100644 --- a/src/jet/daemon.lua +++ b/src/jet/daemon.lua @@ -451,7 +451,7 @@ local create_daemon = function(options) }) end elseif not ok then - log('async call failed('..(peer.name or peer.id)..'):',jencode(message),jencode(err)) + log('async call failed('..(peer.name or peer.id)..'):',jencode(message),jencode(err)) end end return ac From d73804f093ce26dbbe2509a20c8d2b7aeca77878 Mon Sep 17 00:00:00 2001 From: mloy Date: Mon, 1 Jun 2015 13:42:56 +0200 Subject: [PATCH 3/4] allow numeric fetch id --- spec/daemon_spec.lua | 117 ++++++++++++++++++++++++++++++++----------- src/jet/daemon.lua | 100 ++++++++++++++++++------------------ 2 files changed, 139 insertions(+), 78 deletions(-) diff --git a/spec/daemon_spec.lua b/spec/daemon_spec.lua index 47e0492..8c52a32 100644 --- a/spec/daemon_spec.lua +++ b/spec/daemon_spec.lua @@ -28,7 +28,7 @@ for _,info in ipairs(addresses_to_test) do return end family_done[info.family] = true - + describe( 'A daemon with address '..info.addr..' and family '..info.family, function() @@ -41,7 +41,7 @@ for _,info in ipairs(addresses_to_test) do interface = info.addr, } end) - + it( 'provides the correct interface', function() @@ -49,7 +49,7 @@ for _,info in ipairs(addresses_to_test) do assert.is_true(type(daemon.start) == 'function') assert.is_true(type(daemon.stop) == 'function') end) - + it( 'can be started', function() @@ -58,7 +58,7 @@ for _,info in ipairs(addresses_to_test) do daemon:start() end) end) - + it( 'can be stopped', function() @@ -67,7 +67,7 @@ for _,info in ipairs(addresses_to_test) do daemon:stop() end) end) - + describe( 'once started', function() @@ -75,14 +75,14 @@ for _,info in ipairs(addresses_to_test) do function() daemon:start() end) - + teardown( function() daemon:stop() end) - + local sock - + before_each(function() if info.family == 'inet6' then sock = socket.tcp6() @@ -90,12 +90,12 @@ for _,info in ipairs(addresses_to_test) do sock = socket.tcp() end end) - + after_each(function() sock:shutdown() sock:close() end) - + it( 'listens on specified port', function(done) @@ -110,12 +110,12 @@ for _,info in ipairs(addresses_to_test) do end),sock:getfd(),ev.WRITE):start(loop) sock:connect(info.addr,port) end) - + it( 'adding and removing states does not leak memory (may take a while...)', function(done) settimeout(100) - + local add_msg = cjson.encode({ method = 'add', params = { @@ -161,7 +161,7 @@ for _,info in ipairs(addresses_to_test) do end end)) end) - + it( 'sending an Invalid Request is reported correctly', function(done) @@ -182,7 +182,7 @@ for _,info in ipairs(addresses_to_test) do end)) message_socket:send('123') end) - + it( 'sending an Invalid JSON is reported correctly', function(done) @@ -203,7 +203,7 @@ for _,info in ipairs(addresses_to_test) do end)) message_socket:send('this is no json') end) - + local req_resp_test = function(desc) local requests = desc.requests local responses = desc.responses @@ -212,7 +212,7 @@ for _,info in ipairs(addresses_to_test) do function(done) sock:connect(info.addr,port) local message_socket = jetsocket.wrap(sock) - + local count = 0 message_socket:on_message( async( @@ -229,7 +229,7 @@ for _,info in ipairs(addresses_to_test) do end end) end - + req_resp_test({ title = 'adding a state twice fails and "pathAlreadyExists" is reported', requests = { @@ -266,7 +266,7 @@ for _,info in ipairs(addresses_to_test) do } } }}) - + req_resp_test({ title = 'adding a state twice fails and "pathAlreadyExists" is reported / variant with less message ids', requests = { @@ -298,7 +298,7 @@ for _,info in ipairs(addresses_to_test) do } } }}) - + req_resp_test({ title = 'add / change / remove', requests = { @@ -341,7 +341,7 @@ for _,info in ipairs(addresses_to_test) do } } }) - + req_resp_test({ title = 'removing a not existing path gives error "pathNotExists"', requests = { @@ -366,7 +366,7 @@ for _,info in ipairs(addresses_to_test) do } } }) - + req_resp_test({ title = 'calling add without a path gives an error', requests = { @@ -394,7 +394,7 @@ for _,info in ipairs(addresses_to_test) do } } }) - + req_resp_test({ title = 'fetch with path matching works', requests = { @@ -442,7 +442,64 @@ for _,info in ipairs(addresses_to_test) do } } }) - + + + + + + + req_resp_test({ + title = 'fetch with path matching and numeric fetch id works', + requests = { + { + method = 'add', + params = { + path = 'a', + value = 123, + }, + }, + { + method = 'add', + params = { + path = 'b', + value = 456, + }, + }, + { + method = 'add', + params = { + path = 'c', + value = 789, + }, + }, + { + method = 'fetch', + params = { + path = { + unequalsAllOf = {'A','^C$'}, + caseInsensitive = true, + contains = 'B' + }, + id = 6 + }, + } + }, + responses = { + { + method = 6, + params = { + event = 'add', + path = 'b', + value = 456 + } + } + } + }) + + + + + req_resp_test({ title = 'fetch with valueField array works', requests = { @@ -502,7 +559,7 @@ for _,info in ipairs(addresses_to_test) do } } }) - + req_resp_test({ title = 'fetch with sort by value works', requests = { @@ -563,7 +620,7 @@ for _,info in ipairs(addresses_to_test) do } } }) - + req_resp_test({ title = 'fetch with sort by valueField works', requests = { @@ -632,7 +689,7 @@ for _,info in ipairs(addresses_to_test) do } } }) - + req_resp_test({ title = 'calling an invalid service reports error', requests = { @@ -651,7 +708,7 @@ for _,info in ipairs(addresses_to_test) do } } }}) - + req_resp_test({ title = 'sending non jsonrpc JSON gives error with id', requests = { @@ -673,7 +730,7 @@ for _,info in ipairs(addresses_to_test) do } } }}) - + req_resp_test({ title = 'sending non jsonrpc JSON gives error without id', requests = { @@ -692,8 +749,8 @@ for _,info in ipairs(addresses_to_test) do } } }}) - + end) end) - + end diff --git a/src/jet/daemon.lua b/src/jet/daemon.lua index f75ffd5..814631d 100644 --- a/src/jet/daemon.lua +++ b/src/jet/daemon.lua @@ -38,7 +38,7 @@ local create_daemon = function(options) local options = options or {} local port = options.port or 11122 local loop = options.loop or ev.Loop.default - + -- logging functions local log = options.log or noop local info = options.info or noop @@ -47,26 +47,26 @@ local create_daemon = function(options) -- all connected peers (clients) -- key and value are peer itself (table) local peers = {} - + -- all elements which have been added -- key is (unique) path, value is element (table) local elements = {} - + -- holds info about all pending request -- key is (daemon generated) unique id, value is table -- with original id and receiver (peer) and request -- timeout timer. local routes = {} - + -- global for tracking the neccassity of lower casing -- paths on publish local has_case_insensitives -- holds all case insensitive fetchers -- key is fetcher (table), value is true local case_insensitives = {} - + local radixtree = jradix.new() - + -- routes an incoming response to the requestor (peer) -- stops the request timeout eventually local route_response = function(peer,message) @@ -80,11 +80,11 @@ local create_daemon = function(options) log('unknown route id:',jencode(message)) end end - + -- make often refered globals local to speed up lookup local pcall = pcall local pairs = pairs - + -- publishes a notification local publish = function(path,event,value,element) local lpath = has_case_insensitives and path:lower() @@ -95,14 +95,14 @@ local create_daemon = function(options) end end end - + -- flush all outstanding / queued messages to the peer socket local flush_peers = function() for peer in pairs(peers) do peer:flush() end end - + -- checks if the "params" table has the key "key" with type "typename". -- if so, returns the value, else throws invalid params error. local checked = function(params,key,typename) @@ -121,7 +121,7 @@ local create_daemon = function(options) error(invalid_params({missingParam=key,got=params})) end end - + -- checks if the "params" table has the key "key" with type "typename". -- if tyoe mismatches throws invalid params error, else returns the -- value or nil if not present. @@ -139,7 +139,7 @@ local create_daemon = function(options) end end end - + -- dispatches the "change" jet call. -- updates the internal cache (elements table) -- and publishes a change event. @@ -158,15 +158,16 @@ local create_daemon = function(options) error(invalid_params({foreignPath=path})) end end - + -- dispatches the "fetch" jet call. -- creates a fetch operation and optionally a sorter. -- all elements are inputed as "fake" add events. The fetchop -- is associated with the element if the fetchop "shows interest" local fetch = function(peer,message) - + local params = message.params - local fetch_id = checked(params,'id','string') + local fetch_id = params['id'] + local queue_notification local notify = function(nparams) queue_notification(nparams) @@ -184,14 +185,14 @@ local create_daemon = function(options) if not params_ok then error(invalid_params({fetchParams = params, reason = fetcher})) end - + peer.fetchers[fetch_id] = fetcher - + if is_case_insensitive then case_insensitives[fetcher] = true has_case_insensitives = true end - + if not flush then if message.id then peer:queue({ @@ -200,7 +201,7 @@ local create_daemon = function(options) }) end end - + local cq = peer.queue queue_notification = function(nparams) cq(peer,{ @@ -218,7 +219,7 @@ local create_daemon = function(options) elements[path].fetchers[fetcher] = true end end - + initializing = false if flush then if message.id then @@ -230,27 +231,28 @@ local create_daemon = function(options) flush() end end - + -- dispatches the "unfetch" jet call. -- removes all ressources associsted wth the fetcher. local unfetch = function(peer,message) local params = message.params - local fetch_id = checked(params,'id','string') + local fetch_id = params['id'] + local fetcher = peer.fetchers[fetch_id] peer.fetchers[fetch_id] = nil case_insensitives[fetcher] = nil has_case_insensitives = not is_empty_table(case_insensitives) - + for _,element in pairs(elements) do element.fetchers[fetcher] = nil end end - + -- counter to make the routed request more unique. -- addresses situation if a peer makes two requests with -- same message.id. local rcount = 0 - + -- routes / forwards a request ("call","set") to the peer of the corresponding element -- specified by "params.path". -- creates an entry in the "route" table and sets up a timer @@ -288,7 +290,7 @@ local create_daemon = function(options) id = id,-- maybe nil method = path, } - + local value = params.value if value ~= nil then req.params = {value = value} @@ -308,7 +310,7 @@ local create_daemon = function(options) log('route failed',jencode(error)) end end - + local add = function(peer,message) local params = message.params local path = checked(params,'path','string') @@ -325,9 +327,9 @@ local create_daemon = function(options) } elements[path] = element radixtree.add(path) - + local lpath = has_case_insensitives and path:lower() - + -- filter out fetchers, which will never ever -- match / have interest in this element (fetchers, which -- don't depend on the value of the element). @@ -344,7 +346,7 @@ local create_daemon = function(options) end end end - + local remove = function(peer,message) local params = message.params local path = checked(params,'path','string') @@ -361,7 +363,7 @@ local create_daemon = function(options) error(invalid_params({foreignPath=path})) end end - + local config = function(peer,message) local params = message.params if params.peer then @@ -402,7 +404,7 @@ local create_daemon = function(options) end peer.debug = params.debug end - + local sync = function(f) local sc = function(peer,message) local ok,result,dont_auto_reply = pcall(f,peer,message) @@ -433,7 +435,7 @@ local create_daemon = function(options) end return sc end - + local async = function(f) local ac = function(peer,message) local ok,err = pcall(f,peer,message) @@ -456,7 +458,7 @@ local create_daemon = function(options) end return ac end - + local services = { config = sync(config), add = sync(add), @@ -470,7 +472,7 @@ local create_daemon = function(options) return message.params end) } - + local dispatch_request = function(peer,message) local error local service = services[message.method] @@ -493,7 +495,7 @@ local create_daemon = function(options) error = error, }) end - + local dispatch_notification = function(peer,message) local service = services[message.method] if service then @@ -503,7 +505,7 @@ local create_daemon = function(options) end end end - + local dispatch_single_message = function(peer,message) if message.id then if message.method then @@ -523,7 +525,7 @@ local create_daemon = function(options) error = invalid_request(message) }) end - + local dispatch_message = function(peer,msg) local ok,err = pcall( function() @@ -553,7 +555,7 @@ local create_daemon = function(options) end flush_peers() end - + local create_peer = function(ops) local peer = {} peer.release = function(_) @@ -611,16 +613,16 @@ local create_daemon = function(options) peer.fetchers = {} peer.encode = cjson.encode peer.decode = cjson.decode - + return peer end - + local accept_tcp = function(jsock) local peer = create_peer({ close = function() jsock:close() end, send = function(msg) jsock:send(msg) end, }) - + jsock:on_message(function(_,message_string) dispatch_message(peer,message_string) end) @@ -634,7 +636,7 @@ local create_daemon = function(options) end) peers[peer] = peer end - + local accept_websocket = function(ws) local peer peer = create_peer({ @@ -651,7 +653,7 @@ local create_daemon = function(options) ws:send(msg,type) end, }) - + ws:on_message(function(_,message_string) dispatch_message(peer,message_string) end) @@ -665,10 +667,10 @@ local create_daemon = function(options) end) peers[peer] = peer end - + local websocket_server local server - + local daemon = { start = function() server = jsocket.listener({ @@ -677,7 +679,7 @@ local create_daemon = function(options) loop = loop, on_connect = accept_tcp }) - + if options.ws_port then local websocket_ok,err = pcall(function() websocket_server = require'websocket'.server.ev.listen({ @@ -702,10 +704,12 @@ local create_daemon = function(options) end end } - + return daemon end return { new = create_daemon, } + + From 0aaaa7e2f8871603b1f1aa91f251b8acb8142ae2 Mon Sep 17 00:00:00 2001 From: mloy Date: Mon, 1 Jun 2015 13:44:13 +0200 Subject: [PATCH 4/4] ludent --- spec/module_spec.lua | 8 +- spec/path_matcher_spec.lua | 94 ++++++++-------- spec/peer_spec.lua | 210 ++++++++++++++++++------------------ spec/radix_spec.lua | 102 +++++++++--------- spec/socket_spec.lua | 32 +++--- spec/utils_spec.lua | 12 +-- spec/value_matcher_spec.lua | 14 +-- src/jet/socket.lua | 40 +++---- 8 files changed, 256 insertions(+), 256 deletions(-) diff --git a/spec/module_spec.lua b/spec/module_spec.lua index 28aa4b5..0d4802b 100644 --- a/spec/module_spec.lua +++ b/spec/module_spec.lua @@ -10,14 +10,14 @@ describe( jet = require'jet' end) end) - + it( 'jet.daemon is exposed', function() assert.is.equal(jet.daemon,require'jet.daemon') assert.is.equal(type(jet.daemon.new),'function') end) - + it( 'jet.peer is exposed', function() @@ -25,11 +25,11 @@ describe( assert.is.equal(type(jet.peer.new),'function') assert.is.same(jet.peer.new,require'jet.peer'.new) end) - + it( 'jet.new equals jet.peer.new', function() assert.is.same(jet.new,require'jet.peer'.new) end) - + end) diff --git a/spec/path_matcher_spec.lua b/spec/path_matcher_spec.lua index 6d44ee6..d9a1c61 100644 --- a/spec/path_matcher_spec.lua +++ b/spec/path_matcher_spec.lua @@ -3,10 +3,10 @@ local pm = require'jet.daemon.path_matcher' describe( 'The jet.daemon.path_matcher module', function() - + describe('An exact path matcher',function() local match - + setup(function() local path_matcher = pm.new({ path = { @@ -15,11 +15,11 @@ describe( }) match = path_matcher end) - + it('matches',function() assert.is_truthy(match('somepath')) end) - + it('mismatches',function() assert.is_falsy(match('somePath')) assert.is_falsy(match('somepathsomepath')) @@ -27,12 +27,12 @@ describe( assert.is_falsy(match('1somepath')) assert.is_falsy(match('somepath3')) end) - + end) - + describe('An case insensitive exact path matcher',function() local match - + setup(function() local path_matcher = pm.new({ path = { @@ -44,24 +44,24 @@ describe( return path_matcher(path,path:lower()) end end) - + it('matches',function() assert.is_truthy(match('somepath')) assert.is_truthy(match('somePath')) assert.is_truthy(match('somePATH')) end) - + it('mismatches',function() assert.is_falsy(match('somepathsomepath')) assert.is_falsy(match('some*path')) end) - + end) - - + + describe('Multiple exact path matcher',function() local match - + setup(function() match = pm.new({ path = { @@ -72,12 +72,12 @@ describe( } }) end) - + it('matches',function() assert.is_truthy(match('somepath')) assert.is_truthy(match('foobar')) end) - + it('mismatches',function() assert.is_falsy(match('somePath')) assert.is_falsy(match('somepathsomepath')) @@ -85,28 +85,28 @@ describe( assert.is_falsy(match('^somepath')) assert.is_falsy(match('^somepath$')) end) - + end) - + describe('A partial path matcher',function() local match - + setup(function() match = pm.new({ path = { contains = 'somewhere' } }) - + end) - + it('matches',function() assert.is_truthy(match('somewhere')) assert.is_truthy(match('somewhereA')) assert.is_truthy(match('abcsomewhere123')) assert.is_truthy(match('abcsomewhere')) end) - + it('mismatches',function() assert.is_falsy(match('somePath')) assert.is_falsy(match('someOOpath')) @@ -114,10 +114,10 @@ describe( assert.is_falsy(match('asomepathb')) end) end) - + describe('A partial path matcher with exact unmatch',function() local match - + setup(function() match = pm.new({ path = { @@ -125,16 +125,16 @@ describe( equalsNot = 'abcsomewhere1234' }, }) - + end) - + it('matches',function() assert.is_truthy(match('somewhere')) assert.is_truthy(match('somewhereA')) assert.is_truthy(match('abcsomewhere123')) assert.is_truthy(match('abcsomewhere')) end) - + it('mismatches',function() assert.is_falsy(match('abcsomewhere1234')) assert.is_falsy(match('somePath')) @@ -143,10 +143,10 @@ describe( assert.is_falsy(match('asomepathb')) end) end) - + describe('A partial path matcher with partial unmatch',function() local match - + setup(function() match = pm.new({ path = { @@ -154,16 +154,16 @@ describe( containsNot = '1234' } }) - + end) - + it('matches',function() assert.is_truthy(match('somewhere')) assert.is_truthy(match('somewhereA')) assert.is_truthy(match('abcsomewhere123')) assert.is_truthy(match('abcsomewhere')) end) - + it('mismatches',function() assert.is_falsy(match('abcsomewhere1234')) assert.is_falsy(match('1234somewhere')) @@ -173,24 +173,24 @@ describe( assert.is_falsy(match('asomepathb')) end) end) - + describe('A left-bound partial path matcher',function() local match - + setup(function() match = pm.new({ path = { startsWith = 'somepath' } }) - + end) - + it('matches',function() assert.is_truthy(match('somepath')) assert.is_truthy(match('somepathFoo')) end) - + it('mismatches',function() assert.is_falsy(match('somePath')) assert.is_falsy(match('someOOpath')) @@ -198,35 +198,35 @@ describe( assert.is_falsy(match('asomepathb')) end) end) - + describe('Another left-bound partial path matcher',function() local match - + setup(function() match = pm.new({ path = { startsWith = 'bla/blub/a' } }) - + end) - + it('matches',function() assert.is_truthy(match('bla/blub/aha')) assert.is_truthy(match('bla/blub/a')) end) - + it('mismatches',function() assert.is_falsy(match('bla/blo')) assert.is_falsy(match('abla/blub/aha')) assert.is_falsy(match('bla/blub/')) end) end) - - + + describe('A right-bound partial path matcher',function() local match - + setup(function() match = pm.new({ path = { @@ -234,12 +234,12 @@ describe( } }) end) - + it('matches',function() assert.is_truthy(match('somepath')) assert.is_truthy(match('Foosomepath')) end) - + it('mismatches',function() assert.is_falsy(match('somePath')) assert.is_falsy(match('some*path')) @@ -247,5 +247,5 @@ describe( assert.is_falsy(match('asomepatho')) end) end) - + end) diff --git a/spec/peer_spec.lua b/spec/peer_spec.lua index 834976a..6a09209 100644 --- a/spec/peer_spec.lua +++ b/spec/peer_spec.lua @@ -10,7 +10,7 @@ local dt = 0.05 setloop('ev') create_peer_tests = function(config) - + describe( 'A peer basic tests '.. (config.url and '(Websocket)' or ''), function() @@ -24,11 +24,11 @@ create_peer_tests = function(config) } daemon:start() end) - + teardown(function() daemon:stop() end) - + it('provides the correct interface',function() local peer = jetpeer.new{port = port} assert.is_true(type(peer) == 'table') @@ -41,8 +41,8 @@ create_peer_tests = function(config) assert.is_true(type(peer.loop) == 'function') peer:close() end) - - + + it('on_connect gets called',function(done) peer = jetpeer.new { @@ -55,7 +55,7 @@ create_peer_tests = function(config) } -- finally(function() peer:close() end) end) - + it('can add a state',function(done) peer:state( { @@ -69,7 +69,7 @@ create_peer_tests = function(config) end) }) end) - + it('can not add same state again',function() assert.has_error(function() peer:state @@ -79,7 +79,7 @@ create_peer_tests = function(config) } end) end) - + it('can add some other state',function(done) peer:state( { @@ -99,12 +99,12 @@ create_peer_tests = function(config) peer:close() end) end) - - + + describe('with some states in place',function() local peer local states = {} - + before_each(function(done) peer = jetpeer.new { @@ -154,11 +154,11 @@ create_peer_tests = function(config) end) } end) - + after_each(function(done) peer:close() end) - + it( 'can fetch and unfetch states', function(done) @@ -190,7 +190,7 @@ create_peer_tests = function(config) end end)) end) - + it('another peer can set value and change notifications are send',function(done) local new_val = 716 local other = jetpeer.new @@ -205,7 +205,7 @@ create_peer_tests = function(config) done() end end)) - + other:set(states.test:path(),new_val,{ success = async(function() assert.is_true(true) @@ -217,8 +217,8 @@ create_peer_tests = function(config) end) } end) - - + + it('peer can set value and adjustments are visible in result if valueAsResult is true',function(done) peer:state({ path = 'adjusting_state', @@ -244,7 +244,7 @@ create_peer_tests = function(config) end) }) end) - + it('peer can set value and value_as_result aliases valueAsResult',function(done) peer:state({ path = 'adjusting_state', @@ -270,7 +270,7 @@ create_peer_tests = function(config) end) }) end) - + it('peer can set value and adjustments are not visible in result if valueAsResult is undefined',function(done) peer:state({ path = 'adjusting_state2', @@ -295,8 +295,8 @@ create_peer_tests = function(config) end) }) end) - - + + it('can fetch states with simple match string',function(done) local fetcher = peer:fetch( states.test:path(), @@ -307,7 +307,7 @@ create_peer_tests = function(config) end)) finally(function() fetcher:unfetch() end) end) - + it('can remove a state',function(done) local fetcher = peer:fetch( states.test:path(), @@ -321,7 +321,7 @@ create_peer_tests = function(config) finally(function() fetcher:unfetch() end) states.test:remove() end) - + it('can (re)add a state',function(done) local expected = { { @@ -354,7 +354,7 @@ create_peer_tests = function(config) end)) finally(function() fetcher:unfetch() end) end) - + it('does not fetch on simple path mismatch',function(done) local timer local fetcher = peer:fetch( @@ -371,7 +371,7 @@ create_peer_tests = function(config) end),dt) timer:start(loop) end) - + it('can fetch states with "equals" and no "prop" value',function(done) local oldval = states.test:value() local newval = 333 @@ -409,7 +409,7 @@ create_peer_tests = function(config) end)) finally(function() fetcher:unfetch() end) end) - + it('can fetch states with "endsWith" and "value" "equalsNot"',function(done) local oldval = states.bens_hobby:value() local newval = states.peters_hobby:value() @@ -455,7 +455,7 @@ create_peer_tests = function(config) end)) finally(function() fetcher:unfetch() end) end) - + it('can fetch states with "startsWidth" and "valueField"',function(done) local oldval = states.peter:value() local newval = { @@ -506,7 +506,7 @@ create_peer_tests = function(config) end)) finally(function() fetcher:unfetch() end) end) - + it('can fetch states with no path matcher "valueField" "equals"',function(done) local fetcher = peer:fetch( {valueField={ @@ -520,12 +520,12 @@ create_peer_tests = function(config) assert.is_same(fvalue,states.peter:value()) done() end)) - + finally(function() fetcher:unfetch() end) end) - + it('can fetch case insensitive',function(done) local expected = { { @@ -559,13 +559,13 @@ create_peer_tests = function(config) expected[count].action() end)) end) - - + + end) - + describe('when working with clean jet',function() local peer - + before_each(function(done) peer = jetpeer.new { @@ -574,11 +574,11 @@ create_peer_tests = function(config) on_connect = async(function() done() end) } end) - + after_each(function() peer:close() end) - + it('set gets timeout error',function(done) local not_responding = peer:state { @@ -587,7 +587,7 @@ create_peer_tests = function(config) set_async = async(function() -- never responds end) } - + peer:set('abc',231,{ timeout = 0.2, success = async(function() @@ -600,7 +600,7 @@ create_peer_tests = function(config) end) }) end) - + it('call method passes correct args and result',function(done) local m = peer:method { @@ -622,7 +622,7 @@ create_peer_tests = function(config) end) }) end) - + it('call method forwards "non-json-rpc" error as "Internal error"',function(done) local m = peer:method { @@ -646,7 +646,7 @@ create_peer_tests = function(config) end) }) end) - + it('call method forwards json-rpc-error unchanged',function(done) local m = peer:method { @@ -670,9 +670,9 @@ create_peer_tests = function(config) end) }) end) - - - + + + it('call gets timeout error',function(done) local not_responding = peer:method { @@ -680,7 +680,7 @@ create_peer_tests = function(config) call_async = async(function() -- never responds end) } - + peer:call('abc2',{},{ timeout = 0.2, success = async(function() @@ -693,7 +693,7 @@ create_peer_tests = function(config) end) }) end) - + it('fetch with sort works when states are already added',function(done) local expected_adds = { [1] = { @@ -712,13 +712,13 @@ create_peer_tests = function(config) index = 3 } } - + -- add some other states which are not expected peer:state{ path = 'xyz', value = {foo = 'bar'} } - + -- add expected states in reverse order to be more evil for i=#expected_adds,1,-1 do peer:state{ @@ -726,7 +726,7 @@ create_peer_tests = function(config) value = expected_adds[i].value } end - + local fetcher fetcher = peer:fetch({ sort = { @@ -739,19 +739,19 @@ create_peer_tests = function(config) assert.is_same(sorted,expected_adds) done() end)) - + finally(function() fetcher:unfetch() end) - - + + end) - + it('fetch with sort has n properly reduced',function(done) local a = peer:state{path = 'a', value = 1} local b = peer:state{path = 'b', value = 2} local c = peer:state{path = 'c', value = 3} local d = peer:state{path = 'd', value = 4} local e = peer:state{path = 'e', value = 5} - + local expected = { { sorted = { @@ -802,10 +802,10 @@ create_peer_tests = function(config) end }, } - - + + local count = 0 - + local fetcher = peer:fetch({ sort = { from = 1, @@ -818,19 +818,19 @@ create_peer_tests = function(config) assert.is_same(#expected[count].sorted,#sorted) expected[count].action() end)) - + finally(function() fetcher:unfetch() end) - - + + end) - + it('fetch with sort has n properly reduced with from = 2',function(done) local a = peer:state{path = 'a', value = 1} local b = peer:state{path = 'b', value = 2} local c = peer:state{path = 'c', value = 3} local d = peer:state{path = 'd', value = 4} local e = peer:state{path = 'e', value = 5} - + local expected = { { sorted = { @@ -897,10 +897,10 @@ create_peer_tests = function(config) end }, } - - + + local count = 0 - + local fetcher = peer:fetch({ sort = { from = 2, @@ -913,19 +913,19 @@ create_peer_tests = function(config) assert.is_same(#expected[count].sorted,#sorted) expected[count].action() end)) - + finally(function() fetcher:unfetch() end) - - + + end) - + it('fetch with from = 2 works when elements from top are removed',function(done) local a = peer:state{path = 'a', value = 1} local b = peer:state{path = 'b', value = 2} local c = peer:state{path = 'c', value = 3} local d = peer:state{path = 'd', value = 4} local e = peer:state{path = 'e', value = 5} - + local expected = { { sorted = { @@ -950,10 +950,10 @@ create_peer_tests = function(config) end } } - - + + local count = 0 - + local fetcher = peer:fetch({ sort = { from = 2, @@ -966,12 +966,12 @@ create_peer_tests = function(config) assert.is_same(#expected[count].sorted,#sorted) expected[count].action() end)) - + finally(function() fetcher:unfetch() end) - - + + end) - + it('fetch with sort works when states are added afterwards',function(done) local expected = { -- when xcd is added @@ -1023,8 +1023,8 @@ create_peer_tests = function(config) } } } - - + + local count = 0 local fetcher = peer:fetch({ sort = { @@ -1042,35 +1042,35 @@ create_peer_tests = function(config) done() end end)) - + finally(function() fetcher:unfetch() end) - + -- add some other states which are not expected peer:state{ path = 'xcd', value = true } - + peer:state{ path = 'ii98', value = {} } - + peer:state{ path = 'abc', value = 123 } - - + + end) - - + + end) - + describe('when working with clean jet with msgpack encoding',function() - + if pcall(require,'cmsgpack') then - + it('fetch with sort works when states are already added',function(done) local peer = jetpeer.new { @@ -1081,7 +1081,7 @@ create_peer_tests = function(config) finally(function() peer:close() end) - + local expected = { -- when xcd is added { @@ -1132,8 +1132,8 @@ create_peer_tests = function(config) } } } - - + + local count = 0 local fetcher = peer:fetch({ sort = { @@ -1151,35 +1151,35 @@ create_peer_tests = function(config) done() end end)) - + finally(function() fetcher:unfetch() end) - + -- add some other states which are not expected peer:state{ path = 'xcd', value = true } - + peer:state{ path = 'ii98', value = {} } - + peer:state{ path = 'abc', value = 123 } - - + + end) else pending('test msgpack') end - + end) - + end) - + end create_peer_tests({port=port}) @@ -1197,7 +1197,7 @@ if socket.dns and socket.dns.getaddrinfo then end if ipv6_localhost_addr then - + describe('ipv6 stuff',function() local daemon local peer @@ -1209,14 +1209,14 @@ if ipv6_localhost_addr then } daemon:start() end) - + teardown(function() daemon:stop() if peer then peer:close() end end) - + it('The jet.peer can connect to the ipv6 localhost addr '..ipv6_localhost_addr..' and on_connect gets called',function(done) peer = jetpeer.new { @@ -1228,6 +1228,6 @@ if ipv6_localhost_addr then end) } end) - + end) end diff --git a/spec/radix_spec.lua b/spec/radix_spec.lua index c6aa087..e68c5dd 100644 --- a/spec/radix_spec.lua +++ b/spec/radix_spec.lua @@ -3,10 +3,10 @@ local radix = require'jet.daemon.radix' describe( 'The jet.daemon.radix module', function() - + describe('Can add path',function() local match - + setup(function() local radix_tree = radix.new() radix_tree.add('abc') @@ -15,17 +15,17 @@ describe( radix_tree.match_parts(radix_fetchers) match = radix_tree.found_elements()['abc'] end) - + it('matches',function() assert.is_true(match) end) - + end) - + describe('Can remove path',function() local match local removed - + setup(function() local radix_tree = radix.new() radix_tree.add('abc') @@ -38,20 +38,20 @@ describe( radix_tree.match_parts(radix_fetchers) removed = radix_tree.found_elements()['abc'] end) - + it('matches',function() assert.is_true(match) end) - + it('mismatches',function() assert.is_falsy(removed) end) - + end) - + describe('Can fetch equals',function() local match - + setup(function() local radix_tree = radix.new() radix_tree.add('abcdef') @@ -66,23 +66,23 @@ describe( return radix_tree.found_elements()[word] end end) - + it('matches',function() assert.is_true(match('defghi')) end) - + it('mismatches',function() assert.is_falsy(match('ddefghi')) assert.is_falsy(match('defghid')) assert.is_falsy(match('ddefghia')) assert.is_falsy(match('abcdef')) end) - + end) - + describe('Can fetch startsWith',function() local match - + setup(function() local radix_tree = radix.new() radix_tree.add('abcdef') @@ -95,22 +95,22 @@ describe( return radix_tree.found_elements()[word] end end) - + it('matches',function() assert.is_true(match('abcdef')) assert.is_true(match('abcghi')) end) - + it('mismatches',function() assert.is_falsy(match('abc')) assert.is_falsy(match('defghi')) end) - + end) - + describe('Can fetch contains',function() local match - + setup(function() local radix_tree = radix.new() radix_tree.add('abcdef') @@ -125,24 +125,24 @@ describe( return radix_tree.found_elements()[word] end end) - + it('matches',function() assert.is_true(match('defghi')) assert.is_true(match('abcfghi')) assert.is_true(match('fgabcdef')) assert.is_true(match('abcdefg')) end) - + it('mismatches',function() assert.is_falsy(match('fg')) assert.is_falsy(match('abcdef')) end) - + end) - + describe('Can fetch endsWith',function() local match - + setup(function() local radix_tree = radix.new() radix_tree.add('abcdeffg') @@ -156,23 +156,23 @@ describe( return radix_tree.found_elements()[word] end end) - + it('matches',function() assert.is_true(match('abcdeffg')) assert.is_true(match('abchifg')) end) - + it('mismatches',function() assert.is_falsy(match('fg')) assert.is_falsy(match('defghi')) assert.is_falsy(match('afbcfghi')) end) - + end) - + describe('Can fetch startsWith + endsWith',function() local match - + setup(function() local radix_tree = radix.new() radix_tree.add('abcdeffg') @@ -188,11 +188,11 @@ describe( return radix_tree.found_elements()[word] end end) - + it('matches',function() assert.is_true(match('ahfbcfghi')) end) - + it('mismatches',function() assert.is_falsy(match('a')) assert.is_falsy(match('hi')) @@ -200,12 +200,12 @@ describe( assert.is_falsy(match('ahi')) assert.is_falsy(match('abcdeffg')) end) - + end) - + describe('Can fetch contains + endsWith',function() local match - + setup(function() local radix_tree = radix.new() radix_tree.add('hiabghcdeffhi') @@ -221,12 +221,12 @@ describe( return radix_tree.found_elements()[word] end end) - + it('matches',function() assert.is_true(match('deghfghi')) assert.is_true(match('hiabghcdeffhi')) end) - + it('mismatches',function() assert.is_falsy(match('ahchifg')) assert.is_falsy(match('hi')) @@ -235,12 +235,12 @@ describe( assert.is_falsy(match('ahchifg')) assert.is_falsy(match('ahfbcfghi')) end) - + end) - + describe('Can fetch startsWith + contains',function() local match - + setup(function() local radix_tree = radix.new() radix_tree.add('abcdeffg') @@ -257,13 +257,13 @@ describe( return radix_tree.found_elements()[word] end end) - + it('matches',function() assert.is_true(match('ahfbcfghi')) assert.is_true(match('ahchifghi')) assert.is_true(match('ahchifg')) end) - + it('mismatches',function() assert.is_falsy(match('a')) assert.is_falsy(match('hi')) @@ -271,12 +271,12 @@ describe( assert.is_falsy(match('ahia')) assert.is_falsy(match('abcdeffg')) end) - + end) - + describe('Can fetch startsWith + contains + endsWith',function() local match - + setup(function() local radix_tree = radix.new() radix_tree.add('defghi') @@ -293,11 +293,11 @@ describe( return radix_tree.found_elements()[word] end end) - + it('matches',function() assert.is_true(match('ahchifg')) end) - + it('mismatches',function() assert.is_falsy(match('ah')) assert.is_falsy(match('hi')) @@ -305,8 +305,8 @@ describe( assert.is_falsy(match('defghi')) assert.is_falsy(match('ahfbcfghi')) end) - + end) - - + + end) diff --git a/spec/socket_spec.lua b/spec/socket_spec.lua index bb10412..de42195 100644 --- a/spec/socket_spec.lua +++ b/spec/socket_spec.lua @@ -31,7 +31,7 @@ describe( ev.READ) echo_server_io:start(loop) end) - + teardown( function() echo_server_io:stop(loop) @@ -40,7 +40,7 @@ describe( end echo_listener:close() end) - + local sock local echo = function(message,done) local f = function(done) @@ -56,7 +56,7 @@ describe( end return f end - + local echo_array = function(messages,done) local f = function(done) local wrapped = jetsocket.wrap(sock) @@ -77,18 +77,18 @@ describe( end return f end - + before_each( function() sock = socket.connect('localhost',port) end) - + after_each( function() sock:shutdown() sock:close() end) - + it('can echo ascii',echo('ablbalblasdkjhsdkuhqdkkbjasdkjheiurq,jwek')) it('can echo really long data',echo(string.rep('foo',1000000))) it('can echo really long data twice',echo_array({string.rep('foo',1000000),string.rep('bar',1000000)})) @@ -100,7 +100,7 @@ describe( local messages = { '123','sjygdjhgsudkshd','askjdhksahdkshkshdkshdkhaiuysd' } - + wrapped:on_message( async( function(wrapped,echoed) @@ -111,7 +111,7 @@ describe( done() end end)) - + for _,message in ipairs(messages) do wrapped:send(message) end @@ -125,7 +125,7 @@ describe( local server_sock local listener local on_accept - + setup( function() listener,err = socket.bind('*',port) @@ -147,14 +147,14 @@ describe( server_io:stop(loop) listener:close() end) - + after_each( function(done) server_sock:shutdown() server_sock:close() on_accept = nil end) - + it('should fire on_close event when closing', function(done) local wrapped @@ -187,7 +187,7 @@ describe( end end) end) - + it('should fire on_close event when closed while receiving', function(done) local wrapped @@ -223,7 +223,7 @@ describe( end,0.001):start(loop) end end) - + it('should fire on_close and on_error event when receiving a message with len > 10MB', function(done) local wrapped @@ -258,7 +258,7 @@ describe( server_sock_wrapped:send(string.rep('f',10000001)) end end) - + it('should fire on_close event when closed while sending', function(done) local wrapped @@ -292,7 +292,7 @@ describe( server_sock:close() end end) - + it('should fire on_close event when closed immediatly', function(done) local sock = socket.tcp() @@ -307,5 +307,5 @@ describe( end)) wrapped:close() end) - + end) diff --git a/spec/utils_spec.lua b/spec/utils_spec.lua index e717103..3403bc6 100644 --- a/spec/utils_spec.lua +++ b/spec/utils_spec.lua @@ -3,13 +3,13 @@ local utils = require'jet.utils' describe( 'The jet.utils module', function() - + it('is_empty_table works',function() assert.is_true(utils.is_empty_table({})) assert.is_false(utils.is_empty_table({'asd'})) assert.is_false(utils.is_empty_table({a=123})) end) - + it('access_field works one level deep',function() local t = {} local b = {} @@ -18,7 +18,7 @@ describe( assert.is_equal(t.a,accessor(t)) assert.is_nil(accessor({})) end) - + it('access_field works two level deep',function() local t = {} local b = {} @@ -31,7 +31,7 @@ describe( local ok = pcall(accessor,t) assert.is_false(ok) end) - + it('equals_deep works',function() assert.is_true(utils.equals_deep({a={1,3},asd=true},{a={1,3},asd=true})) assert.is_false(utils.equals_deep({a={1,3},asd=true},{a={1},asd=true})) @@ -39,7 +39,7 @@ describe( assert.is_false(utils.equals_deep(1,'foo')) assert.is_true(utils.equals_deep(1,1)) end) - + it('mapper works',function() local map = utils.mapper({ ['person.age'] = 'age', @@ -47,6 +47,6 @@ describe( }) assert.is_same(map({person={age=32},hobby='guitar'}),{age=32,work='guitar'}) end) - + end) diff --git a/spec/value_matcher_spec.lua b/spec/value_matcher_spec.lua index 0fb57a0..5ded7ed 100644 --- a/spec/value_matcher_spec.lua +++ b/spec/value_matcher_spec.lua @@ -3,7 +3,7 @@ local vm = require'jet.daemon.value_matcher' describe( 'The jet.daemon.value_matcher module', function() - + it('equals', function() local pred = vm.new({ value = { @@ -12,7 +12,7 @@ describe( }) assert.is_true(pred('123')) assert.is_falsy(pred(123)) - + local pred = vm.new({ value = { equals = 123 @@ -21,7 +21,7 @@ describe( assert.is_falsy(pred('123')) assert.is_true(pred(123)) end) - + it('equals works with valueField', function() local pred = vm.new({ valueField = { @@ -33,7 +33,7 @@ describe( assert.is_true(pred({abc = '123'})) assert.is_falsy(pred({abc = 123})) assert.is_falsy(pred(123)) - + local pred = vm.new({ valueField = { abc = { @@ -45,7 +45,7 @@ describe( assert.is_true(pred({abc = 123})) assert.is_falsy(pred('123')) end) - + it('hasOneOf', function() local pred = vm.new({ value = { @@ -59,7 +59,7 @@ describe( assert.is_falsy(pred({920,122,999,'123'})) assert.is_falsy(pred(123)) end) - + it('hasAllOf', function() local pred = vm.new({ valueField = { @@ -74,5 +74,5 @@ describe( assert.is_falsy(pred({tags = {'helo',9820,123}})) assert.is_falsy(pred(123)) end) - + end) diff --git a/src/jet/socket.lua b/src/jet/socket.lua index c4256eb..ae98538 100644 --- a/src/jet/socket.lua +++ b/src/jet/socket.lua @@ -42,7 +42,7 @@ local wrap = function(sock,args) local send_io local connect_io local connected = sock:getpeername() - + local stop_ios = function() if connect_io then connect_io:stop(loop) @@ -53,7 +53,7 @@ local wrap = function(sock,args) send_io:stop(loop) send_io:clear_pending(loop) end - + local detach = function(f) if ev.Idle then ev.Idle.new(function(loop,io) @@ -67,7 +67,7 @@ local wrap = function(sock,args) end,eps):start(loop) end end - + local handle_error = function(io_active,err_msg) stop_ios() sock:close() @@ -83,7 +83,7 @@ local wrap = function(sock,args) end) end end - + local handle_close = function(io_active) stop_ios() sock:close() @@ -97,7 +97,7 @@ local wrap = function(sock,args) end) end end - + local send_pos local send_message = function(loop,write_io) local sent,err,sent_so_far = sock:send(send_buffer,send_pos) @@ -119,7 +119,7 @@ local wrap = function(sock,args) local fd = sock:getfd() assert(fd > -1) send_io = ev.IO.new(send_message,fd,ev.WRITE) - + local flush = function() if not send_io:is_active() then send_message(loop,send_io) @@ -128,7 +128,7 @@ local wrap = function(sock,args) end end end - + -- sends asynchronous the supplied message object -- -- the message format is 32bit big endian integer @@ -139,9 +139,9 @@ local wrap = function(sock,args) flush() end end - + local closing - + wrapped.close = function() sock:close() if not closing then @@ -157,7 +157,7 @@ local wrap = function(sock,args) end) end end - + wrapped.connect = function() detach(function() local sock_connected,err = sock:connect(args.ip,args.port) @@ -182,32 +182,32 @@ local wrap = function(sock,args) end end) end - + wrapped.on_message = function(_,f) assert(type(f) == 'function') on_message = f end - + wrapped.on_open = function(_,f) assert(type(f) == 'function') on_open = f end - + wrapped.on_close = function(_,f) assert(type(f) == 'function') on_close = f end - + wrapped.on_error = function(_,f) assert(type(f) == 'function') on_error = f end - + local len local len_bin local message local _ - + local receive_message = function(loop,read_io) local io_active = read_io:is_active() while true do @@ -252,13 +252,13 @@ local wrap = function(sock,args) end end end - + read_io = ev.IO.new(receive_message,sock:getfd(),ev.READ) - + if connected then read_io:start(loop) end - + return wrapped end @@ -321,7 +321,7 @@ local listener = function(config) lsock:getfd(), ev.READ) listen_io:start(loop) - + local l = {} l.close = function() listen_io:stop(loop)