| 136 | } |
| 137 | |
| 138 | func (s *AdapterTests) Test_Issue469_BadConnection() { |
| 139 | sess := s.Session() |
| 140 | |
| 141 | // Ask the PostgreSQL server to disconnect sessions that remain inactive for more |
| 142 | // than 1 second. |
| 143 | _, err := sess.SQL().Exec(`SET SESSION idle_in_transaction_session_timeout=1000`) |
| 144 | s.NoError(err) |
| 145 | |
| 146 | // Remain inactive for 2 seconds. |
| 147 | time.Sleep(time.Second * 2) |
| 148 | |
| 149 | // A query should start a new connection, even if the server disconnected us. |
| 150 | _, err = sess.Collection("artist").Find().Count() |
| 151 | s.NoError(err) |
| 152 | |
| 153 | // This is a new session, ask the PostgreSQL server to disconnect sessions that |
| 154 | // remain inactive for more than 1 second. |
| 155 | _, err = sess.SQL().Exec(`SET SESSION idle_in_transaction_session_timeout=1000`) |
| 156 | s.NoError(err) |
| 157 | |
| 158 | // Remain inactive for 2 seconds. |
| 159 | time.Sleep(time.Second * 2) |
| 160 | |
| 161 | // At this point the server should have disconnected us. Let's try to create |
| 162 | // a transaction anyway. |
| 163 | err = sess.Tx(func(sess db.Session) error { |
| 164 | var err error |
| 165 | |
| 166 | _, err = sess.Collection("artist").Find().Count() |
| 167 | if err != nil { |
| 168 | return err |
| 169 | } |
| 170 | return nil |
| 171 | }) |
| 172 | s.NoError(err) |
| 173 | |
| 174 | // This is a new session, ask the PostgreSQL server to disconnect sessions that |
| 175 | // remain inactive for more than 1 second. |
| 176 | _, err = sess.SQL().Exec(`SET SESSION idle_in_transaction_session_timeout=1000`) |
| 177 | s.NoError(err) |
| 178 | |
| 179 | err = sess.Tx(func(sess db.Session) error { |
| 180 | var err error |
| 181 | |
| 182 | // This query should succeed. |
| 183 | _, err = sess.Collection("artist").Find().Count() |
| 184 | if err != nil { |
| 185 | panic(err.Error()) |
| 186 | } |
| 187 | |
| 188 | // Remain inactive for 2 seconds. |
| 189 | time.Sleep(time.Second * 2) |
| 190 | |
| 191 | // This query should fail because the server disconnected us in the middle |
| 192 | // of a transaction. |
| 193 | _, err = sess.Collection("artist").Find().Count() |
| 194 | if err != nil { |
| 195 | return err |