14

I run my rspec and most of the test were failed. I got the same error for them, which is:

    Failure/Error: Unable to find matching line from backtrace
    ActiveRecord::StatementInvalid:
    PG::ConnectionBad: PQsocket() can't get socket descriptor: BEGIN

I found a question that is similar to my problem, but there is no answer yet and I also tried the solution from this link, but it didn't make any differences for me. I opened my test console and run some simplest queries and it worked.

Community
  • 1
  • 1
ishwr
  • 725
  • 3
  • 14
  • 36
  • 1
    This issue has been a consistent thorn in my side for some time now. My specs will typically run for several tests before something triggers the remainder of the database accessing tests to fail. It is not consistent in terms of which test(s) will cause the specs to fail. – Ivar Feb 20 '15 at 00:59
  • 1
    It appears that there is a connection leak in the test suite that eventually exhausts the connection pool. The question is where this might be occurring (and how to solve it). – Ivar Feb 20 '15 at 16:54
  • 1
    I've created an [issue with rspec](https://github.com/rspec/rspec-rails/issues/1317) - perhaps we can shed more light on this. – Ivar Feb 20 '15 at 17:16
  • 1
    I had this same problem; for me it happened during a long-running `delayed_job` that imports some records from a CSV file. I switched to MySQL, and that solved the problem. – Scott Weldon Mar 30 '15 at 23:06

5 Answers5

2

There may be a problem with pg: '0.18.1'. I tried reverting to 0.18.0, and I believe the problem has gone away.

I'll open up an issue on the repo after testing a bit more locally. I added this issue on the pg repo.

EDIT: I was wrong. What did in fact help though, was installing the spring-commands-rspec gem. I think what it ended up doing for me was caching it in a state where it worked. Although it could also just as easily cache it in a state where it didn't.

BJ McDuck
  • 164
  • 1
  • 12
  • Ugh. After further testing, it appears I was wrong about this. – BJ McDuck Mar 27 '15 at 22:38
  • What did in fact help though, was installing the [spring-commands-rspec](https://github.com/jonleighton/spring-commands-rspec) gem. I think what it ended up doing for me was caching it in a state where it worked. Although it could also just as easily cache it in a state where it didn't. :( – BJ McDuck Mar 27 '15 at 22:40
  • Please update the answer to note that it is incorrect! – Julie Apr 19 '18 at 14:31
0

I don't know if the issues are linked- but I found that by using the local unix socket file instead of localhost that the issue seemed to go away for the linked question; perhaps it might help you too?

"Rake spec" failing most tests, but "rails s" working fine on Diaspora source

Community
  • 1
  • 1
Dan Garland
  • 3,350
  • 1
  • 24
  • 24
0

I was getting the same error, and some other errors like PG::UnableToSend: socket not open. Seemed to error at random times. I traced it down and it was actually due to a model having an improper belongs_to association. This might not apply to you but hopefully it helps someone.

I had:

belongs_to :user, class_name: User

When it should have been:

belongs_to :user, class_name: 'User'

Somehow that one line was breaking all of my PG sockets connections.

Robert
  • 681
  • 1
  • 6
  • 19
0

May not have any bearing on rspec, but throwing this out there just in case. I fought this for weeks and for me it was the way passenger spawns new processes. I just had to add this

# Rails -- prepared statements incompatible with faster spawn methods.  
PassengerSpawnMethod conservative

to the \etc\apache2\mods-available\passenger.conf

https://www.phusionpassenger.com/library/indepth/ruby/spawn_methods/

wruckie
  • 1,717
  • 1
  • 23
  • 34
0

I had installed 'pg', '0.20.0', by downgrading to 0.19.0 it worked for me.

blitzblade
  • 126
  • 1
  • 9