Rails 5: Using Capybara with Phusion Passenger - ruby-on-rails

I am trying to run specs on a Rails 5 app. However, I am seeing the following LoadError:
Failure/Error: raise LoadError, 'Capybara is unable to load puma for
its server, please add puma to your project or specify a different
server via something like Capybara.server = :webrick.'
I am using the passenger gem for the production server. Is there a way in which I can use passenger for the Capybara tests as well?
Thanks in advance

If you really want to use passenger to run your app while testing you will need to startup your app separately with passenger using the rails test environment (so it uses the test DB, etc) and then set
Capybara.run_server = false
Capybara.app_host = "http://<wherever the app is accessible>"
for your tests. This tells Capybara not to bother with running the app itself and to just connect to the app you're already running. You will also need to use something like database_cleaner to handle database resetting between tests, and be very careful to make sure you don't have any leftover requests running at the end of each test.
When running the tests with puma or webrick none of that is required (database_cleaner is generally required for rails < 5.1 but not 5.1+), because the web server is run in the same process (different threads) as the tests which allows Capybara to know when requests are still being processed and for Rails to share the DB connection with the tests. Overall you're just going to have a much smoother experience if you stick with puma or webrick for your tests.

I don't think you can do this:
The capybara documentation:
This block takes a rack app and a port and returns a
rack server listening on that port
From the passenger repo:
This is fundamentally incompatible with Phusion Passenger's model.
Such a Rack handler implies a single application and a single process.
Phusion Passenger runs multiple processes which can even serve multiple
applications.

Related

Understanding rails s conflict with Puma and Capybara. How do I properly install puma in the Gemfile?

I am inheriting a code base where the tests have been abandoned for a little over a year. I am trying to clean up the test suite as much as possible but I am having a difficult time teasing out my integration tests with 1. Capybara 2. Puma, 3. Selenium, 4. Starting the Rails Server with rails s.
Here is my initial setup and problem with the Rails 4.2 app. So without doing anything from the get go, when I execute rails testing, I get the following error:
Capybara is unable to load puma for its server, please add puma to your project or specify a different server via something like Capybara.server = :webrick. (LoadError).
I want to point out that in a separate file named start-dev, I have the following in its contents:
rails s -b 0.0.0.0
When I execute this comand with ./start-dev, I am able to view my development app with this url defined in my /etc/hosts 127.0.0.1 secure.ssl.local
Now here is where I start to run into trouble, Through reading some github forums regarding capybara and puma, I start by adding puma into my Gemfile and bundle install but now I am unable to see my development app through the browser at secure.ssl.local.
This is the erorr I get:
/usr/lib/ruby/2.6.0/uri/rfc3986_parser.rb:67:in `split': bad URI(is not URI?): "tcp://0.0.0.0\r:3000" (URI::InvalidURIError)
I noticed that in the start up though, when I execute ./start-dev which if you remeber has rails s -b 0.0.0.0, I see this:
Booting Puma rails 4.2.11.1 application starting in development on https://0.0.0.0:3000
So I am confused by this error. Is Puma blocking my port of 3000 meaning that I have to change the port of Puma? And what makes this even more confusing is that instead of running ./start-dev in the terminal and I simply run rails s -b 0.0.0.0 it magically works except it only works if I navigate to localhost:3000 and not secure.ssl.localhost. This is important because on secure.ssl.localhost I have and need a certificate and localhost I don't.
And finally to add one more layer of confusion, when I run the tests with the puma gem installed and run rake test I get this with Puma:
Capybara starting Puma...
* Version 4.2.1 , codename: Distant Airhorns
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:36608
And the test takes ages to load. There is a lot going on but I guess my question can be summarised by the following? When I install puma into my rails application, do I need to specify it on a specific port so it does not conflict with my app? Without puma, my tests can't run and my ./start-dev file works. With puma, my tests are kind of working but my ./start-dev file isn't anymore. Surely there must be a standard to configure puma. Thank you.
Install puma only for test environment by putting it into test group:
group :test do
gem "puma"
end
Or do as Capybara proposes and put Capybara.server = :webrick into your spec/rails_helper.rb
FYI: Rails uses puma as default web server if puma is installed

Just curious to ask being a newbie in ruby on rails

Does A rails application runs on any webserver or some particular servers are required to run a rails application.
Thanks for help!
Rails application runs only on Rack based servers like Webrick, Puma, Passenger, Unicorn etc.
Checkout https://www.digitalocean.com/community/tutorials/a-comparison-of-rack-web-servers-for-ruby-web-applications

Does Heroku support ActionController::Live?

I have a Rails app hosted on Heroku and I want to add Server Sent Events functionalities, but I can't find any documentation or blog post specific for Heroku.
As not all servers (e.g. WEBrick) support ActionController::Live I was wondering what is the default server on Heroku and whether is possible to configure the environment (i.e. change server) to support SSEs.
Any further advice about the server to use and how to configure would be greatly appreciated.
I think my answer is not so widely helpfull, but you can try.
For the first thing:
create Procfile in rails root within the following content:
web: bundle exec rails server puma -p $PORT -e $RACK_ENV
then add to Gemfile:
gem 'puma'
In above you can switch to thin, but consider link below (and many more details)
http://tenderlovemaking.com/2012/07/30/is-it-live.html
Heroku wouldn't necessarily be the issue here - it's an environment which allows your app to run (on Amazon EC2 I think)
Multi-Threaded Servers
The thing you've got to look for is the server software you use to run your app. Heroku basically takes your server gem & allows it to run with their processors, and other computing power; so it's really whether their platform can play ball with the right server
You're really looking for multi-threaded servers, which you can find here Is puma the ONLY multi-threaded rails 4 http server?
Puma
Rainbows! supports multiple concurrency models, including multithreading
Zbatery - Rack HTTP server without a fork stuck in it
Phusion Passenger 4 has supported multithreading since its beta stages
Thin does have a threaded mode which can be enabled by passing
--threaded or by setting threaded: true in the appropriate configuration file (e.g. bundle exec thin start --threaded)
Net::HTTP::Server, despite the lack of advertising, supports
multithreading; very minimalist

Rails locale development server "rails s" to serve multiple request at the same time

Windows 7, Rails 3 here. I local/development mode, rails server does not handle multiple request at the same time. The process crash and the cmd prompt comes in front.
I've noticed this behaviour when :
having too much ajax request, too close from one another
loading a simple page on 2 browsers
Is there a way workaround that ? Change the local server (default is webrick) ? How is that done ?
Thanks.
I don't know if this still needs an answer but I did this by adding gem 'puma' to the Gemfile then you'll need to add config.threadsafe! to either your config/application.rb or the environment file you're running on (like config/environments/development.rb.
Sometimes you might not want threadsafe on so I so did this in my development.rb:
if ENV["THREADS"]
config.threadsafe!
end
Now (with what I did in my development.rb) I can do rails s Puma and it will run with a max of 16 threads and can handle multiple requests. You can also up the thread pool and configure more with Puma, docs are here
Update
Note that the use of config.threadsafe! is not needed in Rails 4+ and is deprecated I believe.
You need to install the mongrel gem and this specify which server you want to use when you rails s
I don't know how you guys do it on win systems. Why not run a virtual Unix box? isn't rails just much easier with it? So with Unix it would be something like:
Install mongrel gem:
gem install mongrel
Then specify which server you want to run:
rails server mongrel

How do I detect whether or not code is being run from a rails server?

I have a rails app and I run the main rails app on a mongrel server. However, I also have an comet server (using EventMachine) running using the rails environment (so it can access the database). However, Mongrel seems to mess up EventMachine. My question is, how can I detect whether or not I am running the rails environment from the rails server or from the EventMachine server so I can require the mongrel gem only in that case.
Thanks.
request.server_software()
http://api.rubyonrails.org/classes/ActionController/Request.html#M000515
I figured out what to do. I already have some global variables declared for the EventMachine server, so I check to see whether or not they're defined. If they are not, then I know it's not running the EventMachine server.

Resources