Skip to content

Instantly share code, notes, and snippets.

@samwgoldman samwgoldman/main.rb
Last active Aug 29, 2015

Embed
What would you like to do?
TorqueBox testing issue
require "capybara"
require "capybara/poltergeist"
require "torquebox-web"
require "torquebox-messaging"
page = DATA.read
app = proc do
headers = {
"Content-Type" => "text/html",
"Content-Length" => page.length.to_s
}
[200, headers, [page]]
end
Capybara.server do |app, port|
topic = TorqueBox::Messaging.topic("default")
server = TorqueBox::Web.run(rack_app: app, port: port, auto_start: false)
server.sockjs(path: "/messages").on_connection do |conn|
STDOUT.puts("#{conn.id} connected")
listener = topic.listen do |message|
STDOUT.puts("#{conn.id} listening to topic")
conn.write(message)
end
conn.on_data do |message|
STDOUT.puts("#{conn.id} pushed message: #{message}")
topic.publish(message)
end
conn.on_close do
STDOUT.puts("#{conn.id} closed")
listener.close
end
end
server.start
sleep # Capybara expects the server thread to never return
end
2.times do
sessions = Array.new(2) do
Capybara::Session.new(:poltergeist, app).tap do |session|
session.visit("/")
end
end
sessions.each_with_index do |session, i|
unless session.has_selector?("p", text: "someone joined", count: 2)
session.save_screenshot("screen#{i}.png", full: true)
end
end
Capybara.reset_sessions!
end
# or else this never ends
org.projectodd.wunderboss.WunderBoss.shutdown_and_reset
__END__
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="//cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js"></script>
</head>
<body>
<h1>Test</h1>
<ol id="messages"></ol>
<script type="text/javascript">
$(function() {
console.log("page loaded");
var messages = document.getElementById("messages");
var client = new SockJS("/messages");
client.onopen = function() {
console.log("sending message: join");
setTimeout(function() {
client.send("join");
}, 100);
};
client.onmessage = function(message) {
console.log("receiving message: " + message.data);
$("<li>someone joined</li>").appendTo(messages);
};
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.