root / kamaki / clients / connection / tests.py @ e02728f9
History | View | Annotate | Download (4.3 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 unittest |
35 |
import sys |
36 |
from StringIO import StringIO |
37 |
from time import sleep |
38 |
|
39 |
from kamaki.clients.connection.kamakicon import KamakiHTTPConnection |
40 |
|
41 |
|
42 |
class testKamakiCon(unittest.TestCase): |
43 |
def setUp(self): |
44 |
self.async_pool = None |
45 |
self.conn1 = KamakiHTTPConnection()
|
46 |
self.conn2 = KamakiHTTPConnection()
|
47 |
self.conn3 = KamakiHTTPConnection()
|
48 |
self.conn4 = KamakiHTTPConnection()
|
49 |
account = 'saxtouri@grnet.gr'
|
50 |
|
51 |
self.conn1.url =\
|
52 |
'https://pithos.okeanos.io/v1/%s/pithos?path=files' % account
|
53 |
self.conn1.set_header('X-Auth-Token', '0TpoyAXqJSPxLdDuZHiLOA==') |
54 |
self.conn2.url = 'https://pithos.okeanos.io/v1/%s/pithos' % account |
55 |
self.conn2.set_header('X-Auth-Token', '0TpoyAXqJSPxLdDuZHiLOA==') |
56 |
self.conn3.url =\
|
57 |
'https://pithos.okeanos.io/v1/%s/pithos?path=subdir' % account
|
58 |
self.conn3.set_header('X-Auth-Token', '0TpoyAXqJSPxLdDuZHiLOA==') |
59 |
self.conn4.url = 'https://pithos.okeanos.io/v1/%s' % account |
60 |
self.conn4.set_header('X-Auth-Token', '0TpoyAXqJSPxLdDuZHiLOA==') |
61 |
|
62 |
def tearDown(self): |
63 |
pass
|
64 |
|
65 |
"""
|
66 |
def _get_async_content(self, con, **kwargs):
|
67 |
class SilentGreenlet(gevent.Greenlet):
|
68 |
def _report_error(self, exc_info):
|
69 |
_stderr = None
|
70 |
try:
|
71 |
_stderr = sys._stderr
|
72 |
sys.stderr = StringIO()
|
73 |
gevent.Greenlet._report_error(self, exc_info)
|
74 |
finally:
|
75 |
sys.stderr = _stderr
|
76 |
POOL_SIZE = 2
|
77 |
if self.async_pool is None:
|
78 |
self.async_pool = gevent.pool.Pool(size=POOL_SIZE)
|
79 |
g = SilentGreenlet(self._get_content_len, con, **kwargs)
|
80 |
self.async_pool.start(g)
|
81 |
return g
|
82 |
"""
|
83 |
|
84 |
def _get_content_len(self, con, **kwargs): |
85 |
r = con.perform_request('GET', **kwargs)
|
86 |
return len(r.content) |
87 |
|
88 |
def test_gevents(self): |
89 |
h1 = self._get_async_content(self.conn1) |
90 |
h2 = self._get_async_content(self.conn2) |
91 |
h3 = self._get_async_content(self.conn3) |
92 |
h4 = self._get_async_content(self.conn2, |
93 |
async_headers={'X-Auth-Token': 'FAKETOKEN'}) |
94 |
h5 = self._get_async_content(self.conn1) |
95 |
|
96 |
while not (h1.ready()\ |
97 |
and h2.ready()\
|
98 |
and h3.ready()\
|
99 |
and h4.ready()\
|
100 |
and h5.ready()):
|
101 |
sleep(.000001)
|
102 |
|
103 |
r1 = h1.value |
104 |
r2 = h2.value |
105 |
# r3 = h3.value
|
106 |
r4 = h4.value |
107 |
r5 = h5.value |
108 |
self.assertEqual(r1, r5)
|
109 |
self.assertNotEqual(r2, r4)
|
110 |
#print('1:%s 2:%s 3:%s 4:%s 5:%s'%(r1, r2, r3, r4, r5))
|
111 |
|
112 |
if __name__ == '__main__': |
113 |
suiteFew = unittest.TestSuite() |
114 |
suiteFew.addTest(unittest.makeSuite(testKamakiCon)) |
115 |
unittest.TextTestRunner(verbosity=2).run(suiteFew)
|