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