11# -* encoding: utf-8 *-
2+ import random
23import sys
34import logging
45
1011
1112from django .db .backends .base .base import BaseDatabaseWrapper
1213from django .db import connection , OperationalError , transaction
13- from django .test import TestCase
14+ from django .test import TestCase , override_settings
1415
1516
1617logging .basicConfig (stream = sys .stderr )
@@ -24,6 +25,8 @@ class FullErrorTests(TestCase):
2425 database connections reliably fail. If I had been able to think of
2526 a better way, I'd have used it.
2627 """
28+ max_dbconn_retry_times = random .randint (1 , 100 )
29+
2730 def test_getting_root (self ) -> None :
2831 self .client .get ('/' )
2932
@@ -38,26 +41,31 @@ def tearDown(self) -> None:
3841 BaseDatabaseWrapper .connect = self .s_connect
3942 del BaseDatabaseWrapper .connection
4043
41- def test_prehook (self ) -> None :
42- cb = Mock (name = 'pre_reconnect_hook' )
43- ddr .pre_reconnect .connect (cb )
44+ def do_assert (self , cb ):
4445 self .assertRaises (OperationalError , connection .ensure_connection )
4546 self .assertTrue (cb .called )
47+ self .assertEqual (connection ._connection_retries , self .max_dbconn_retry_times )
4648 del connection ._connection_retries
4749
50+ @override_settings (MAX_DBCONN_RETRY_TIMES = max_dbconn_retry_times )
51+ def test_prehook (self ) -> None :
52+ cb = Mock (name = 'pre_reconnect_hook' )
53+ ddr .pre_reconnect .connect (cb )
54+ self .do_assert (cb )
55+
56+ @override_settings (MAX_DBCONN_RETRY_TIMES = max_dbconn_retry_times )
4857 def test_posthook (self ) -> None :
4958 cb = Mock (name = 'post_reconnect_hook' )
5059 ddr .post_reconnect .connect (cb )
51- self .assertRaises (OperationalError , connection .ensure_connection )
52- self .assertTrue (cb .called )
53- del connection ._connection_retries
60+ self .do_assert (cb )
5461
5562
5663def fix_connection (sender : type , * , dbwrapper : BaseDatabaseWrapper , ** kwargs : Any ) -> None :
5764 dbwrapper .connect = dbwrapper .s_connect
5865
5966
6067class ReconnectTests (TestCase ):
68+
6169 @classmethod
6270 def tearDownClass (cls ) -> None :
6371 return
@@ -67,10 +75,7 @@ def test_ensure_closed(self) -> None:
6775 connection .close ()
6876 self .assertFalse (connection .is_usable ()) # should be true after setUp
6977
70- def test_prehook (self ) -> None :
71- cb = Mock (name = 'pre_reconnect_hook' )
72- ddr .pre_reconnect .connect (fix_connection )
73- ddr .pre_reconnect .connect (cb )
78+ def do_assert (self , cb ):
7479 from django .db import connection
7580 connection .close ()
7681 connection .s_connect = connection .connect
@@ -80,17 +85,16 @@ def test_prehook(self) -> None:
8085 ReconnectTests .cls_atomics ['default' ].__enter__ ()
8186 self .assertTrue (cb .called )
8287 self .assertTrue (connection .is_usable ())
88+ self .assertEqual (connection ._connection_retries , 0 )
89+
90+ def test_prehook (self ) -> None :
91+ cb = Mock (name = 'pre_reconnect_hook' )
92+ ddr .pre_reconnect .connect (fix_connection )
93+ ddr .pre_reconnect .connect (cb )
94+ self .do_assert (cb )
8395
8496 def test_posthook (self ) -> None :
8597 cb = Mock (name = 'post_reconnect_hook' )
8698 ddr .pre_reconnect .connect (fix_connection )
8799 ddr .post_reconnect .connect (cb )
88- from django .db import connection
89- connection .close ()
90- connection .s_connect = connection .connect
91- connection .connect = Mock (side_effect = OperationalError ('reconnect testing' ))
92- connection .ensure_connection ()
93- ReconnectTests .cls_atomics ['default' ] = transaction .atomic (using = 'default' )
94- ReconnectTests .cls_atomics ['default' ].__enter__ ()
95- self .assertTrue (cb .called )
96- self .assertTrue (connection .is_usable ())
100+ self .do_assert (cb )
0 commit comments