summaryrefslogtreecommitdiff
path: root/tooz/tests/test_memcache.py
blob: d3b6d8a4f6daec1118adf2759fd755cd1fe278f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-

# Copyright (c) 2015 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import socket
from unittest import mock

from testtools import testcase

from tooz import coordination
from tooz import tests


class TestMemcacheDriverFailures(testcase.TestCase):
    FAKE_URL = "memcached://mocked-not-really-localhost"

    @mock.patch('pymemcache.client.PooledClient')
    def test_client_failure_start(self, mock_client_cls):
        mock_client_cls.side_effect = socket.timeout('timed-out')
        member_id = tests.get_random_uuid()
        coord = coordination.get_coordinator(self.FAKE_URL, member_id)
        self.assertRaises(coordination.ToozConnectionError, coord.start)

    @mock.patch('pymemcache.client.PooledClient')
    def test_client_failure_join(self, mock_client_cls):
        mock_client = mock.MagicMock()
        mock_client_cls.return_value = mock_client
        member_id = tests.get_random_uuid()
        coord = coordination.get_coordinator(self.FAKE_URL, member_id)
        coord.start()
        mock_client.gets.side_effect = socket.timeout('timed-out')
        fut = coord.join_group(tests.get_random_uuid())
        self.assertRaises(coordination.ToozConnectionError, fut.get)

    @mock.patch('pymemcache.client.PooledClient')
    def test_client_failure_leave(self, mock_client_cls):
        mock_client = mock.MagicMock()
        mock_client_cls.return_value = mock_client
        member_id = tests.get_random_uuid()
        coord = coordination.get_coordinator(self.FAKE_URL, member_id)
        coord.start()
        mock_client.gets.side_effect = socket.timeout('timed-out')
        fut = coord.leave_group(tests.get_random_uuid())
        self.assertRaises(coordination.ToozConnectionError, fut.get)

    @mock.patch('pymemcache.client.PooledClient')
    def test_client_failure_heartbeat(self, mock_client_cls):
        mock_client = mock.MagicMock()
        mock_client_cls.return_value = mock_client
        member_id = tests.get_random_uuid()
        coord = coordination.get_coordinator(self.FAKE_URL, member_id)
        coord.start()
        mock_client.set.side_effect = socket.timeout('timed-out')
        self.assertRaises(coordination.ToozConnectionError, coord.heartbeat)

    @mock.patch(
        'tooz.coordination.CoordinationDriverCachedRunWatchers.run_watchers',
        autospec=True)
    @mock.patch('pymemcache.client.PooledClient')
    def test_client_run_watchers_mixin(self, mock_client_cls,
                                       mock_run_watchers):
        mock_client = mock.MagicMock()
        mock_client_cls.return_value = mock_client
        member_id = tests.get_random_uuid()
        coord = coordination.get_coordinator(self.FAKE_URL, member_id)
        coord.start()
        coord.run_watchers()
        self.assertTrue(mock_run_watchers.called)