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