def test_push(self):
        '''
        Test that buffer push works
        '''
        random_size = random.randrange(1, 100)
        buffer = RingBuffer(random_size)

        buffer.push(0)
    def test_pop(self):
        '''
        Test that buffer pop returns pushed value
        '''
        random_size = random.randrange(1, 100)
        buffer = RingBuffer(random_size)

        buffer.push(0)
        returned_element = buffer.pop()

        self.assertEqual(returned_element, 0)
Ejemplo n.º 3
0
def test_reader_reads_data_and_moves_offset(buffer):
    read_buff = RingBuffer(name=buffer.name, size=0, create=False)

    messages = _messages(3)
    expected = [_decode_message(m) for m in messages]
    for m in messages:
        buffer.put(m)

    got = [_decode_message(read_buff.get()) for _ in range(3)]
    assert got == expected

    assert read_buff.reader_pos() == 48
    def test_get_capacity(self):
        '''
        Test that get_capacity method
        returns correct values
        '''
        random_size = random.randrange(1, 100)
        buffer = RingBuffer(random_size)
        for i in range(0, random.randrange(1, random_size)):
            buffer.push(i)

        buffer_capacity = buffer.get_capacity()
        self.assertEqual(buffer_capacity, random_size)
    def test_pop_empty_fail(self):
        '''
        Test that popping from an empty buffer
        results in RuntimeException being raised
        along with "Buffer empty" exception message
        '''
        with pytest.raises(RuntimeError) as excinfo:
            random_size = random.randrange(1, 100)
            buffer = RingBuffer(random_size)
            buffer.pop()

        self.assertEqual(str(excinfo.value), 'Buffer is empty!')
Ejemplo n.º 6
0
def test_reader_does_not_go_past_writer(buffer):
    read_buff = RingBuffer(name=buffer.name, size=0, create=False)

    messages = _messages(3)
    for m in messages:
        buffer.put(m)

    with pytest.raises(WriterCollisionError) as excinfo:
        [read_buff.get() for _ in range(5)]

    assert str(
        excinfo.value
    ) == "Writer cursor at 48. Reader cursor attemping to read [48 :64]"
    def test_push_full_fail(self):
        '''
        Test that pushing into a full
        buffer raises Runtime Exception
        and the "buffer full" message
        '''
        with pytest.raises(RuntimeError) as excinfo:
            random_size = random.randrange(1, 100)
            buffer = RingBuffer(random_size)
            for i in range(0, random_size + 1):
                buffer.push(i)

        self.assertEqual(str(excinfo.value), 'Buffer is full!')
Ejemplo n.º 8
0
def run_producer(name, size, delay):
    buffer = RingBuffer(name=name, size=size, create=True)
    shutdown = False

    def terminate(signum, frame):
        nonlocal shutdown
        print(
            "Received=",
            signal.Signals(signum).name,
            "Attempting graceful shutdown of producer",
        )
        shutdown = True

    signal.signal(signal.SIGTERM, terminate)
    signal.signal(signal.SIGINT, terminate)

    try:
        while not shutdown:
            buffer.put(get_next_message())
            print(f"Reader: {buffer.reader_pos()} Writer: {buffer.writer_pos()}")
            time.sleep(delay)

        buffer.destroy()
        print(f"Destroyed shared memory buffer {name}")

    except Exception as ex:
        buffer.destroy()
        print(f"Error {str(ex)}. Destroying shared memory buffer and exiting")
        traceback.print_exc(file=sys.stdout)
        sys.exit(1)
    def test_clear(self):
        '''
        Test that clear() method clears
        the buffer properly
        '''

        random_size = random.randrange(1, 100)
        buffer = RingBuffer(random_size)
        for i in range(0, random.randrange(1, random_size)):
            buffer.push(i)

        buffer.clear()
        self.assertTrue(buffer.is_empty())
        self.assertFalse(buffer.is_full())
        self.assertEqual(buffer.get_capacity(), random_size)
        self.assertEqual(buffer.get_buffer_fill(), 0)
Ejemplo n.º 10
0
def test_reader_reads_data_and_moves_offset_when_writer_wraps_around(buffer):
    read_buff = RingBuffer(name=buffer.name, size=0, create=False)

    messages = _messages(8)
    for m in messages[:6]:
        buffer.put(m)

    [read_buff.get() for _ in range(6)]

    for m in messages[6:]:
        buffer.put(m)

    got = [_decode_message(read_buff.get()) for _ in range(2)]
    expected = [_decode_message(m) for m in messages[6:]]
    assert got == expected

    assert read_buff.reader_pos() == 32
Ejemplo n.º 11
0
def test_reader_does_not_go_past_writer_when_writer_wraps_around(buffer):
    read_buff = RingBuffer(name=buffer.name, size=0, create=False)

    messages = _messages(8)
    for m in messages[:6]:
        buffer.put(m)

    [read_buff.get() for _ in range(6)]

    for m in messages[6:7]:
        buffer.put(m)

    with pytest.raises(WriterCollisionError) as excinfo:
        [read_buff.get() for _ in range(2)]

    assert str(
        excinfo.value
    ) == "Writer cursor at 16. Reader cursor attemping to read [16 :32]"
Ejemplo n.º 12
0
def test_attach_with_existing_buffer(buffer):
    new_buff = RingBuffer(name=buffer.name, size=0, create=False)

    assert new_buff.data_buff.name == "test"
    assert len(new_buff.data_buff.buf) == 100

    assert new_buff.write_index_buff.name == "test-writer"
    assert len(new_buff.write_index_buff.buf) == WRITE_INDEX_BUFFER_SIZE

    assert new_buff.read_index_buff.name == "test-reader"
    assert len(new_buff.read_index_buff.buf) == READ_INDEX_BUFFER_SIZE
    def test_initialize_params(self):
        '''
        Test that buffer init params are returned
        correctly
        '''
        random_size = random.randrange(1, 100)
        buffer = RingBuffer(random_size)

        self.assertEqual(buffer.get_capacity(), random_size)
        self.assertFalse(buffer.is_full())
        self.assertTrue(buffer.is_empty())
        self.assertEqual(buffer.get_buffer_fill(), 0)
Ejemplo n.º 14
0
 def setUp(self):
     self.capacity = 5
     self.buffer = RingBuffer(self.capacity)
    def test_buffer_fill_size_return(self):
        '''
        Test that buffer fill size is
        correctly returned under various
        use scenarios
        '''

        # Test that buffer fill size is returned as
        # 0 when buffer is empty
        random_size = random.randrange(1, 100)
        buffer = RingBuffer(random_size)
        fill_size = buffer.get_buffer_fill()
        self.assertEqual(fill_size, 0)

        # Test that buffer fill size is returned as
        # the number of elements pushed when the
        # buffer is neither empty nor full
        random_size = random.randrange(1, 100)
        buffer = RingBuffer(random_size)
        for i in range(0, int(random_size / 2)):
            buffer.push(i)

        fill_size = buffer.get_buffer_fill()
        self.assertEqual(fill_size, int(random_size / 2))

        # Test that buffer fill size is correctly
        # returned when buffer is full
        random_size = random.randrange(1, 100)
        buffer = RingBuffer(random_size)
        for i in range(0, random_size):
            buffer.push(i)

        fill_size = buffer.get_buffer_fill()
        self.assertEqual(fill_size, random_size)
Ejemplo n.º 16
0
class RingBufferTests(unittest.TestCase):
    def setUp(self):
        self.capacity = 5
        self.buffer = RingBuffer(self.capacity)

    def test_new_buffer_has_appropriate_capacity(self):
        self.assertEqual(self.buffer.capacity, self.capacity)

    def test_adding_one_element_to_buffer(self):
        self.buffer.append('a')
        self.assertEqual(self.buffer.get(), ['a'])

    def test_filling_buffer_to_capacity(self):
        self.buffer.append('a')
        self.buffer.append('b')
        self.buffer.append('c')
        self.buffer.append('d')
        self.buffer.append('e')
        self.assertEqual(self.buffer.get(), ['a', 'b', 'c', 'd', 'e'])

    def test_adding_one_element_to_full_buffer(self):
        self.buffer.append('a')
        self.buffer.append('b')
        self.buffer.append('c')
        self.buffer.append('d')
        self.buffer.append('e')
        self.buffer.append('f')
        self.assertEqual(self.buffer.get(), ['f', 'b', 'c', 'd', 'e'])

    def test_adding_many_elements_to_full_buffer(self):
        self.buffer.append('a')
        self.buffer.append('b')
        self.buffer.append('c')
        self.buffer.append('d')
        self.buffer.append('e')
        self.buffer.append('f')
        self.buffer.append('g')
        self.buffer.append('h')
        self.buffer.append('i')
        self.assertEqual(self.buffer.get(), ['f', 'g', 'h', 'i', 'e'])

    def test_adding_50_elements_to_buffer(self):
        for i in range(50):
            self.buffer.append(i)

        self.assertEqual(self.buffer.get(), [45, 46, 47, 48, 49])
Ejemplo n.º 17
0
def create_buffer():
    buff = RingBuffer(name="test", size=100, create=True)

    yield buff

    buff.destroy()