コード例 #1
0
    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)
コード例 #2
0
ファイル: runner.py プロジェクト: skabbass1/ring-buffer
def run_consumer(name, delay):
    buffer = RingBuffer(name=name, size=0, create=False)
    shutdown = False

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

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

    try:
        while not shutdown:
            b = buffer.get()
            print(f"Message: {decode_message(b)}Reader: {buffer.reader_pos()} Writer: {buffer.writer_pos()}")
            time.sleep(delay)

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

    except Exception as ex:
        buffer.close()
        print(f"Error {str(ex)}. Destroying shared memory buffer and exiting")
        traceback.print_exc(file=sys.stdout)
        sys.exit(1)
コード例 #3
0
    def test_push(self):
        '''
        Test that buffer push works
        '''
        random_size = random.randrange(1, 100)
        buffer = RingBuffer(random_size)

        buffer.push(0)
コード例 #4
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
コード例 #5
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)
コード例 #6
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
コード例 #7
0
    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)
コード例 #8
0
    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!')
コード例 #9
0
    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)
コード例 #10
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]"
コード例 #11
0
    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!')
コード例 #12
0
    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)
コード例 #13
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
コード例 #14
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]"
コード例 #15
0
 def setUp(self):
     self.capacity = 5
     self.buffer = RingBuffer(self.capacity)
コード例 #16
0
def create_buffer():
    buff = RingBuffer(name="test", size=100, create=True)

    yield buff

    buff.destroy()