-
-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(tests): Add automated test for recording functionality #803
Open
onyedikachi-david
wants to merge
13
commits into
OpenAdaptAI:main
Choose a base branch
from
onyedikachi-david:feature/add-automated-test-for-recording
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
99bf1a4
feat(tests): Add automated test for recording functionality
onyedikachi-david e5880bd
Merge branch 'main' into feature/add-automated-test-for-recording
onyedikachi-david c0fcd22
fix(recording): Improve logging and error handling for recording process
onyedikachi-david 556fb5a
Merge branch 'main' into feature/add-automated-test-for-recording
onyedikachi-david ff5a436
use const for record started timeout
onyedikachi-david 3b774e9
use const for record started timeout
onyedikachi-david 52ca7c1
use const for record started timeout
onyedikachi-david 818b2ab
removed unnecessary timeout
onyedikachi-david 6ebbb73
Merge branch 'main' into feature/add-automated-test-for-recording
onyedikachi-david bbec189
fixed: now works after some refactor
onyedikachi-david 33b9ceb
chore: increase record timeout and added assert
onyedikachi-david 160d4fc
chore: comment on increased timeout
onyedikachi-david 03ac124
removed unused DB setup function.
onyedikachi-david File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,4 @@ src | |
|
||
dist/ | ||
build/ | ||
openadapt/error.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
"""Module for testing the recording module.""" | ||
|
||
import multiprocessing | ||
import time | ||
import os | ||
import pytest | ||
from openadapt import record, playback, utils, video | ||
from openadapt.config import config | ||
from openadapt.db import crud | ||
from openadapt.models import Recording, ActionEvent | ||
from loguru import logger | ||
|
||
RECORD_STARTED_TIMEOUT = 360 # Increased timeout to 6 minutes | ||
|
||
|
||
@pytest.fixture | ||
def setup_db(): | ||
# Setup the database connection and return the session | ||
db_session = crud.get_new_session(read_and_write=True) | ||
yield db_session | ||
db_session.close() | ||
|
||
|
||
def test_record_functionality(): | ||
logger.info("Starting test_record_functionality") | ||
|
||
# Set up multiprocessing communication | ||
parent_conn, child_conn = multiprocessing.Pipe() | ||
|
||
# Set up termination events | ||
terminate_processing = multiprocessing.Event() | ||
terminate_recording = multiprocessing.Event() | ||
|
||
# Start the recording process | ||
record_process = multiprocessing.Process( | ||
target=record.record, | ||
args=( | ||
"Test recording", | ||
terminate_processing, | ||
terminate_recording, | ||
child_conn, | ||
False, | ||
), | ||
) | ||
|
||
try: | ||
record_process.start() | ||
logger.info("Recording process started") | ||
|
||
# Wait for the 'record.started' signal | ||
start_time = time.time() | ||
while time.time() - start_time < RECORD_STARTED_TIMEOUT: | ||
if parent_conn.poll(1): # 1 second timeout for poll | ||
message = parent_conn.recv() | ||
logger.info(f"Received message: {message}") | ||
if message["type"] == "record.started": | ||
logger.info("Received 'record.started' signal") | ||
break | ||
else: | ||
logger.debug("No message received, continuing to wait...") | ||
else: | ||
logger.error("Timed out waiting for 'record.started' signal") | ||
pytest.fail("Timed out waiting for 'record.started' signal") | ||
|
||
# Wait a short time to ensure some data is recorded | ||
time.sleep(5) | ||
|
||
logger.info("Stopping the recording") | ||
terminate_processing.set() # Signal the recording to stop | ||
|
||
# Wait for the recording to stop | ||
logger.info("Waiting for recording to stop") | ||
terminate_recording.wait(timeout=RECORD_STARTED_TIMEOUT) | ||
if not terminate_recording.is_set(): | ||
logger.error("Recording did not stop within the expected time") | ||
pytest.fail("Recording did not stop within the expected time") | ||
|
||
logger.info("Recording stopped successfully") | ||
|
||
# Assert database state | ||
with crud.get_new_session(read_and_write=True) as session: | ||
recording = session.query(Recording).order_by(Recording.id.desc()).first() | ||
assert recording is not None, "No recording was created in the database" | ||
assert recording.task_description == "Test recording" | ||
logger.info("Database assertions passed") | ||
|
||
# Assert filesystem state | ||
video_path = video.get_video_file_path(recording.timestamp) | ||
if config.RECORD_VIDEO: | ||
assert os.path.exists(video_path), f"Video file not found at {video_path}" | ||
logger.info(f"Video file found at {video_path}") | ||
else: | ||
logger.info("Video recording is disabled in the configuration") | ||
|
||
performance_plot_path = utils.get_performance_plot_file_path(recording.timestamp) | ||
assert os.path.exists(performance_plot_path), f"Performance plot not found at {performance_plot_path}" | ||
logger.info(f"Performance plot found at {performance_plot_path}") | ||
|
||
# Assert that at least one action event was recorded | ||
with crud.get_new_session(read_and_write=True) as session: | ||
action_events = crud.get_action_events(session, recording) | ||
assert len(action_events) > 0, "No action events were recorded" | ||
logger.info(f"Number of action events recorded: {len(action_events)}") | ||
|
||
logger.info("All assertions passed") | ||
|
||
except Exception as e: | ||
logger.exception(f"An error occurred during the test: {e}") | ||
raise | ||
|
||
finally: | ||
# Clean up the recording process | ||
if record_process.is_alive(): | ||
logger.info("Terminating recording process") | ||
record_process.terminate() | ||
record_process.join() | ||
logger.info("Test completed") | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main([__file__]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This appears to be unused. If so, can you please remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done