Skip to content

Commit

Permalink
Clean up tests and add coverage reporting through coveralls (#38)
Browse files Browse the repository at this point in the history
* Add coverage config to travis

* Correct pip argrument error

* Allow test in helpers.py to be discovered

* Add coverage badge (linked to temporary branch)

* Add back missing coverage packages for travis

* Separate all test code (except for 'DummyRtlSdr') outside of the library code

* Add check for travis env in sdr_cls fixture

* Add fake librtlsdr to emulate the low-level functions
Not yet implemented, needs to be monkeypatched in test fixtures

* Use pytest.monkeypatch to override librtlsdr functions

Removes the need to override base classes at the module level

* Raise 'NotImplementedError' on async reads in tcp module

* Clean up tcp test and handle expected exceptions

* Add forgotten import

* Move test from helpers module into tests directory

* Make sure async module passes the generic test

* Generate non-random data so it can be verified in tests

* Test sampled data against generated data

* Add PEP238 true division

* Add support for direct sampling modes in tests

* Add missing method on tcp module to support direct sampling tests

* Ensure container type can be compared

* Use str parameters for direct_sampling params in tests

* Begin adding assertion tests for exceptions

* Parameterize tests to run with and without numpy

* Update pytest versions in travis env

* Make sure device is closed for each test run

* Import base RtlSdr class directly
In Py>=3.5 it gets overridden by rtlsdraio and causes issues with tests

* Avoid parameterized tests with asyncio

* Change coverage badge to point to main repo
  • Loading branch information
nocarryr committed Apr 9, 2016
1 parent 7885001 commit 785e2ee
Show file tree
Hide file tree
Showing 17 changed files with 434 additions and 400 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
@@ -0,0 +1,2 @@
[run]
source = rtlsdr/*
5 changes: 4 additions & 1 deletion .travis.yml
Expand Up @@ -16,9 +16,12 @@ before_install:
- cd ../..
- export LD_LIBRARY_PATH=$HOME/.local:$LD_LIBRARY_PATH
install:
- pip install -U pytest pytest-cov coveralls
- pip install -e .
script:
- py.test
- py.test --cov-config .coveragerc --cov=rtlsdr
after_success:
- coveralls
deploy:
provider: pypi
distributions: "sdist bdist_wheel"
Expand Down
2 changes: 1 addition & 1 deletion README.md
@@ -1,4 +1,4 @@
[![Build Status](https://travis-ci.org/roger-/pyrtlsdr.svg?branch=master)](https://travis-ci.org/roger-/pyrtlsdr)
[![Build Status](https://travis-ci.org/roger-/pyrtlsdr.svg?branch=master)](https://travis-ci.org/roger-/pyrtlsdr)[![Coverage Status](https://coveralls.io/repos/github/roger-/pyrtlsdr/badge.svg?branch=master)](https://coveralls.io/github/roger-/pyrtlsdr?branch=master)

# Description

Expand Down
9 changes: 0 additions & 9 deletions conftest.py

This file was deleted.

2 changes: 1 addition & 1 deletion pytest.ini
@@ -1,3 +1,3 @@
[pytest]
testpaths = rtlsdr
testpaths = tests
python_files = *.py
28 changes: 0 additions & 28 deletions rtlsdr/helpers.py
Expand Up @@ -65,31 +65,3 @@ def wrapper(buffer, rtlsdr_obj):

return wrapper
return decorator


def main():

@limit_time(0.01)
@limit_calls(20)
def read_callback(buffer, rtlsdr_obj):
print('In callback')
print(' signal mean:', sum(buffer)/len(buffer))

from rtlsdr import RtlSdr

sdr = RtlSdr()

print('Configuring SDR...')
sdr.rs = 1e6
sdr.fc = 70e6
sdr.gain = 5
print(' sample rate: %0.6f MHz' % (sdr.rs/1e6))
print(' center ferquency %0.6f MHz' % (sdr.fc/1e6))
print(' gain: %d dB' % sdr.gain)

print('Testing callback...')
sdr.read_samples_async(read_callback)


if __name__ == '__main__':
main()
30 changes: 0 additions & 30 deletions rtlsdr/rtlsdr.py
Expand Up @@ -442,33 +442,3 @@ def cancel_read_async(self):
% (result))

self.read_async_canceling = True


def test():
try:
from testutils import build_test_sdr, async_read_test
except ImportError:
from .testutils import build_test_sdr, async_read_test

sdr = build_test_sdr(RtlSdr)

print('Configuring SDR...')
sdr.rs = 2.4e6
sdr.fc = 70e6
sdr.gain = 4
print(' sample rate: %0.6f MHz' % (sdr.rs/1e6))
print(' center frequency %0.6f MHz' % (sdr.fc/1e6))
print(' gain: %d dB' % sdr.gain)

print('Reading samples...')
samples = sdr.read_samples(1024)
print(' signal mean:', sum(samples)/len(samples))

print('Testing callback...')
async_read_test(sdr)

sdr.close()


if __name__ == '__main__':
test()
53 changes: 1 addition & 52 deletions rtlsdr/rtlsdraio.py
@@ -1,16 +1,9 @@
import logging
import asyncio
try:
from rtlsdr import RtlSdr as _RtlSdr
from testutils import is_travisci, DummyRtlSdr
from rtlsdr import RtlSdr
except ImportError:
from .rtlsdr import RtlSdr as _RtlSdr
from .testutils import is_travisci, DummyRtlSdr

if is_travisci():
RtlSdr = DummyRtlSdr
else:
RtlSdr = _RtlSdr


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -108,47 +101,3 @@ def stream(self, num_samples_or_bytes=DEFAULT_READ_SIZE, format='samples', loop=
def stop(self):
''' Stop async stream. '''
self.async_iter.stop()


async def main():
import math

sdr = RtlSdrAio()

print('Configuring SDR...')
sdr.rs = 2.4e6
sdr.fc = 100e6
sdr.gain = 10
print(' sample rate: %0.6f MHz' % (sdr.rs/1e6))
print(' center frequency %0.6f MHz' % (sdr.fc/1e6))
print(' gain: %d dB' % sdr.gain)

print('Streaming samples...')

i = 0
async for samples in sdr.stream():
power = sum(abs(s)**2 for s in samples) / len(samples)
print('Relative power:', 10*math.log10(power), 'dB')

i += 1

if i > 20:
sdr.stop()
break

print('Done')

sdr.close()

async def do_nothing():
for i in range(50):
await asyncio.sleep(0.1)
print('#')


def test():
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait([main(), do_nothing()]))

if __name__ == '__main__':
test()
64 changes: 8 additions & 56 deletions rtlsdr/rtlsdrtcp.py
Expand Up @@ -18,16 +18,10 @@
from socketserver import TCPServer, BaseRequestHandler

try:
from rtlsdr import RtlSdr as _RtlSdr
from testutils import is_travisci, DummyRtlSdr
from rtlsdr import RtlSdr
except ImportError:
from .rtlsdr import RtlSdr as _RtlSdr
from .testutils import is_travisci, DummyRtlSdr
from .rtlsdr import RtlSdr

if is_travisci():
RtlSdr = DummyRtlSdr
else:
RtlSdr = _RtlSdr

MAX_BUFFER_SIZE = 4096
RECEIVE_TIMEOUT = 20
Expand Down Expand Up @@ -546,11 +540,17 @@ def get_tuner_type(self):
def set_direct_sampling(self, value):
self._communicate_method('set_direct_sampling', value)

def read_bytes(self, num_bytes=RtlSdr.DEFAULT_READ_SIZE):
return self._communicate_method('read_bytes', num_bytes)

def read_samples(self, num_samples=RtlSdr.DEFAULT_READ_SIZE):
raw_data = self._communicate_method('read_samples', num_samples)
iq = self.packed_bytes_to_iq(raw_data)
return iq

def read_bytes_async(self, *args):
raise NotImplementedError('Async read not available in TCP mode')

center_freq = fc = property(get_center_freq, set_center_freq)
sample_rate = rs = property(get_sample_rate, set_sample_rate)
gain = property(get_gain, set_gain)
Expand Down Expand Up @@ -584,53 +584,5 @@ def run_server():
server = RtlSdrTcpServer(**o)
server.run_forever()


def test():
import time
port = 1235
while True:
try:
server = RtlSdrTcpServer(port=port)
server.run()
except socket.error as e:
if e.errno != errno.EADDRINUSE:
raise
server = None
port += 1
if server is not None:
break
client = RtlSdrTcpClient(port=port)
test_props = [
['sample_rate', 2e6],
['center_freq', 6e6],
['gain', 10.],
['freq_correction', 20]
]
try:
gains = client.get_gains()
gains = [gain / 10. for gain in gains]
print('gains: ', gains)
for prop_name, set_value in test_props:
if prop_name != 'gain':
value = getattr(client, prop_name)
print('%s initial value: %s' % (prop_name, value))
else:
set_value = gains[1]
setattr(client, prop_name, set_value)
value = getattr(client, prop_name)
print('%s set to %s, real value: %s' % (prop_name, set_value, value))
assert int(value) == int(set_value)
time.sleep(.2)
tuner_type = client.get_tuner_type()
print('tuner_type: ', tuner_type)
for num_samples in [1024, 4096, 16384, 65536, 131072]:
print('Reading %s samples...' % (num_samples))
samples = client.read_samples(num_samples)
print('%s samples received' % (len(samples)))
assert len(samples) == num_samples
finally:
server.close()
print('Complete')

if __name__ == '__main__':
run_server()

0 comments on commit 785e2ee

Please sign in to comment.