Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

tests for remote markup service

  • Loading branch information...
commit 2b02c14facc417fff80077f40ecdae60f0bebecd 1 parent 81ed600
@technoweenie authored
Showing with 69 additions and 5 deletions.
  1. +44 −5 server-ruby/lib/markup_cloud.rb
  2. +25 −0 server-ruby/test/server_test.rb
View
49 server-ruby/lib/markup_cloud.rb
@@ -11,6 +11,19 @@ def render(filename, content)
end
end
+ def local_markup(pattern, file = nil, &block)
+ require file if file
+ markups[compile_pattern(pattern)] = block
+ end
+
+ def remote_markup(pattern, address)
+ markups[compile_pattern(pattern)] = RemoteMarkup.new(address)
+ end
+
+ def renderable?(filename)
+ !! renderer_for(filename)
+ end
+
def renderer_for(filename)
markups.each do |pattern, endpoint|
return endpoint if filename =~ pattern
@@ -18,11 +31,6 @@ def renderer_for(filename)
nil
end
- def local_markup(pattern, file = nil, &block)
- require file if file
- markups[compile_pattern(pattern)] = block
- end
-
def markups
@markups ||= {}
end
@@ -30,4 +38,35 @@ def markups
def compile_pattern(pattern)
Regexp.compile("\\.(#{pattern})$")
end
+
+ class RemoteMarkup
+ def initialize(address)
+ @address = address
+ reset
+ end
+
+ def call(content)
+ @socket.send_string content
+ @socket.recv_string html=''
+ html.empty? ? content : html
+ end
+
+ def reset
+ @socket = self.class.context { |c| c.socket(ZMQ::REQ) }
+ @socket.connect @address
+ end
+
+ class << self
+ attr_writer :context
+
+ def context
+ @context ||= begin
+ require 'ffi-rzmq'
+ ZMQ::Context.new
+ end
+ block_given? ? yield(@context) : @context
+ end
+ end
+ end
end
+
View
25 server-ruby/test/server_test.rb
@@ -1,15 +1,35 @@
require 'test/unit'
+require 'thread'
require File.expand_path("../../lib/markup_cloud", __FILE__)
class MarkupCloudServerTest < Test::Unit::TestCase
def setup
@cloud = MarkupCloud.new
+ @context = MarkupCloud::RemoteMarkup.context
+ @threads = []
end
def test_empty_cloud
assert_equal 'def', @cloud.render('abc', 'def')
end
+ def test_remote_markup
+ addr = 'inproc://remote-markup'
+
+ rep = @context.socket ZMQ::REP
+ rep.bind addr
+ @cloud.remote_markup :zmq, addr
+
+ Thread.new do
+ rep.recv_string msg = ''
+ assert_equal 'abc', msg
+ rep.send_string 'zmq'
+ rep.close
+ end
+
+ assert_equal 'zmq', @cloud.render('foo.zmq', 'abc')
+ end
+
def test_local_markup_without_dependency
@cloud.local_markup :q do |content|
content + '?'
@@ -26,4 +46,9 @@ def test_local_markup_with_dependency
assert_equal 'wat!', @cloud.render('zomg.ex', 'wat')
end
+
+ def thread
+ @threads << Thread.new(&Proc.new)
+ end
end
+
Please sign in to comment.
Something went wrong with that request. Please try again.