##// END OF EJS Templates
tests: Add session factory fixture and use it in tests.
Martin Bornhold -
r289:d3e9c393 default
parent child Browse files
Show More
@@ -1,90 +1,100 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2016 RhodeCode GmbH
3 # Copyright (C) 2010-2016 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import logging
21 import logging
22
22
23 import mock
23 import mock
24 import msgpack
24 import msgpack
25 import pytest
25 import pytest
26
26
27 from rhodecode.lib import vcs
27 from rhodecode.lib import vcs
28 from rhodecode.lib.vcs import client_http
28 from rhodecode.lib.vcs import client_http
29
29
30
30
31 def test_uses_persistent_http_connections(caplog, vcsbackend_hg):
31 def test_uses_persistent_http_connections(caplog, vcsbackend_hg):
32 repo = vcsbackend_hg.repo
32 repo = vcsbackend_hg.repo
33 remote_call = repo._remote.branches
33 remote_call = repo._remote.branches
34
34
35 with caplog.at_level(logging.INFO):
35 with caplog.at_level(logging.INFO):
36 for x in range(5):
36 for x in range(5):
37 remote_call(normal=True, closed=False)
37 remote_call(normal=True, closed=False)
38
38
39 new_connections = [
39 new_connections = [
40 r for r in caplog.record_tuples() if is_new_connection(*r)]
40 r for r in caplog.record_tuples() if is_new_connection(*r)]
41 assert len(new_connections) <= 1
41 assert len(new_connections) <= 1
42
42
43
43
44 def is_new_connection(logger, level, message):
44 def is_new_connection(logger, level, message):
45 return (
45 return (
46 logger == 'requests.packages.urllib3.connectionpool' and
46 logger == 'requests.packages.urllib3.connectionpool' and
47 message.startswith('Starting new HTTP'))
47 message.startswith('Starting new HTTP'))
48
48
49
49
50 @pytest.fixture
50 @pytest.fixture
51 def stub_session():
51 def stub_session():
52 """
52 """
53 Stub of `requests.Session()`.
53 Stub of `requests.Session()`.
54 """
54 """
55 session = mock.Mock()
55 session = mock.Mock()
56 session.post().content = msgpack.packb({})
56 session.post().content = msgpack.packb({})
57 session.reset_mock()
57 session.reset_mock()
58 return session
58 return session
59
59
60
60
61 def test_repo_maker_uses_session_for_classmethods(stub_session):
61 @pytest.fixture
62 def stub_session_factory(stub_session):
63 """
64 Stub of `rhodecode.lib.vcs.client_http.ThreadlocalSessionFactory`.
65 """
66 session_factory = mock.Mock()
67 session_factory.return_value = stub_session
68 return session_factory
69
70
71 def test_repo_maker_uses_session_for_classmethods(stub_session_factory):
62 repo_maker = client_http.RepoMaker(
72 repo_maker = client_http.RepoMaker(
63 'server_and_port', 'endpoint', stub_session)
73 'server_and_port', 'endpoint', stub_session_factory)
64 repo_maker.example_call()
74 repo_maker.example_call()
65 stub_session.post.assert_called_with(
75 stub_session_factory().post.assert_called_with(
66 'http://server_and_port/endpoint', data=mock.ANY)
76 'http://server_and_port/endpoint', data=mock.ANY)
67
77
68
78
69 def test_repo_maker_uses_session_for_instance_methods(
79 def test_repo_maker_uses_session_for_instance_methods(
70 stub_session, config):
80 stub_session_factory, config):
71 repo_maker = client_http.RepoMaker(
81 repo_maker = client_http.RepoMaker(
72 'server_and_port', 'endpoint', stub_session)
82 'server_and_port', 'endpoint', stub_session_factory)
73 repo = repo_maker('stub_path', config)
83 repo = repo_maker('stub_path', config)
74 repo.example_call()
84 repo.example_call()
75 stub_session.post.assert_called_with(
85 stub_session_factory().post.assert_called_with(
76 'http://server_and_port/endpoint', data=mock.ANY)
86 'http://server_and_port/endpoint', data=mock.ANY)
77
87
78
88
79 @mock.patch('rhodecode.lib.vcs.client_http.ThreadlocalSessionFactory')
89 @mock.patch('rhodecode.lib.vcs.client_http.ThreadlocalSessionFactory')
80 @mock.patch('rhodecode.lib.vcs.connection')
90 @mock.patch('rhodecode.lib.vcs.connection')
81 def test_connect_passes_in_the_same_session(connection, session_factory_class,
91 def test_connect_passes_in_the_same_session(
82 stub_session):
92 connection, session_factory_class, stub_session):
83 session_factory = session_factory_class.return_value
93 session_factory = session_factory_class.return_value
84 session_factory.return_value = stub_session
94 session_factory.return_value = stub_session
85
95
86 vcs.connect_http('server_and_port')
96 vcs.connect_http('server_and_port')
87
97
88 assert connection.Hg._session_factory() == stub_session
98 assert connection.Hg._session_factory() == stub_session
89 assert connection.Svn._session_factory() == stub_session
99 assert connection.Svn._session_factory() == stub_session
90 assert connection.Git._session_factory() == stub_session
100 assert connection.Git._session_factory() == stub_session
General Comments 0
You need to be logged in to leave comments. Login now