root / py / scenic / test / test_ports.py @ efa933e5
History | View | Annotate | Download (2.3 kB)
| 1 |
#!/usr/bin/env python
|
|---|---|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
#
|
| 4 |
# Miville
|
| 5 |
# Copyright (C) 2008 Société des arts technologiques (SAT)
|
| 6 |
# http://www.sat.qc.ca
|
| 7 |
# All rights reserved.
|
| 8 |
#
|
| 9 |
# This file is free software: you can redistribute it and/or modify
|
| 10 |
# it under the terms of the GNU General Public License as published by
|
| 11 |
# the Free Software Foundation, either version 2 of the License, or
|
| 12 |
# (at your option) any later version.
|
| 13 |
#
|
| 14 |
# Miville is distributed in the hope that it will be useful,
|
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 17 |
# GNU General Public License for more details.
|
| 18 |
#
|
| 19 |
# You should have received a copy of the GNU General Public License
|
| 20 |
# along with Miville. If not, see <http://www.gnu.org/licenses/>.
|
| 21 |
|
| 22 |
from twisted.trial import unittest |
| 23 |
from scenic.ports import PortsAllocator |
| 24 |
from scenic.ports import PortsAllocatorError |
| 25 |
|
| 26 |
class Test_01_Ports_Allocator(unittest.TestCase): |
| 27 |
def _tst(self, expected, value): |
| 28 |
if value != expected:
|
| 29 |
self.fail("Expected value %s but got %s." % (expected, value)) |
| 30 |
|
| 31 |
def test_01_add_remove(self): |
| 32 |
a = PortsAllocator(minimum=2000, increment=2, maximum=2010) |
| 33 |
|
| 34 |
# value = 2000; value < 2012; value += 2
|
| 35 |
for value in xrange(2000, 2012, 2): |
| 36 |
self._tst(value, a.allocate())
|
| 37 |
try:
|
| 38 |
value = a.allocate() |
| 39 |
except PortsAllocatorError, e:
|
| 40 |
pass
|
| 41 |
else:
|
| 42 |
self.fail("Ports allocator should have overflown. Got value %d." % (value)) |
| 43 |
# value = 2000; value < 2012; value += 2
|
| 44 |
for value in xrange(2000, 2012, 2): |
| 45 |
a.free(value) |
| 46 |
try:
|
| 47 |
a.free(100)
|
| 48 |
except PortsAllocatorError, e:
|
| 49 |
pass
|
| 50 |
else:
|
| 51 |
self.fail("Trying to free value %d should have raised an error." % (100)) |
| 52 |
|
| 53 |
def test_02_add_many(self): |
| 54 |
a = PortsAllocator(minimum=2000, increment=2, maximum=2010) |
| 55 |
values = a.allocate_many(6)
|
| 56 |
a.free_many(values) |
| 57 |
values = a.allocate_many(3)
|
| 58 |
values = a.allocate_many(3)
|
| 59 |
try:
|
| 60 |
value = a.allocate() |
| 61 |
except PortsAllocatorError, e:
|
| 62 |
pass
|
| 63 |
else:
|
| 64 |
self.fail("Ports allocator should have overflown. Got value %d." % (value)) |
| 65 |
pass
|
| 66 |
|
