Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / connection / tests.py @ 3dabe5d2

History | View | Annotate | Download (4.4 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import gevent
35
import gevent.monkey
36
# Monkey-patch everything for gevent early on
37
gevent.monkey.patch_all()
38
import gevent.pool
39

    
40
import unittest
41
import sys
42
from StringIO import StringIO
43
from time import sleep
44

    
45
from kamaki.clients.connection.kamakicon import KamakiHTTPConnection
46

    
47

    
48
class testKamakiCon(unittest.TestCase):
49
    def setUp(self):
50
        self.async_pool = None
51
        self.conn1 = KamakiHTTPConnection()
52
        self.conn2 = KamakiHTTPConnection()
53
        self.conn3 = KamakiHTTPConnection()
54
        self.conn4 = KamakiHTTPConnection()
55
        account = 'saxtouri@grnet.gr'
56

    
57
        self.conn1.url =\
58
            'https://pithos.okeanos.io/v1/%s/pithos?path=files' % account
59
        self.conn1.set_header('X-Auth-Token', '0TpoyAXqJSPxLdDuZHiLOA==')
60
        self.conn2.url = 'https://pithos.okeanos.io/v1/%s/pithos' % account
61
        self.conn2.set_header('X-Auth-Token', '0TpoyAXqJSPxLdDuZHiLOA==')
62
        self.conn3.url =\
63
            'https://pithos.okeanos.io/v1/%s/pithos?path=subdir' % account
64
        self.conn3.set_header('X-Auth-Token', '0TpoyAXqJSPxLdDuZHiLOA==')
65
        self.conn4.url = 'https://pithos.okeanos.io/v1/%s' % account
66
        self.conn4.set_header('X-Auth-Token', '0TpoyAXqJSPxLdDuZHiLOA==')
67

    
68
    def tearDown(self):
69
        pass
70

    
71
    def _get_async_content(self, con, **kwargs):
72
        class SilentGreenlet(gevent.Greenlet):
73
            def _report_error(self, exc_info):
74
                _stderr = None
75
                try:
76
                    _stderr = sys._stderr
77
                    sys.stderr = StringIO()
78
                    gevent.Greenlet._report_error(self, exc_info)
79
                finally:
80
                    sys.stderr = _stderr
81
        POOL_SIZE = 2
82
        if self.async_pool is None:
83
            self.async_pool = gevent.pool.Pool(size=POOL_SIZE)
84
        g = SilentGreenlet(self._get_content_len, con, **kwargs)
85
        self.async_pool.start(g)
86
        return g
87

    
88
    def _get_content_len(self, con, **kwargs):
89
        r = con.perform_request('GET', **kwargs)
90
        return len(r.content)
91

    
92
    def test_gevents(self):
93
        h1 = self._get_async_content(self.conn1)
94
        h2 = self._get_async_content(self.conn2)
95
        h3 = self._get_async_content(self.conn3)
96
        h4 = self._get_async_content(self.conn2,
97
            async_headers={'X-Auth-Token': 'FAKETOKEN'})
98
        h5 = self._get_async_content(self.conn1)
99

    
100
        while not (h1.ready()\
101
            and h2.ready()\
102
            and h3.ready()\
103
            and h4.ready()\
104
            and h5.ready()):
105
            sleep(.000001)
106

    
107
        r1 = h1.value
108
        r2 = h2.value
109
        # r3 = h3.value
110
        r4 = h4.value
111
        r5 = h5.value
112
        self.assertEqual(r1, r5)
113
        self.assertNotEqual(r2, r4)
114
        #print('1:%s 2:%s 3:%s 4:%s 5:%s'%(r1, r2, r3, r4, r5))
115

    
116
        gevent.joinall([h1, h2, h3, h4, h5])
117

    
118
if __name__ == '__main__':
119
    suiteFew = unittest.TestSuite()
120
    suiteFew.addTest(unittest.makeSuite(testKamakiCon))
121
    unittest.TextTestRunner(verbosity=2).run(suiteFew)