Skip to content
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

Improve unit test temp file usage #696

Merged
merged 1 commit into from
Oct 5, 2023

Conversation

woodsp-ibm
Copy link
Member

@woodsp-ibm woodsp-ibm commented Oct 5, 2023

Summary

As it was two separate unit tests created/used the same named file in the temp directory. Running separately things are fine and are most often in CI - but CI runs tests in parallel and this has led to occasional failures for example in this log with the relevant excerpt as below

==============================
Failed 2 tests - output below:
==============================

test.algorithms.regressors.test_qsvr.TestQSVR.test_save_load
------------------------------------------------------------

Captured traceback:
~~~~~~~~~~~~~~~~~~~
    Traceback (most recent call last):

      File "/Users/runner/work/qiskit-machine-learning/qiskit-machine-learning/test/algorithms/regressors/test_qsvr.py", line 123, in test_save_load
    FakeModel.load(file_name)

      File "/Users/runner/work/qiskit-machine-learning/qiskit-machine-learning/qiskit_machine_learning/algorithms/serializable_model.py", line 54, in load
    model = dill.load(handler)
            ^^^^^^^^^^^^^^^^^^

      File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/dill/_dill.py", line 287, in load
    return Unpickler(file, ignore=ignore, **kwds).load()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

      File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/dill/_dill.py", line 442, in load
    obj = StockUnpickler.load(self)
          ^^^^^^^^^^^^^^^^^^^^^^^^^

    EOFError: Ran out of input


test.algorithms.regressors.test_fidelity_quantum_kernel_qsvr.TestQSVR.test_save_load
------------------------------------------------------------------------------------

Captured traceback:
~~~~~~~~~~~~~~~~~~~
    Traceback (most recent call last):

      File "/Users/runner/work/qiskit-machine-learning/qiskit-machine-learning/test/algorithms/regressors/test_fidelity_quantum_kernel_qsvr.py", line 112, in test_save_load
    regressor_load = QSVR.load(file_name)
                     ^^^^^^^^^^^^^^^^^^^^

      File "/Users/runner/work/qiskit-machine-learning/qiskit-machine-learning/qiskit_machine_learning/algorithms/serializable_model.py", line 53, in load
    with open(file_name, "rb") as handler:
         ^^^^^^^^^^^^^^^^^^^^^

    FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/qsvr.model'

    
During handling of the above exception, another exception occurred:


    Traceback (most recent call last):

      File "/Users/runner/work/qiskit-machine-learning/qiskit-machine-learning/test/algorithms/regressors/test_fidelity_quantum_kernel_qsvr.py", line 127, in test_save_load
    os.remove(file_name)

    FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/qsvr.model'

Details and comments

This changes from specific named temp file to creating a temp folder with that named file in it, same as is done in another similar test here

with tempfile.TemporaryDirectory() as dir_name:
where the temp folder is automatically removed, along with the contents, when the context ends.

This change should should eliminate that occasional random CI failures where the two tests are run at the same time and there was collision in the file used. By using a temp folder each test will have their own folder and any collision avoided from use of the same named file since they will be in different folders. This also allows multiple instances of the same test to be run in parallel, not that CI does this, but its a more general fix.

Copy link
Collaborator

@adekusar-drl adekusar-drl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking care of this!

@coveralls
Copy link

Pull Request Test Coverage Report for Build 6421477684

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 92.689%

Totals Coverage Status
Change from base Build 6419328274: 0.0%
Covered Lines: 1851
Relevant Lines: 1997

💛 - Coveralls

@mergify mergify bot merged commit fe0a8f3 into qiskit-community:main Oct 5, 2023
15 checks passed
@woodsp-ibm woodsp-ibm deleted the improve_tmp branch October 5, 2023 17:24
oscar-wallis pushed a commit that referenced this pull request Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants