File: test_adbapi2.py

package info (click to toggle)
twextpy 1%3A0.1~git20161216.0.b90293c-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,724 kB
  • sloc: python: 20,458; sh: 742; makefile: 5
file content (1365 lines) | stat: -rw-r--r-- 51,895 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
##
# Copyright (c) 2010-2016 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##

"""
Tests for L{twext.enterprise.adbapi2}.
"""

import gc

from zope.interface.verify import verifyObject

from twisted.python.failure import Failure

from twisted.trial.unittest import TestCase

from twisted.internet.defer import Deferred, fail, succeed, inlineCallbacks

from twisted.test.proto_helpers import StringTransport

from twext.enterprise.ienterprise import ConnectionError
from twext.enterprise.ienterprise import AlreadyFinishedError
from twext.enterprise.adbapi2 import ConnectionPoolClient
from twext.enterprise.adbapi2 import ConnectionPoolConnection
from twext.enterprise.ienterprise import IAsyncTransaction
from twext.enterprise.ienterprise import ICommandBlock
from twext.enterprise.adbapi2 import FailsafeException
from twext.enterprise.adbapi2 import ConnectionPool
from twext.enterprise.fixtures import ConnectionPoolHelper
from twext.enterprise.fixtures import resultOf
from twext.enterprise.fixtures import ClockWithThreads
from twext.enterprise.fixtures import FakeConnectionError
from twext.enterprise.fixtures import RollbackFail
from twext.enterprise.fixtures import CommitFail
from twext.enterprise.adbapi2 import Commit
from twext.enterprise.adbapi2 import _HookableOperation


class TrashCollector(object):
    """
    Test helper for monitoring gc.garbage.
    """

    def __init__(self, testCase):
        self.testCase = testCase
        testCase.addCleanup(self.checkTrash)
        self.start()

    def start(self):
        gc.collect()
        self.garbageStart = len(gc.garbage)

    def checkTrash(self):
        """
        Ensure that the test has added no additional garbage.
        """
        gc.collect()
        newGarbage = gc.garbage[self.garbageStart:]
        if newGarbage:
            # Don't clean up twice.
            self.start()
            self.testCase.fail("New garbage: " + repr(newGarbage))


class AssertResultHelper(object):
    """
    Mixin for asserting about synchronous Deferred results.
    """

    def assertResultList(self, resultList, expected):
        """
        Assert that a list created with L{resultOf} contais the expected
        result.

        @param resultList: The return value of L{resultOf}.
        @type resultList: L{list}

        @param expected: The expected value that should be present in the list;
            a L{Failure} if an exception is expected to be raised.
        """
        if not resultList:
            self.fail("No result; Deferred didn't fire yet.")
        else:
            if isinstance(resultList[0], Failure):
                if isinstance(expected, Failure):
                    resultList[0].trap(expected.type)
                else:
                    resultList[0].raiseException()
            else:
                self.assertEqual(resultList, [expected])


class ConnectionPoolBootTests(TestCase):
    """
    Tests for the start-up phase of L{ConnectionPool}.
    """

    def test_threadCount(self):
        """
        The reactor associated with a L{ConnectionPool} will have its maximum
        thread count adjusted when L{ConnectionPool.startService} is called, to
        accomodate for L{ConnectionPool.maxConnections} additional threads.

        Stopping the service should restore it to its original value, so that a
        repeatedly re-started L{ConnectionPool} will not cause the thread
        ceiling to grow without bound.
        """
        defaultMax = 27
        connsMax = 45
        combinedMax = defaultMax + connsMax
        pool = ConnectionPool(None, maxConnections=connsMax)
        pool.reactor = ClockWithThreads()
        threadpool = pool.reactor.getThreadPool()
        pool.reactor.suggestThreadPoolSize(defaultMax)
        self.assertEquals(threadpool.max, defaultMax)
        pool.startService()
        self.assertEquals(threadpool.max, combinedMax)
        justChecking = []
        pool.stopService().addCallback(justChecking.append)
        # No SQL run, so no threads started, so this deferred should fire
        # immediately.  If not, we're in big trouble, so sanity check.
        self.assertEquals(justChecking, [None])
        self.assertEquals(threadpool.max, defaultMax)

    def test_isRunning(self):
        """
        L{ConnectionPool.startService} should set its C{running} attribute to
        true.
        """
        pool = ConnectionPool(None)
        pool.reactor = ClockWithThreads()
        self.assertEquals(pool.running, False)
        pool.startService()
        self.assertEquals(pool.running, True)


class ConnectionPoolNameTests(TestCase):
    """
    Tests for L{ConnectionPool}'s C{name} attribute.
    """

    def test_default(self):
        """
        If no value is given for the C{name} parameter to L{ConnectionPool}'s
        initializer then L{ConnectionPool.name} is C{None}.
        """
        pool = ConnectionPool(None)
        self.assertIs(None, pool.name)

    def test_specified(self):
        """
        If a value is given for the C{name} parameter to L{ConnectionPool}'s
        initializer then it is used as the value for L{ConnectionPool.name}.
        """
        name = "some test pool"
        pool = ConnectionPool(None, name=name)
        self.assertEqual(name, pool.name)


class ConnectionPoolTests(ConnectionPoolHelper, TestCase, AssertResultHelper):
    """
    Tests for L{ConnectionPool}.
    """

    def test_tooManyConnections(self):
        """
        When the number of outstanding busy transactions exceeds the number of
        slots specified by L{ConnectionPool.maxConnections},
        L{ConnectionPool.connection} will return a pooled transaction that is
        not backed by any real database connection; this object will queue its
        SQL statements until an existing connection becomes available.
        """
        a = self.createTransaction()

        alphaResult = self.resultOf(a.execSQL("alpha"))
        [[_ignore_counter, _ignore_echo]] = alphaResult[0]

        b = self.createTransaction()
        # "b" should have opened a connection.
        self.assertEquals(len(self.factory.connections), 2)
        betaResult = self.resultOf(b.execSQL("beta"))
        [[bcounter, _ignore_becho]] = betaResult[0]

        # both "a" and "b" are holding open a connection now; let's try to open
        # a third one.  (The ordering will be deterministic even if this fails,
        # because those threads are already busy.)
        c = self.createTransaction()
        gammaResult = self.resultOf(c.execSQL("gamma"))

        # Did "c" open a connection?  Let's hope not...
        self.assertEquals(len(self.factory.connections), 2)
        # SQL shouldn't be executed too soon...
        self.assertEquals(gammaResult, [])

        commitResult = self.resultOf(b.commit())

        # Now that "b" has committed, "c" should be able to complete.
        [[ccounter, _ignore_cecho]] = gammaResult[0]

        # The connection for "a" ought to still be busy, so let's make sure
        # we're using the one for "c".
        self.assertEquals(ccounter, bcounter)

        # Sanity check: the commit should have succeeded!
        self.assertEquals(commitResult, [None])

    def test_stopService(self):
        """
        L{ConnectionPool.stopService} stops all the associated L{ThreadHolder}s
        and thereby frees up the resources it is holding.
        """
        a = self.createTransaction()
        alphaResult = self.resultOf(a.execSQL("alpha"))
        [[[_ignore_counter, _ignore_echo]]] = alphaResult
        self.assertEquals(len(self.factory.connections), 1)
        self.assertEquals(len(self.holders), 1)
        [holder] = self.holders
        self.assertEquals(holder.started, True)
        self.assertEquals(holder.stopped, False)
        self.pool.stopService()
        self.assertEquals(self.pool.running, False)
        self.assertEquals(len(self.holders), 1)
        self.assertEquals(holder.started, True)
        self.assertEquals(holder.stopped, True)
        # Closing fake connections removes them from the list.
        self.assertEquals(len(self.factory.connections), 1)
        self.assertEquals(self.factory.connections[0].closed, True)

    def test_retryAfterConnectError(self):
        """
        When the C{connectionFactory} passed to L{ConnectionPool} raises an
        exception, the L{ConnectionPool} will log the exception and delay
        execution of a new connection's SQL methods until an attempt succeeds.
        """
        self.factory.willFail()
        self.factory.willFail()
        self.factory.willConnect()
        c = self.createTransaction()

        def checkOneFailure():
            errors = self.flushLoggedErrors(FakeConnectionError)
            self.assertEquals(len(errors), 1)

        checkOneFailure()
        d = c.execSQL("alpha")
        happened = []
        d.addBoth(happened.append)
        self.assertEquals(happened, [])
        self.clock.advance(self.pool.RETRY_TIMEOUT + 0.01)
        checkOneFailure()
        self.assertEquals(happened, [])
        self.clock.advance(self.pool.RETRY_TIMEOUT + 0.01)
        self.flushHolders()
        self.assertEquals(happened, [[[1, "alpha"]]])

    def test_shutdownDuringRetry(self):
        """
        If a L{ConnectionPool} is attempting to shut down while it's in the
        process of re-trying a connection attempt that received an error, the
        connection attempt should be cancelled and the shutdown should complete
        as normal.
        """
        self.factory.defaultFail()
        self.createTransaction()
        errors = self.flushLoggedErrors(FakeConnectionError)
        self.assertEquals(len(errors), 1)
        stopd = []
        self.pool.stopService().addBoth(stopd.append)
        self.assertResultList(stopd, None)
        self.assertEquals(self.clock.calls, [])
        [holder] = self.holders
        self.assertEquals(holder.started, True)
        self.assertEquals(holder.stopped, True)

    def test_shutdownDuringAttemptSuccess(self):
        """
        If L{ConnectionPool.stopService} is called while a connection attempt
        is outstanding, the resulting L{Deferred} won't be fired until the
        connection attempt has finished; in this case, succeeded.
        """
        self.pauseHolders()
        self.createTransaction()
        stopd = []
        self.pool.stopService().addBoth(stopd.append)
        self.assertEquals(stopd, [])
        self.flushHolders()
        self.assertResultList(stopd, None)
        [holder] = self.holders
        self.assertEquals(holder.started, True)
        self.assertEquals(holder.stopped, True)

    def test_shutdownDuringAttemptFailed(self):
        """
        If L{ConnectionPool.stopService} is called while a connection attempt
        is outstanding, the resulting L{Deferred} won't be fired until the
        connection attempt has finished; in this case, failed.
        """
        self.factory.defaultFail()
        self.pauseHolders()
        self.createTransaction()
        stopd = []
        self.pool.stopService().addBoth(stopd.append)
        self.assertEquals(stopd, [])
        self.flushHolders()
        errors = self.flushLoggedErrors(FakeConnectionError)
        self.assertEquals(len(errors), 1)
        self.assertResultList(stopd, None)
        [holder] = self.holders
        self.assertEquals(holder.started, True)
        self.assertEquals(holder.stopped, True)

    def test_stopServicePseudoTxn(self):
        """
        When L{ConnectionPool.stopService} is called with a pending
        L{_ConnectingPseudoTxn} active, the DB connection being created
        is closed.
        """
        self.pauseHolders()
        self.createTransaction()
        stopResult = self.resultOf(self.pool.stopService())
        self.assertEquals(stopResult, [])
        self.flushHolders()
        [holder] = self.holders
        self.assertEquals(holder.started, True)
        self.assertEquals(holder.stopped, True)
        self.assertEquals(len(self.factory.connections), 1)
        self.assertEquals(self.factory.connections[0].closed, True)

    def test_stopServiceMidAbort(self):
        """
        When L{ConnectionPool.stopService} is called with deferreds from
        C{abort} still outstanding, it will wait for the currently-aborting
        transaction to fully abort before firing the L{Deferred} returned from
        C{stopService}.
        """
        # TODO: commit() too?
        self.pauseHolders()
        c = self.createTransaction()
        abortResult = self.resultOf(c.abort())
        # Should abort instantly, as it hasn't managed to unspool anything yet.
        # FIXME: kill all Deferreds associated with this thing, make sure that
        # any outstanding query callback chains get nuked.
        self.assertEquals(abortResult, [None])
        stopResult = self.resultOf(self.pool.stopService())
        self.assertEquals(stopResult, [])
        self.flushHolders()
        # self.assertEquals(abortResult, [None])
        self.assertResultList(stopResult, None)

    def test_stopServiceWithSpooled(self):
        """
        When L{ConnectionPool.stopService} is called when spooled transactions
        are outstanding, any pending L{Deferreds} returned by those
        transactions will be failed with L{ConnectionError}.
        """
        # Use up the free slots so we have to spool.
        hold = []
        hold.append(self.createTransaction())
        hold.append(self.createTransaction())

        c = self.createTransaction()
        se = self.resultOf(c.execSQL("alpha"))
        ce = self.resultOf(c.commit())
        self.assertEquals(se, [])
        self.assertEquals(ce, [])
        self.resultOf(self.pool.stopService())
        self.assertEquals(se[0].type, self.translateError(ConnectionError))
        self.assertEquals(ce[0].type, self.translateError(ConnectionError))

    def test_repoolSpooled(self):
        """
        Regression test for a somewhat tricky-to-explain bug: when a spooled
        transaction which has already had commit() called on it before it's
        received a real connection to start executing on, it will not leave
        behind any detritus that prevents stopService from working.
        """
        self.pauseHolders()
        c = self.createTransaction()
        c2 = self.createTransaction()
        c3 = self.createTransaction()
        c.commit()
        c2.commit()
        c3.commit()
        self.flushHolders()
        self.assertEquals(len(self.factory.connections), 2)
        stopResult = self.resultOf(self.pool.stopService())
        self.assertEquals(stopResult, [None])
        self.assertEquals(len(self.factory.connections), 2)
        self.assertEquals(self.factory.connections[0].closed, True)
        self.assertEquals(self.factory.connections[1].closed, True)

    def test_connectAfterStop(self):
        """
        Calls to connection() after stopService() result in transactions which
        immediately fail all operations.
        """
        stopResults = self.resultOf(self.pool.stopService())
        self.assertEquals(stopResults, [None])
        self.pauseHolders()
        postClose = self.createTransaction()
        queryResult = self.resultOf(postClose.execSQL("hello"))
        self.assertEquals(len(queryResult), 1)
        self.assertEquals(queryResult[0].type,
                          self.translateError(ConnectionError))

    def test_connectAfterStartedStopping(self):
        """
        Calls to connection() after stopService() has been called but before it
        has completed will result in transactions which immediately fail all
        operations.
        """
        self.pauseHolders()
        preClose = self.createTransaction()
        preCloseResult = self.resultOf(preClose.execSQL("statement"))
        stopResult = self.resultOf(self.pool.stopService())
        postClose = self.createTransaction()
        queryResult = self.resultOf(postClose.execSQL("hello"))
        self.assertEquals(stopResult, [])
        self.assertEquals(len(queryResult), 1)
        self.assertEquals(
            queryResult[0].type,
            self.translateError(ConnectionError)
        )
        self.assertEquals(len(preCloseResult), 1)
        self.assertEquals(
            preCloseResult[0].type,
            self.translateError(ConnectionError)
        )

    def test_abortFailsDuringStopService(self):
        """
        L{IAsyncTransaction.abort} might fail, most likely because the
        underlying database connection has already been disconnected.  If this
        happens, shutdown should continue.
        """
        txns = []
        txns.append(self.createTransaction())
        txns.append(self.createTransaction())
        for txn in txns:
            # Make sure rollback will actually be executed.
            results = self.resultOf(txn.execSQL("maybe change something!"))
            [[[_ignore_counter, echo]]] = results
            self.assertEquals("maybe change something!", echo)
        # Fail one (and only one) call to rollback().
        self.factory.rollbackFail = True
        stopResult = self.resultOf(self.pool.stopService())
        self.assertEquals(stopResult, [None])
        self.assertEquals(len(self.flushLoggedErrors(RollbackFail)), 1)
        self.assertEquals(self.factory.connections[0].closed, True)
        self.assertEquals(self.factory.connections[1].closed, True)

    def test_partialTxnFailsDuringStopService(self):
        """
        Using the logic in L{ConnectionPool.stopService}, make sure that an
        L{_ConnectedTxn} cannot continue to process SQL after L{_ConnectedTxn.abort}
        is called and before L{_ConnectedTxn.reset} is called.
        """
        txn = self.createTransaction()
        if hasattr(txn, "_baseTxn"):
            # Send initial statement
            txn.execSQL("maybe change something!")

            # Make it look like the service is stopping
            txn._baseTxn._connection.close()
            txn._baseTxn.terminate()

            # Try to send more SQL - must fail
            self.failUnlessRaises(RuntimeError, txn.execSQL, "maybe change something else!")

    def test_abortRecycledTransaction(self):
        """
        L{ConnectionPool.stopService} will shut down if a recycled transaction
        is still pending.
        """
        recycled = self.createTransaction()
        self.resultOf(recycled.commit())
        remember = []
        remember.append(self.createTransaction())
        self.assertEquals(self.resultOf(self.pool.stopService()), [None])

    def test_abortSpooled(self):
        """
        Aborting a still-spooled transaction (one which has no statements being
        executed) will result in all of its Deferreds immediately failing and
        none of the queued statements being executed.
        """
        active = []
        # Use up the available connections ...
        for _ignore in xrange(self.pool.maxConnections):
            active.append(self.createTransaction())

        # ... so that this one has to be spooled.
        spooled = self.createTransaction()
        result = self.resultOf(spooled.execSQL("alpha"))

        # sanity check, it would be bad if this actually executed.
        self.assertEqual(result, [])
        self.resultOf(spooled.abort())
        self.assertEqual(result[0].type, self.translateError(ConnectionError))

    def test_waitForAlreadyAbortedTransaction(self):
        """
        L{ConnectionPool.stopService} will wait for all transactions to shut
        down before exiting, including those which have already been stopped.
        """
        it = self.createTransaction()
        self.pauseHolders()
        abortResult = self.resultOf(it.abort())

        # steal it from the queue so we can do it out of order
        d, _ignore_work = self.holders[0]._q.get()

        # that should be the only work unit so don't continue if something else
        # got in there
        self.assertEquals(list(self.holders[0]._q.queue), [])
        self.assertEquals(len(self.holders), 1)
        self.flushHolders()
        stopResult = self.resultOf(self.pool.stopService())

        # Sanity check that we haven't actually stopped it yet
        self.assertEquals(abortResult, [])

        # We haven't fired it yet, so the service had better not have
        # stopped...
        self.assertEquals(stopResult, [])

        d.callback(None)
        self.flushHolders()
        self.assertEquals(abortResult, [None])
        self.assertEquals(stopResult, [None])

    def test_garbageCollectedTransactionAborts(self):
        """
        When an L{IAsyncTransaction} is garbage collected, it ought to abort
        itself.
        """
        t = self.createTransaction()
        self.resultOf(t.execSQL("echo", []))
        conns = self.factory.connections
        self.assertEquals(len(conns), 1)
        self.assertEquals(conns[0]._rollbackCount, 0)
        del t
        gc.collect()
        self.flushHolders()
        self.assertEquals(len(conns), 1)
        self.assertEquals(conns[0]._rollbackCount, 1)
        self.assertEquals(conns[0]._commitCount, 0)

    def circularReferenceTest(self, finish, hook):
        """
        Collecting a completed (committed or aborted) L{IAsyncTransaction}
        should not leak any circular references.
        """
        tc = TrashCollector(self)
        commitExecuted = []

        def carefullyManagedScope():
            t = self.createTransaction()

            def holdAReference():
                """
                This is a hook that holds a reference to "t".
                """
                commitExecuted.append(True)
                return t.execSQL("teardown", [])

            hook(t, holdAReference)
            finish(t)

        self.failIf(commitExecuted, "Commit hook executed.")
        carefullyManagedScope()
        tc.checkTrash()

    def test_noGarbageOnCommit(self):
        """
        Committing a transaction does not cause gc garbage.
        """
        self.circularReferenceTest(
            lambda txn: txn.commit(),
            lambda txn, hook: txn.preCommit(hook)
        )

    def test_noGarbageOnCommitWithAbortHook(self):
        """
        Committing a transaction does not cause gc garbage.
        """
        self.circularReferenceTest(
            lambda txn: txn.commit(),
            lambda txn, hook: txn.postAbort(hook)
        )

    def test_noGarbageOnAbort(self):
        """
        Aborting a transaction does not cause gc garbage.
        """
        self.circularReferenceTest(
            lambda txn: txn.abort(),
            lambda txn, hook: txn.preCommit(hook)
        )

    def test_noGarbageOnAbortWithPostCommitHook(self):
        """
        Aborting a transaction does not cause gc garbage.
        """
        self.circularReferenceTest(
            lambda txn: txn.abort(),
            lambda txn, hook: txn.postCommit(hook)
        )

    def test_tooManyConnectionsWhileOthersFinish(self):
        """
        L{ConnectionPool.connection} will not spawn more than the maximum
        connections if there are finishing transactions outstanding.
        """
        a = self.createTransaction()
        b = self.createTransaction()
        self.pauseHolders()
        a.abort()
        b.abort()

        # Remove the holders for the existing connections, so that the "extra"
        # connection() call wins the race and gets executed first.
        oldholders = list(self.holders)
        self.holders[:] = []
        self.createTransaction()
        self.flushHolders()
        self.assertEquals(len(self.factory.connections), 2)

        self.holders = oldholders
        self.flushHolders()

    def setParamstyle(self, paramstyle):
        """
        Change the paramstyle of the transaction under test.
        """
        self.pool.dbtype = self.pool.dbtype.copyreplace(paramstyle=paramstyle)

    def test_propagateParamstyle(self):
        """
        Each different type of L{ISQLExecutor} relays the C{paramstyle}
        attribute from the L{ConnectionPool}.
        """
        TEST_PARAMSTYLE = "justtesting"
        self.setParamstyle(TEST_PARAMSTYLE)
        normaltxn = self.createTransaction()
        self.assertEquals(normaltxn.dbtype.paramstyle, TEST_PARAMSTYLE)
        self.assertEquals(normaltxn.commandBlock().dbtype.paramstyle, TEST_PARAMSTYLE)
        self.pauseHolders()
        extra = []
        extra.append(self.createTransaction())
        waitingtxn = self.createTransaction()
        self.assertEquals(waitingtxn.dbtype.paramstyle, TEST_PARAMSTYLE)
        self.flushHolders()
        self.pool.stopService()
        notxn = self.createTransaction()
        self.assertEquals(notxn.dbtype.paramstyle, TEST_PARAMSTYLE)

    def setDialect(self, dialect):
        """
        Change the dialect of the transaction under test.
        """
        self.pool.dbtype = self.pool.dbtype.copyreplace(dialect=dialect)

    def test_propagateDialect(self):
        """
        Each different type of L{ISQLExecutor} relays the C{dialect}
        attribute from the L{ConnectionPool}.
        """
        TEST_DIALECT = "otherdialect"
        self.setDialect(TEST_DIALECT)
        normaltxn = self.createTransaction()
        self.assertEquals(normaltxn.dbtype.dialect, TEST_DIALECT)
        self.assertEquals(normaltxn.commandBlock().dbtype.dialect, TEST_DIALECT)
        self.pauseHolders()
        extra = []
        extra.append(self.createTransaction())
        waitingtxn = self.createTransaction()
        self.assertEquals(waitingtxn.dbtype.dialect, TEST_DIALECT)
        self.flushHolders()
        self.pool.stopService()
        notxn = self.createTransaction()
        self.assertEquals(notxn.dbtype.dialect, TEST_DIALECT)

    def test_reConnectWhenFirstExecFails(self):
        """
        Generally speaking, DB-API 2.0 adapters do not provide information
        about the cause of a failed C{execute} method; they definitely don't
        provide it in a way which can be identified as related to the syntax of
        the query, the state of the database itself, the state of the
        connection, etc.

        Therefore the best general heuristic for whether the connection to the
        database has been lost and needs to be re-established is to catch
        exceptions which are raised by the I{first} statement executed in a
        transaction.
        """
        # Allow C{connect} to succeed.  This should behave basically the same
        # whether connect() happened to succeed in some previous transaction
        # and it's recycling the underlying transaction, or connect() just
        # succeeded.  Either way you just have a _SingleTxn wrapping a
        # _ConnectedTxn.
        txn = self.createTransaction()
        self.assertEquals(len(self.factory.connections), 1,
                          "Sanity check failed.")

        class CustomExecuteFailed(Exception):
            """
            Custom "execute-failed" exception.
            """

        self.factory.connections[0].executeWillFail(CustomExecuteFailed)
        results = self.resultOf(txn.execSQL("hello, world!"))
        [[[_ignore_counter, echo]]] = results
        self.assertEquals("hello, world!", echo)

        # Two execution attempts should have been made, one on each connection.
        # The first failed with a RuntimeError, but that is deliberately
        # obscured, because then we tried again and it succeeded.
        self.assertEquals(
            len(self.factory.connections), 2,
            "No new connection opened."
        )
        self.assertEquals(self.factory.connections[0].executions, 1)
        self.assertEquals(self.factory.connections[1].executions, 1)
        self.assertEquals(self.factory.connections[0].closed, True)
        self.assertEquals(self.factory.connections[1].closed, False)

        # Nevertheless, since there is currently no classification of "safe"
        # errors, we should probably log these messages when they occur.
        self.assertEquals(len(self.flushLoggedErrors(CustomExecuteFailed)), 1)

    def test_reConnectWhenFirstExecOnExistingConnectionFails(
        self, moreFailureSetup=lambda factory: None
    ):
        """
        Another situation that might arise is that a connection will be
        successfully connected, executed and recycled into the connection pool;
        then, the database server will shut down and the connections will die,
        but we will be none the wiser until we try to use them.
        """
        txn = self.createTransaction()
        moreFailureSetup(self.factory)
        self.assertEquals(
            len(self.factory.connections), 1, "Sanity check failed."
        )
        results = self.resultOf(txn.execSQL("hello, world!"))
        txn.commit()
        [[[_ignore_counter, echo]]] = results
        self.assertEquals("hello, world!", echo)
        txn2 = self.createTransaction()
        self.assertEquals(
            len(self.factory.connections), 1, "Sanity check failed."
        )

        class CustomExecFail(Exception):
            """
            Custom C{execute()} failure.
            """

        self.factory.connections[0].executeWillFail(CustomExecFail)
        results = self.resultOf(txn2.execSQL("second try!"))
        txn2.commit()
        [[[_ignore_counter, echo]]] = results
        self.assertEquals("second try!", echo)
        self.assertEquals(len(self.flushLoggedErrors(CustomExecFail)), 1)

    def test_closeExceptionDoesntHinderReconnection(self):
        """
        In some database bindings, if the server closes the connection,
        C{close()} will fail.  If C{close} fails, there's not much that could
        mean except that the connection is already closed, so similar to the
        condition described in
        L{test_reConnectWhenFirstExecOnExistingConnectionFails}, the
        failure should be logged, but transparent to application code.
        """

        class BindingSpecificException(Exception):
            """
            Exception that's a placeholder for something that a database
            binding might raise.
            """

        def alsoFailClose(factory):
            factory.childCloseWillFail(BindingSpecificException())

        t = self.test_reConnectWhenFirstExecOnExistingConnectionFails(
            alsoFailClose
        )
        errors = self.flushLoggedErrors(BindingSpecificException)
        self.assertEquals(len(errors), 1)
        return t

    def test_preCommitSuccess(self):
        """
        Callables passed to L{IAsyncTransaction.preCommit} will be invoked upon
        commit.
        """
        txn = self.createTransaction()

        def simple():
            simple.done = True

        simple.done = False
        txn.preCommit(simple)
        self.assertEquals(simple.done, False)
        result = self.resultOf(txn.commit())
        self.assertEquals(len(result), 1)
        self.assertEquals(simple.done, True)

    def test_deferPreCommit(self):
        """
        If callables passed to L{IAsyncTransaction.preCommit} return
        L{Deferred}s, they will defer the actual commit operation until it has
        fired.
        """
        txn = self.createTransaction()
        d = Deferred()

        def wait():
            wait.started = True

            def executed(it):
                wait.sqlResult = it

            # To make sure the _underlying_ commit operation was Deferred, we
            # have to execute some SQL to make sure it happens.
            d.addCallback(lambda ignored: txn.execSQL("some test sql"))
            d.addCallback(executed)
            return d

        wait.started = False
        wait.sqlResult = None
        txn.preCommit(wait)
        result = self.resultOf(txn.commit())
        self.flushHolders()
        self.assertEquals(wait.started, True)
        self.assertEquals(wait.sqlResult, None)
        self.assertEquals(result, [])
        d.callback(None)
        # allow network I/O for pooled / networked implementation; there should
        # be the commit message now.
        self.flushHolders()
        self.assertEquals(len(result), 1)
        self.assertEquals(wait.sqlResult, [[1, "some test sql"]])

    def test_failPreCommit(self):
        """
        If callables passed to L{IAsyncTransaction.preCommit} raise an
        exception or return a Failure, subsequent callables will not be run,
        and the transaction will be aborted.
        """
        def test(flawedCallable, exc):
            # Set up.
            test.committed = False
            test.aborted = False
            # Create transaction and add monitoring hooks.
            txn = self.createTransaction()

            def didCommit():
                test.committed = True

            def didAbort():
                test.aborted = True

            txn.postCommit(didCommit)
            txn.postAbort(didAbort)
            txn.preCommit(flawedCallable)
            result = self.resultOf(txn.commit())
            self.flushHolders()
            self.assertResultList(result, Failure(exc()))
            self.assertEquals(test.committed, False)
            self.assertEquals(test.aborted, True)

        def failer():
            return fail(ZeroDivisionError())

        def raiser():
            raise EOFError()

        test(failer, ZeroDivisionError)
        test(raiser, EOFError)

    def test_noOpCommitDoesntHinderReconnection(self):
        """
        Until you've executed a query or performed a statement on an ADBAPI
        connection, the connection is semantically idle (between transactions).
        A .commit() or .rollback() followed immediately by a .commit() is
        therefore pointless, and can be ignored.  Furthermore, actually
        executing the commit and propagating a possible connection-oriented
        error causes clients to see errors, when, if those clients had actually
        executed any statements, the connection would have been recycled and
        the statement transparently re-executed by the logic tested by
        L{test_reConnectWhenFirstExecFails}.
        """
        txn = self.createTransaction()
        self.factory.commitFail = True
        self.factory.rollbackFail = True
        [x] = self.resultOf(txn.commit())

        # No statements have been executed, so C{commit} will *not* be
        # executed.
        self.assertEquals(self.factory.commitFail, True)
        self.assertIdentical(x, None)
        self.assertEquals(len(self.pool._free), 1)
        self.assertEquals(self.pool._finishing, [])
        self.assertEquals(len(self.factory.connections), 1)
        self.assertEquals(self.factory.connections[0].closed, False)

    def test_reConnectWhenSecondExecFailsThenFirstExecFails(self):
        """
        Other connection-oriented errors might raise exceptions if they occur
        in the middle of a transaction, but that should cause the error to be
        caught, the transaction to be aborted, and the (closed) connection to
        be recycled, where the next transaction that attempts to do anything
        with it will encounter the error immediately and discover it needs to
        be recycled.

        It would be better if this behavior were invisible, but that could only
        be accomplished with more precise database exceptions.  We may come up
        with support in the future for more precisely identifying exceptions,
        but I{unknown} exceptions should continue to be treated in this manner,
        relaying the exception back to application code but attempting a
        re-connection on the next try.
        """
        txn = self.createTransaction()
        [[[_ignore_counter, _ignore_echo]]] = self.resultOf(txn.execSQL("hello, world!", []))
        self.factory.connections[0].executeWillFail(ZeroDivisionError)
        [f] = self.resultOf(txn.execSQL("divide by zero", []))
        f.trap(self.translateError(ZeroDivisionError))
        self.assertEquals(self.factory.connections[0].executions, 2)

        # Reconnection should work exactly as before.
        self.assertEquals(self.factory.connections[0].closed, False)

        # Application code has to roll back its transaction at this point,
        # since it failed (and we don't necessarily know why it failed: not
        # enough information).
        self.resultOf(txn.abort())
        self.factory.connections[0].executions = 0  # re-set for next test
        self.assertEquals(len(self.factory.connections), 1)
        self.test_reConnectWhenFirstExecFails()

    def test_disconnectOnFailedRollback(self):
        """
        When C{rollback} fails for any reason on a connection object, then we
        don't know what state it's in.  Most likely, it's already been
        disconnected, so the connection should be closed and the transaction
        de-pooled instead of recycled.

        Also, a new connection will immediately be established to keep the pool
        size the same.
        """
        txn = self.createTransaction()
        results = self.resultOf(txn.execSQL("maybe change something!"))
        [[[_ignore_counter, echo]]] = results
        self.assertEquals("maybe change something!", echo)
        self.factory.rollbackFail = True
        [x] = self.resultOf(txn.abort())

        # Abort does not propagate the error on, the transaction merely gets
        # disposed of.
        self.assertIdentical(x, None)
        self.assertEquals(len(self.pool._free), 1)
        self.assertEquals(self.pool._finishing, [])
        self.assertEquals(len(self.factory.connections), 2)
        self.assertEquals(self.factory.connections[0].closed, True)
        self.assertEquals(self.factory.connections[1].closed, False)
        self.assertEquals(len(self.flushLoggedErrors(RollbackFail)), 1)

    def test_exceptionPropagatesFailedCommit(self):
        """
        A failed C{rollback} is fine (the premature death of the connection
        without C{commit} means that the changes are surely gone), but a failed
        C{commit} has to be relayed to client code, since that actually means
        some changes didn't hit the database.
        """
        txn = self.createTransaction()
        self.factory.commitFail = True
        results = self.resultOf(txn.execSQL("maybe change something!"))
        [[[_ignore_counter, echo]]] = results
        self.assertEquals("maybe change something!", echo)
        [x] = self.resultOf(txn.commit())
        x.trap(self.translateError(CommitFail))

        self.assertEquals(len(self.pool._free), 1)
        self.assertEquals(self.pool._finishing, [])
        self.assertEquals(len(self.factory.connections), 2)
        self.assertEquals(self.factory.connections[0].closed, True)
        self.assertEquals(self.factory.connections[1].closed, False)

    def test_commandBlock(self):
        """
        L{IAsyncTransaction.commandBlock} returns an L{IAsyncTransaction}
        provider which ensures that a block of commands are executed together.
        """
        txn = self.createTransaction()
        a = self.resultOf(txn.execSQL("a"))
        cb = txn.commandBlock()
        verifyObject(ICommandBlock, cb)
        b = self.resultOf(cb.execSQL("b"))
        d = self.resultOf(txn.execSQL("d"))
        c = self.resultOf(cb.execSQL("c"))
        cb.end()
        e = self.resultOf(txn.execSQL("e"))

        self.assertEquals(
            self.factory.connections[0].cursors[0].allExecutions,
            [("a", []), ("b", []), ("c", []), ("d", []), ("e", [])]
        )
        self.assertEquals(len(a), 1)
        self.assertEquals(len(b), 1)
        self.assertEquals(len(c), 1)
        self.assertEquals(len(d), 1)
        self.assertEquals(len(e), 1)

    def test_commandBlockWithLatency(self):
        """
        A block returned by L{IAsyncTransaction.commandBlock} won't start
        executing until all SQL statements scheduled before it have completed.
        """
        self.pauseHolders()
        txn = self.createTransaction()
        a = self.resultOf(txn.execSQL("a"))
        b = self.resultOf(txn.execSQL("b"))
        cb = txn.commandBlock()
        c = self.resultOf(cb.execSQL("c"))
        d = self.resultOf(cb.execSQL("d"))
        e = self.resultOf(txn.execSQL("e"))
        cb.end()
        self.flushHolders()

        self.assertEquals(
            self.factory.connections[0].cursors[0].allExecutions,
            [("a", []), ("b", []), ("c", []), ("d", []), ("e", [])]
        )

        self.assertEquals(len(a), 1)
        self.assertEquals(len(b), 1)
        self.assertEquals(len(c), 1)
        self.assertEquals(len(d), 1)
        self.assertEquals(len(e), 1)

    def test_twoCommandBlocks(self, flush=lambda: None):
        """
        When execution of one command block is complete, it will proceed to the
        next queued block, then to regular SQL executed on the transaction.
        """
        txn = self.createTransaction()
        cb1 = txn.commandBlock()
        cb2 = txn.commandBlock()
        txn.execSQL("e")
        cb1.execSQL("a")
        cb2.execSQL("c")
        cb1.execSQL("b")
        cb2.execSQL("d")
        cb2.end()
        cb1.end()
        flush()
        self.flushHolders()
        self.assertEquals(
            self.factory.connections[0].cursors[0].allExecutions,
            [("a", []), ("b", []), ("c", []), ("d", []), ("e", [])]
        )

    def test_twoCommandBlocksLatently(self):
        """
        Same as L{test_twoCommandBlocks}, but with slower callbacks.
        """
        self.pauseHolders()
        self.test_twoCommandBlocks(self.flushHolders)

    def test_commandBlockEndTwice(self):
        """
        L{CommandBlock.end} will raise L{AlreadyFinishedError} when called more
        than once.
        """
        txn = self.createTransaction()
        block = txn.commandBlock()
        block.end()
        self.assertRaises(AlreadyFinishedError, block.end)

    def test_commandBlockDelaysCommit(self):
        """
        Some command blocks need to run asynchronously, without the overall
        transaction-managing code knowing how far they've progressed.
        Therefore when you call {IAsyncTransaction.commit}(), it should not
        actually take effect if there are any pending command blocks.
        """
        txn = self.createTransaction()
        block = txn.commandBlock()
        commitResult = self.resultOf(txn.commit())
        self.resultOf(block.execSQL("in block"))
        self.assertEquals(commitResult, [])
        self.assertEquals(
            self.factory.connections[0].cursors[0].allExecutions,
            [("in block", [])]
        )
        block.end()
        self.flushHolders()
        self.assertEquals(commitResult, [None])

    def test_commandBlockDoesntDelayAbort(self):
        """
        A L{CommandBlock} can't possibly have anything interesting to say about
        a transaction that gets rolled back, so C{abort} applies immediately;
        all outstanding C{execSQL}s will fail immediately, on both command
        blocks and on the transaction itself.
        """
        txn = self.createTransaction()
        block = txn.commandBlock()
        block2 = txn.commandBlock()
        abortResult = self.resultOf(txn.abort())
        self.assertEquals(abortResult, [None])
        self.assertRaises(AlreadyFinishedError, block2.execSQL, "bar")
        self.assertRaises(AlreadyFinishedError, block.execSQL, "foo")
        self.assertRaises(AlreadyFinishedError, txn.execSQL, "baz")
        self.assertEquals(
            self.factory.connections[0].cursors[0].allExecutions, []
        )
        # end() should _not_ raise an exception, because this is the sort of
        # thing that might be around a try/finally or try/except; it's just
        # putting the commandBlock itself into a state consistent with the
        # transaction.
        block.end()
        block2.end()

    def test_endedBlockDoesntExecuteMoreSQL(self):
        """
        Attempting to execute SQL on a L{CommandBlock} which has had C{end}
        called on it will result in an L{AlreadyFinishedError}.
        """
        txn = self.createTransaction()
        block = txn.commandBlock()
        block.end()
        self.assertRaises(AlreadyFinishedError, block.execSQL, "hello")
        self.assertEquals(
            self.factory.connections[0].cursors[0].allExecutions, []
        )

    def test_commandBlockAfterCommitRaises(self):
        """
        Once an L{IAsyncTransaction} has been committed, L{commandBlock} raises
        an exception.
        """
        txn = self.createTransaction()
        txn.commit()
        self.assertRaises(AlreadyFinishedError, txn.commandBlock)

    def test_commandBlockAfterAbortRaises(self):
        """
        Once an L{IAsyncTransaction} has been committed, L{commandBlock} raises
        an exception.
        """
        txn = self.createTransaction()
        self.resultOf(txn.abort())
        self.assertRaises(AlreadyFinishedError, txn.commandBlock)

    def test_raiseOnZeroRowCount(self):
        """
        L{IAsyncTransaction.execSQL} will return a L{Deferred} failing with the
        exception passed as its raiseOnZeroRowCount argument if the underlying
        query returns no rows.
        """
        self.factory.hasResults = False
        txn = self.createTransaction()
        f = self.resultOf(
            txn.execSQL("hello", raiseOnZeroRowCount=ZeroDivisionError)
        )[0]
        self.assertRaises(ZeroDivisionError, f.raiseException)
        txn.commit()

    def test_raiseOnZeroRowCountWithUnreliableRowCount(self):
        """
        As it turns out, some databases can't reliably tell you how many rows
        they're going to fetch via the C{rowcount} attribute before the rows
        have actually been fetched, so the C{raiseOnZeroRowCount} will I{not}
        raise an exception if C{rowcount} is zero but C{description} and
        C{fetchall} indicates the presence of some rows.
        """
        self.factory.hasResults = True
        self.factory.shouldUpdateRowcount = False
        txn = self.createTransaction()
        r = self.resultOf(
            txn.execSQL("some-rows", raiseOnZeroRowCount=RuntimeError)
        )
        [[[_ignore_counter, echo]]] = r
        self.assertEquals(echo, "some-rows")


class IOPump(object):
    """
    Connect a client and a server.

    @ivar client: a client protocol

    @ivar server: a server protocol
    """

    def __init__(self, client, server):
        self.client = client
        self.server = server
        self.clientTransport = StringTransport()
        self.serverTransport = StringTransport()
        self.client.makeConnection(self.clientTransport)
        self.server.makeConnection(self.serverTransport)
        self.c2s = [self.clientTransport, self.server]
        self.s2c = [self.serverTransport, self.client]

    def moveData(self, (outTransport, inProtocol)):
        """
        Move data from a L{StringTransport} to an L{IProtocol}.

        @return: C{True} if any data was moved, C{False} if no data was moved.
        """
        data = outTransport.io.getvalue()
        outTransport.io.seek(0)
        outTransport.io.truncate()
        if data:
            inProtocol.dataReceived(data)
            return True
        else:
            return False

    def pump(self):
        """
        Deliver all input from the client to the server, then from the server
        to the client.
        """
        a = self.moveData(self.c2s)
        b = self.moveData(self.s2c)
        return a or b

    def flush(self, maxTurns=100):
        """
        Continue pumping until no more data is flowing.
        """
        turns = 0
        while self.pump():
            turns += 1
            if turns > maxTurns:
                raise RuntimeError("Ran too long!")


class NetworkedPoolHelper(ConnectionPoolHelper):
    """
    An extension of L{ConnectionPoolHelper} that can set up a
    L{ConnectionPoolClient} and L{ConnectionPoolConnection} attached to each
    other.
    """

    def setUp(self):
        """
        Do the same setup from L{ConnectionPoolBase}, but also establish a
        loopback connection between a L{ConnectionPoolConnection} and a
        L{ConnectionPoolClient}.
        """
        super(NetworkedPoolHelper, self).setUp()
        self.pump = IOPump(
            ConnectionPoolClient(
                dbtype=self.dbtype,
            ),
            ConnectionPoolConnection(self.pool)
        )

    def flushHolders(self):
        """
        In addition to flushing the L{ThreadHolder} stubs, also flush any
        pending network I/O.
        """
        self.pump.flush()
        super(NetworkedPoolHelper, self).flushHolders()
        self.pump.flush()

    def createTransaction(self):
        txn = self.pump.client.newTransaction()
        self.pump.flush()
        return txn

    def translateError(self, err):
        """
        All errors raised locally will unfortunately be translated into
        UnknownRemoteError, since AMP requires specific enumeration of all of
        them.  Flush the locally logged error of the given type and return
        L{UnknownRemoteError}.
        """
        if err in Commit.errors:
            return err
        self.flushLoggedErrors(err)
        return FailsafeException

    def resultOf(self, it):
        result = resultOf(it)
        self.pump.flush()
        return result


class NetworkedConnectionPoolTests(NetworkedPoolHelper, ConnectionPoolTests):
    """
    Tests for L{ConnectionPoolConnection} and L{ConnectionPoolClient}
    interacting with each other.
    """

    def setParamstyle(self, paramstyle):
        """
        Change the paramstyle on both the pool and the client.
        """
        super(NetworkedConnectionPoolTests, self).setParamstyle(paramstyle)
        self.pump.client.dbtype = self.pump.client.dbtype.copyreplace(paramstyle=paramstyle)

    def setDialect(self, dialect):
        """
        Change the dialect on both the pool and the client.
        """
        super(NetworkedConnectionPoolTests, self).setDialect(dialect)
        self.pump.client.dbtype = self.pump.client.dbtype.copyreplace(dialect=dialect)

    def test_newTransaction(self):
        """
        L{ConnectionPoolClient.newTransaction} returns a provider of
        L{IAsyncTransaction}, and creates a new transaction on the server side.
        """
        txn = self.pump.client.newTransaction()
        verifyObject(IAsyncTransaction, txn)
        self.pump.flush()
        self.assertEquals(len(self.factory.connections), 1)


class HookableOperationTests(TestCase):
    """
    Tests for L{_HookableOperation}.
    """

    @inlineCallbacks
    def test_clearPreventsSubsequentAddHook(self):
        """
        After clear() or runHooks() are called, subsequent calls to addHook()
        are NO-OPs.
        """
        def hook():
            return succeed(None)

        hookOp = _HookableOperation()
        hookOp.addHook(hook)
        self.assertEquals(len(hookOp._hooks), 1)
        hookOp.clear()
        self.assertEquals(hookOp._hooks, None)

        hookOp = _HookableOperation()
        hookOp.addHook(hook)
        yield hookOp.runHooks()
        self.assertEquals(hookOp._hooks, None)
        hookOp.addHook(hook)
        self.assertEquals(hookOp._hooks, None)