Show More
@@ -1,87 +1,90 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 | def test_repo_maker_uses_session_for_classmethods(stub_session): | |
62 | repo_maker = client_http.RepoMaker( |
|
62 | repo_maker = client_http.RepoMaker( | |
63 | 'server_and_port', 'endpoint', stub_session) |
|
63 | 'server_and_port', 'endpoint', stub_session) | |
64 | repo_maker.example_call() |
|
64 | repo_maker.example_call() | |
65 | stub_session.post.assert_called_with( |
|
65 | stub_session.post.assert_called_with( | |
66 | 'http://server_and_port/endpoint', data=mock.ANY) |
|
66 | 'http://server_and_port/endpoint', data=mock.ANY) | |
67 |
|
67 | |||
68 |
|
68 | |||
69 | def test_repo_maker_uses_session_for_instance_methods( |
|
69 | def test_repo_maker_uses_session_for_instance_methods( | |
70 | stub_session, config): |
|
70 | stub_session, config): | |
71 | repo_maker = client_http.RepoMaker( |
|
71 | repo_maker = client_http.RepoMaker( | |
72 | 'server_and_port', 'endpoint', stub_session) |
|
72 | 'server_and_port', 'endpoint', stub_session) | |
73 | repo = repo_maker('stub_path', config) |
|
73 | repo = repo_maker('stub_path', config) | |
74 | repo.example_call() |
|
74 | repo.example_call() | |
75 | stub_session.post.assert_called_with( |
|
75 | stub_session.post.assert_called_with( | |
76 | 'http://server_and_port/endpoint', data=mock.ANY) |
|
76 | 'http://server_and_port/endpoint', data=mock.ANY) | |
77 |
|
77 | |||
78 |
|
78 | |||
|
79 | @mock.patch('rhodecode.lib.vcs.client_http.ThreadlocalSessionFactory') | |||
79 | @mock.patch('rhodecode.lib.vcs.connection') |
|
80 | @mock.patch('rhodecode.lib.vcs.connection') | |
80 |
def test_connect_passes_in_the_same_session(connection, |
|
81 | def test_connect_passes_in_the_same_session(connection, session_factory_class, | |
81 | session_factory_patcher = mock.patch.object( |
|
82 | stub_session): | |
82 | vcs, '_create_http_rpc_session', return_value=stub_session) |
|
83 | session_factory = session_factory_class.return_value | |
83 | with session_factory_patcher: |
|
84 | session_factory.return_value = stub_session | |
|
85 | ||||
84 |
|
|
86 | vcs.connect_http('server_and_port') | |
85 | assert connection.Hg._session == stub_session |
|
87 | ||
86 |
assert connection. |
|
88 | assert connection.Hg._session_factory() == stub_session | |
87 |
assert connection. |
|
89 | assert connection.Svn._session_factory() == stub_session | |
|
90 | assert connection.Git._session_factory() == stub_session |
General Comments 0
You need to be logged in to leave comments.
Login now