Revision 3dabe5d2 kamaki/clients/connection/tests.py

b/kamaki/clients/connection/tests.py
37 37
gevent.monkey.patch_all()
38 38
import gevent.pool
39 39

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

  
44
from kamaki.clients.connection.kamakicon import KamakiHTTPConnection, KamakiHTTPResponse
45
from kamaki.clients.connection import HTTPConnectionError
45
from kamaki.clients.connection.kamakicon import KamakiHTTPConnection
46 46

  
47
class testKamakiCon(unittest.TestCase):
48
	def setUp(self):
49
		self.async_pool=None
50
		self.conn1 = KamakiHTTPConnection()
51
		self.conn2 = KamakiHTTPConnection()
52
		self.conn3 = KamakiHTTPConnection()
53
		self.conn4 = KamakiHTTPConnection()
54

  
55
		self.conn1.url = 'https://pithos.okeanos.io/v1/saxtouri@admin.grnet.gr/pithos?path=files'
56
		self.conn1.set_header('X-Auth-Token', '0TpoyAXqJSPxLdDuZHiLOA==')
57
		self.conn2.url = 'https://pithos.okeanos.io/v1/saxtouri@admin.grnet.gr/pithos'
58
		self.conn2.set_header('X-Auth-Token', '0TpoyAXqJSPxLdDuZHiLOA==')
59
		self.conn3.url = 'https://pithos.okeanos.io/v1/saxtouri@admin.grnet.gr/pithos?path=subdir'
60
		self.conn3.set_header('X-Auth-Token', '0TpoyAXqJSPxLdDuZHiLOA==')
61
		self.conn4.url = 'https://pithos.okeanos.io/v1/saxtouri@admin.grnet.gr'
62
		self.conn4.set_header('X-Auth-Token', '0TpoyAXqJSPxLdDuZHiLOA==')
63 47

  
64
	def tearDown(self):
65
		pass
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'
66 56

  
67
	def _get_async_content(self, con, **kwargs):
68
		class SilentGreenlet(gevent.Greenlet):
69
			def _report_error(self, exc_info):
70
				_stderr = None
71
				try:
72
					_stderr = sys._stderr
73
					sys.stderr = StringIO()
74
					gevent.Greenlet._report_error(self, exc_info)
75
				finally:
76
					sys.stderr = _stderr
77
		POOL_SIZE = 2
78
		if self.async_pool is None:
79
			self.async_pool = gevent.pool.Pool(size=POOL_SIZE)
80
		g = SilentGreenlet(self._get_content_len, con, **kwargs)
81
		self.async_pool.start(g)
82
		return g
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==')
83 67

  
84
	def _get_content_len(self, con, **kwargs):
85
		r = con.perform_request('GET', **kwargs)
86
		return len(r.content)
68
    def tearDown(self):
69
        pass
87 70

  
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, async_headers={'X-Auth-Token':'FAKETOKEN'})
93
		h5 = self._get_async_content(self.conn1)
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
94 87

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

  
96
		while not (h1.ready() and h2.ready() and h3.ready() and h4.ready() and h5.ready()):
97
			sleep(.000001)
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)
98 99

  
99
		r1 = h1.value
100
		r2 = h2.value
101
		r3 = h3.value
102
		r4 = h4.value
103
		r5 = h5.value
104
		self.assertEqual(r1, r5)
105
		self.assertNotEqual(r2, r4)
106
		#print('1:%s 2:%s 3:%s 4:%s 5:%s'%(r1, r2, r3, r4, r5))
100
        while not (h1.ready()\
101
            and h2.ready()\
102
            and h3.ready()\
103
            and h4.ready()\
104
            and h5.ready()):
105
            sleep(.000001)
107 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))
108 115

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

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

Also available in: Unified diff