Passed
Push — master ( e601a7...eb7115 )
by Humberto
02:04 queued 14s
created

TestLink.test_link_id()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
"""Link tests."""
2
import logging
3
import time
4
import unittest
5
from unittest.mock import Mock
6
7
from kytos.core.interface import Interface
8
from kytos.core.link import Link
9
from kytos.core.switch import Switch
10
11
logging.basicConfig(level=logging.CRITICAL)
12
13
14
class TestLink(unittest.TestCase):
15
    """Test Links."""
16
17
    def setUp(self):
18
        """Create interface objects."""
19
        self.iface1, self.iface2 = self._get_v0x04_ifaces()
20
21
    @staticmethod
22
    def _get_v0x04_ifaces(*args, **kwargs):
23
        """Create a pair of v0x04 interfaces with optional extra arguments."""
24
        switch1 = Switch('dpid1')
25
        switch1.connection = Mock()
26
        switch1.connection.protocol.version = 0x04
27
        iface1 = Interface('interface1', 41, switch1, *args, **kwargs)
28
29
        switch2 = Switch('dpid2')
30
        switch2.connection = Mock()
31
        switch2.connection.protocol.version = 0x04
32
        iface2 = Interface('interface2', 42, switch2, *args, **kwargs)
33
34
        return iface1, iface2
35
36
    def test_init(self):
37
        """Test normal Link initialization."""
38
        link = Link(self.iface1, self.iface2)
39
        self.assertIsInstance(link, Link)
40
        self.assertIs(link.is_active(), True)
41
        self.assertIs(link.is_enabled(), False)
42
43
    def test_init_with_null_endpoints(self):
44
        """Test initialization with None as endpoints."""
45
        with self.assertRaises(ValueError):
46
            Link(None, None)
47
48
    def test_link_id(self):
49
        """Test equality of links with the same values ​​in different order."""
50
        link1 = Link(self.iface1, self.iface2)
51
        link2 = Link(self.iface2, self.iface1)
52
        self.assertEqual(link1.id, link2.id)
53
54
    def test_get_next_available_tag(self):
55
        """Test get next available tags returns different tags"""
56
        link = Link(self.iface1, self.iface2)
57
        tag = link.get_next_available_tag()
58
        next_tag = link.get_next_available_tag()
59
60
        self.assertNotEqual(tag, next_tag)
61
62
    def test_get_tag_multiple_calls(self):
63
        """Test get next available tags returns different tags"""
64
        link = Link(self.iface1, self.iface2)
65
        tag = link.get_next_available_tag()
66
        next_tag = link.get_next_available_tag()
67
        self.assertNotEqual(next_tag.value, tag.value)
68
69
    def test_next_tag_with_use_tags(self):
70
        """Test get next availabe tags returns different tags"""
71
        link = Link(self.iface1, self.iface2)
72
        tag = link.get_next_available_tag()
73
        is_available = link.is_tag_available(tag)
74
        self.assertFalse(is_available)
75
        link.use_tag(tag)
76
77
    def test_tag_life_cicle(self):
78
        """Test get next available tags returns different tags"""
79
        link = Link(self.iface1, self.iface2)
80
        tag = link.get_next_available_tag()
81
82
        is_available = link.is_tag_available(tag)
83
        self.assertFalse(is_available)
84
85
        link.make_tag_available(tag)
86
        is_available = link.is_tag_available(tag)
87
        self.assertTrue(is_available)
88
89
    def test_concurrent_get_next_tag(self):
90
        """Test get next available tags in concurrent execution"""
91
        # pylint: disable=import-outside-toplevel
92
        from tests.helper import test_concurrently
93
        _link = Link(self.iface1, self.iface2)
94
95
        _i = []
96
        _initial_size = len(_link.endpoint_a.available_tags)
97
98
        @test_concurrently(20)
99
        def test_get_next_available_tag():
100
            """Assert that get_next_available_tag() returns different tags."""
101
            _i.append(1)
102
            _i_len = len(_i)
103
            tag = _link.get_next_available_tag()
104
            time.sleep(0.0001)
105
            _link.use_tag(tag)
106
107
            next_tag = _link.get_next_available_tag()
108
            _link.use_tag(next_tag)
109
110
            self.assertNotEqual(tag, next_tag)
111
112
        test_get_next_available_tag()
113
114
        # sleep not needed because test_concurrently waits for all threads
115
        # to finish before returning.
116
        # time.sleep(0.1)
117
118
        # Check if after the 20th iteration we have 40 tags
119
        # It happens because we get 2 tags for every iteration
120
        self.assertEqual(_initial_size,
121
                         len(_link.endpoint_a.available_tags) + 40)
122