-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cvlib: Update doWithTimeout to block recursive calling
Stop doWithTimeout from being able to be recursively called. Also reset alarm handler to previous default on timeout func completion Change-Id: I43dec86ef8ccaeca401e3a8cd051266ba3e871a2
- Loading branch information
1 parent
bf22782
commit 8fed532
Showing
2 changed files
with
51 additions
and
3 deletions.
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
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,31 @@ | ||
# Copyright (c) 2025 Arista Networks, Inc. | ||
# Use of this source code is governed by the Apache License 2.0 | ||
# that can be found in the COPYING file. | ||
|
||
import pytest | ||
|
||
from cloudvision.cvlib import Context, InvalidContextException, User | ||
|
||
|
||
def test_do_with_timeout(): | ||
ctx = Context(user=User("test_user", "123")) | ||
|
||
# Function that is ok to use in a doWithTimeout | ||
def okFunc(): | ||
print("ok") | ||
|
||
# Function that is not ok to use in a doWithTimeout | ||
def exceptionFunc(): | ||
ctx.doWithTimeout(okFunc, 5) | ||
|
||
# Run a function that will pass, though will have set up the | ||
# necessary alarms and then unset them | ||
ctx.doWithTimeout(okFunc, 5) | ||
|
||
# Run a function that will fail as it is recursively calling doWithTimeout | ||
with pytest.raises(InvalidContextException) as exc_info: | ||
ctx.doWithTimeout(exceptionFunc, 5) | ||
assert "Cannot recursively call doWithTimeout" in str(exc_info.value) | ||
|
||
# Run ok function once again to make sure that previous exception still unset the handler | ||
ctx.doWithTimeout(okFunc, 5) |