Skip to content

Commit 2fdaf97

Browse files
committed
Add RingBuffer.reset() method
Signed-off-by: Elijah Shaw-Rutschman <elijahr@gmail.com>
1 parent d25635d commit 2fdaf97

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,8 @@ Pull requests are welcome, please file any issues you encounter.
142142

143143
## Changelog
144144

145+
### v2.4.0 2020-03-23
146+
* Added `RingBuffer.reset()` method for .
147+
145148
### v2.3.0 2020-03-22
146-
* Added `concatenate` function for joining multiple Cython arrays.
149+
* Added `concatenate` function for joining multiple arbitrary Python objects that support the buffer protocol.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def get_ext_modules():
128128

129129
setup(
130130
name='ringbuf',
131-
version='2.3.0',
131+
version='2.4.0',
132132
description='A lock-free ring buffer for Python and Cython.',
133133
long_description=get_readme(),
134134
long_description_content_type="text/markdown",

src/ringbuf/boost.pxd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ cdef extern from '<boost/lockfree/spsc_queue.hpp>' namespace 'boost::lockfree' n
2828
# get write space to write elements
2929
size_t write_available() const
3030

31+
# return true, if implementation is lock-free
3132
libcpp.bool is_lock_free()
3233

34+
# reset the ringbuffer
35+
void reset()
36+
3337

3438
cdef extern from *:
3539
spsc_queue[char]* void_ptr_to_spsc_queue_char_ptr 'static_cast<boost::lockfree::spsc_queue<char>*>' (void*) nogil except NULL

src/ringbuf/ringbuf.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ cdef class RingBuffer:
114114
def is_lock_free(self) -> bool:
115115
return self.queue.is_lock_free()
116116

117+
def reset(self) -> None:
118+
self.queue.reset()
119+
117120
cdef void* queue_void_ptr(self):
118121
return spsc_queue_char_ptr_to_void_ptr(self.queue)
119122

test.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ def expected(request):
3838
return request.param
3939

4040

41-
def test_push_pop(expected):
41+
def test_ringbuffer_push_pop(expected):
4242
capacity = expected.size
4343

4444
buffer = RingBuffer(format=expected.dtype.char, capacity=capacity)
45+
assert buffer.capacity == capacity
4546
assert buffer.is_lock_free
4647

4748
assert buffer.read_available == 0
@@ -93,21 +94,28 @@ def test_push_pop(expected):
9394
expected)
9495

9596

96-
def test_underflow():
97+
def test_ringbuffer_underflow():
9798
buffer = RingBuffer(format='b', capacity=1)
9899
assert buffer.pop(1) is None
99100

100101

101-
def test_overflow():
102+
def test_ringbuffer_overflow():
102103
buffer = RingBuffer(format='B', capacity=7)
103104
remaining = buffer.push(b'spam')
104105
assert remaining is None
105106
remaining = buffer.push(b'eggs')
106107
assert bytes(remaining) == b's'
107-
assert bytes(buffer.pop(buffer.capacity)) == b'spamegg'
108+
assert bytes(buffer.pop(7)) == b'spamegg'
108109

109110

110-
def test_invalid():
111+
def test_ringbuffer_reset():
112+
buffer = RingBuffer(format='B', capacity=5)
113+
buffer.push(b'hello')
114+
buffer.reset()
115+
assert buffer.pop(5) is None
116+
117+
118+
def test_ringbuffer_invalid():
111119
with pytest.raises(ValueError):
112120
RingBuffer(format='B', capacity=0)
113121

0 commit comments

Comments
 (0)