nidaqlib.testing¶
nidaqlib.testing ¶
Testing utilities — :class:FakeDaqBackend re-export.
The fake backend lives at :mod:nidaqlib.backend.fake so internal module
graphs stay clean; this module is the user-facing surface mentioned in
the design doc's §28 v0.1 import list.
FakeDaqBackend ¶
FakeDaqBackend(
*,
blocks=None,
read_block_default_shape=None,
read_errors=None,
write_errors=None,
)
In-memory test double for :class:~nidaqlib.backend.base.DaqBackend.
Capabilities:
- Scripted block reads, keyed by task name and consumed FIFO.
- Optional deterministic ramp generation when no script is provided.
- Scripted timeouts / errors injected by the test.
- An operation log (:attr:
operations) for asserting the §11.3.2 shutdown ordering. - A driver-thread simulator (:meth:
simulate_callbacks) that fires the registered every-N-samples callback on a privatethreading.Thread, matching the threading model of NI's real callback.
Configure the fake backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blocks
|
dict[str, Sequence[ndarray]] | None
|
Per-task-name sequence of pre-built |
None
|
read_block_default_shape
|
tuple[int, int] | None
|
|
None
|
read_errors
|
dict[str, Iterable[Exception]] | None
|
Per-task-name iterable of exceptions to raise from
:meth: |
None
|
write_errors
|
dict[str, Iterable[Exception]] | None
|
Per-task-name iterable of exceptions to raise from
:meth: |
None
|
Source code in src/nidaqlib/backend/fake.py
operations
instance-attribute
¶
Append-only log of backend calls. Tests assert ordering against this.
add_channel ¶
Append spec to task.channels.
close_task ¶
configure_logging ¶
Record logging on task.
configure_timing ¶
Record timing on task.
configure_trigger ¶
Record trigger on task for test inspection.
Source code in src/nidaqlib/backend/fake.py
create_task ¶
Create and return a new :class:_FakeTask.
Raises:
| Type | Description |
|---|---|
NIDaqBackendError
|
A task with |
Source code in src/nidaqlib/backend/fake.py
device_info ¶
Return scripted DeviceInfo for device if registered, else None.
Tests register product types via :meth:register_device_info so the
manager's module-level preflight can be exercised against the fake.
Default behaviour (no registration) returns None, matching the
Protocol's "unknown device" semantics.
Source code in src/nidaqlib/backend/fake.py
read_block ¶
Pop the next scripted block, fall back to a deterministic ramp.
Raises:
| Type | Description |
|---|---|
NIDaqTimeoutError
|
A scripted timeout exception was queued. |
NIDaqReadError
|
A scripted read exception was queued, or the
queue is empty and no |
Source code in src/nidaqlib/backend/fake.py
register_device_info ¶
Scripted DeviceInfo for tests of the manager's module-level preflight.
Source code in src/nidaqlib/backend/fake.py
register_every_n_samples ¶
Stash callback on the task. Returns task as the handle.
Mirrors NI's ordering invariant: registration must precede
task.start(). Real NI rejects post-start registration with
-200960 ("Register all your DAQmx software events prior to starting
the task"); the fake raises an analogous
:class:NIDaqBackendError so the unit suite catches violations
that the hardware would otherwise surface only at integration.
Raises:
| Type | Description |
|---|---|
NIDaqBackendError
|
A callback is already registered, or the task has already been started. |
Source code in src/nidaqlib/backend/fake.py
simulate_callbacks ¶
Fire the registered callback firings times from a worker thread.
Models the behaviour of NI's DAQmx driver thread so the §11.3.2
bridge can be exercised end-to-end in unit tests. The callback runs
on a fresh threading.Thread (NOT the asyncio event loop).
The simulator stops early if the callback is unregistered between
firings — this models NI's "any pending events are discarded" note
on stop_task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
_FakeTask
|
The fake task on which the callback was registered. |
required |
firings
|
int
|
Number of times to invoke the callback. |
required |
cadence_s
|
float
|
Optional sleep between firings, in seconds. Use 0 for a tight burst, > 0 to mimic a finite sample-clock cadence. |
0.0
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
Thread
|
class: |
Thread
|
usually do not need to |
|
Thread
|
on the recorder's own shutdown to drain pending chunks. Joinable |
|
Thread
|
if asserting on thread liveness. |
Source code in src/nidaqlib/backend/fake.py
start_task ¶
stop_task ¶
unregister_every_n_samples ¶
Clear the buffer-event callback on task.
Mirrors NI's ordering invariant: unregistration requires the task
to be stopped. Real NI rejects post-running unregister with -200986
("DAQmx software event cannot be unregistered because the task is
running"); the fake raises an analogous
:class:NIDaqBackendError so the unit suite catches violations
that real hardware would otherwise surface only at integration.
Raises:
| Type | Description |
|---|---|
NIDaqBackendError
|
The task is still running. |
Source code in src/nidaqlib/backend/fake.py
write ¶
Record one write — for tests asserting on outputs.
Validation parity with the real backend: missing channel keys raise
:class:NIDaqConfigurationError; scripted errors raise from the
per-task write_errors queue.
Raises:
| Type | Description |
|---|---|
NIDaqConfigurationError
|
|
NIDaqWriteError / NIDaqTimeoutError
|
A scripted error was queued. |