~crunch.io/ubuntu/precise/pymongo/unstable

« back to all changes in this revision

Viewing changes to test/test_pooling_gevent.py

  • Committer: Joseph Tate
  • Date: 2013-01-31 08:00:57 UTC
  • mfrom: (1.1.12)
  • Revision ID: jtate@dragonstrider.com-20130131080057-y7lv17xi6x8c1j5x
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from nose.plugins.skip import SkipTest
20
20
 
21
21
from pymongo import pool
 
22
from test.utils import looplet
22
23
from test.test_connection import host, port
23
24
from test.test_pooling_base import (
24
25
    _TestPooling, _TestMaxPoolSize, _TestPoolSocketSharing)
25
26
 
26
27
 
27
 
def looplet(greenlets):
28
 
    """World's smallest event loop; run until all greenlets are done
29
 
    """
30
 
    while True:
31
 
        done = True
32
 
 
33
 
        for g in greenlets:
34
 
            if not g.dead:
35
 
                done = False
36
 
                g.switch()
37
 
 
38
 
        if done:
39
 
            return
40
 
 
41
 
 
42
28
class TestPoolingGevent(_TestPooling, unittest.TestCase):
43
 
    """Apply all the standard pool tests to GreenletPool with Gevent"""
 
29
    """Apply all the standard pool tests with greenlets and Gevent"""
44
30
    use_greenlets = True
45
31
 
46
32
 
47
33
class TestPoolingGeventSpecial(unittest.TestCase):
48
 
    """Do a few special GreenletPool tests that don't use TestPoolingBase"""
 
34
    """Do a few special greenlet tests that don't use TestPoolingBase"""
49
35
    def test_greenlet_sockets(self):
50
36
        # Check that Pool gives two sockets to two greenlets
51
37
        try:
53
39
        except ImportError:
54
40
            raise SkipTest('greenlet not installed')
55
41
 
56
 
        cx_pool = pool.GreenletPool(
 
42
        cx_pool = pool.Pool(
57
43
            pair=(host,port),
58
44
            max_size=10,
59
45
            net_timeout=1000,
60
46
            conn_timeout=1000,
61
 
            use_ssl=False
62
 
        )
 
47
            use_ssl=False,
 
48
            use_greenlets=True)
63
49
 
64
50
        socks = []
65
51
 
76
62
        self.assertNotEqual(socks[0], socks[1])
77
63
 
78
64
    def test_greenlet_sockets_with_request(self):
79
 
        # Verify two assumptions: that start_request() with two greenlets and
80
 
        # the regular pool will fail, meaning that the two greenlets will
81
 
        # share one socket. Also check that start_request() with GreenletPool
82
 
        # succeeds, meaning that two greenlets will get different sockets (this
83
 
        # is exactly the reason for creating GreenletPool).
 
65
        # Verify two assumptions: that start_request() with two greenlets but
 
66
        # not use_greenlets fails, meaning that the two greenlets will
 
67
        # share one socket. Also check that start_request() with use_greenlets
 
68
        # succeeds, meaning that two greenlets will get different sockets.
84
69
 
85
70
        try:
86
71
            import greenlet
95
80
            use_ssl=False,
96
81
        )
97
82
 
98
 
        for pool_class, use_request, expect_success in [
99
 
            (pool.GreenletPool, True, True),
100
 
            (pool.GreenletPool, False, False),
101
 
            (pool.Pool, True, False),
102
 
            (pool.Pool, False, False),
 
83
        for use_greenlets, use_request, expect_success in [
 
84
            (True, True, True),
 
85
            (True, False, False),
 
86
            (False, True, False),
 
87
            (False, False, False),
103
88
        ]:
104
 
            cx_pool = pool_class(**pool_args)
 
89
            pool_args_cp = pool_args.copy()
 
90
            pool_args_cp['use_greenlets'] = use_greenlets
 
91
            cx_pool = pool.Pool(**pool_args_cp)
105
92
 
106
93
            # Map: greenlet -> socket
107
94
            greenlet2socks = {}
116
103
 
117
104
                for _ in range(2):
118
105
                    sock = cx_pool.get_socket()
119
 
                    cx_pool.return_socket(sock)
 
106
                    cx_pool.maybe_return_socket(sock)
120
107
                    greenlet2socks.setdefault(
121
108
                        greenlet.getcurrent(), []
122
109
                    ).append(id(sock))
144
131
 
145
132
            # If we started a request, then there was a point at which we had
146
133
            # 2 active sockets, otherwise we always used one.
147
 
            if use_request and pool_class is pool.GreenletPool:
 
134
            if use_request and use_greenlets:
148
135
                self.assertEqual(2, len(cx_pool.sockets))
149
136
            else:
150
137
                self.assertEqual(1, len(cx_pool.sockets))
166
153
            )
167
154
 
168
155
            if expect_success:
169
 
                # We used the proper pool class, so start_request successfully
 
156
                # We passed use_greenlets=True, so start_request successfully
170
157
                # distinguished between the two greenlets.
171
158
                self.assertNotEqual(
172
159
                    socks_for_gr0[0], socks_for_gr1[0],
174
161
                )
175
162
 
176
163
            else:
177
 
                # We used the wrong pool class, so start_request didn't
 
164
                # We passed use_greenlets=False, so start_request didn't
178
165
                # distinguish between the two greenlets, and it gave them both
179
166
                # the same socket.
180
167
                self.assertEqual(