Comments (4)
imo this is out of scope and it's better to just add redis + saq to your unit tests
the alternative would be you'd have to basically implement redis
i'm not sure what the state of this is yet https://pypi.org/project/fakeredis/
but you could look into it
from saq.
I am specifically talking of a lightweight test double, not something comprehensive.
e.g:
- confirming that a task got queued
- running a task
The latter is easier as you can just call it, but generating a valid ctx
is something that, if needed, can be done transparently.
The prior really just needs to replace the Queue.enque()
method.
It's not for testing that saq itself works right, so assumption is that retries, delayed execution all works just fine.
It could probably be done with no replacement of redis itself.
from saq.
is using unittest mock not enough?
from saq.
It's hard, I need the job object back when queueing so basically I need to implement all the logic for that in the mocks.
I have a small prototype:
@patch('magneto.config.Config.queue', new=TestQueue())
class TestSaq(IsolatedAsyncioTestCase):
async def test_queue(self):
job = await queue.enqueue('add', val1=3, val2=5, timeout=10)
self.assertIsInstance(job, Job)
self.assertEqual(job.function, 'add')
self.assertEqual(job.timeout, 10)
self.assertEqual(job.status, Status.QUEUED)
self.assertEqual(job.kwargs, {'val1': 3, 'val2': 5})
And a simple double:
class TestQueue(Queue):
def __init__(self) -> None:
super().__init__(redis=AsyncMock(spec=Redis))
async def enqueue(self, job_or_func: str | Job, **kwargs: t.Any) -> Job:
job_kwargs: dict[str, t.Any] = {}
for k, v in kwargs.items():
if k in Job.__dataclass_fields__: # pylint: disable=no-member
job_kwargs[k] = v
else:
job_kwargs.setdefault("kwargs", {})[k] = v
if isinstance(job_or_func, str):
job = Job(function=job_or_func, **job_kwargs)
else:
job = job_or_func
for k, v in job_kwargs.items():
setattr(job, k, v)
job.queue = self
job.queued = now()
job.status = Status.QUEUED
return job
If there is some small refactoring in the saq.queue.Queue
class that TestQueue
can be made significantly smaller.
If this is expanded to also make calling tasks a little easier (e.g. generating a valid ctx), and handle queue.apply()
/queue.map()
it would probably suit the needs of 80% of users.
from saq.
Related Issues (20)
- duplicate jobs displayed in monitor
- Beginners ask for help HOT 1
- May I ask the experts how the 【swept】 error is caused? How to Troubleshooting? HOT 1
- cronjob is not respecting current timezone HOT 1
- cron jobs running at millisecond interval in tests HOT 2
- Running an action after a task fails and there are no more retries left HOT 1
- Please release version 0.12.3 HOT 1
- Running multiple workers HOT 9
- cron job updating HOT 6
- Windows Support HOT 2
- SAQ clients hang if one of workers is dead HOT 9
- "RuntimeError: read() called while another coroutine is already waiting for incoming data" while asyncio.gather job.refresh HOT 4
- For TERMINAL_STATUSES call after_process hook after the job status is updated HOT 1
- Random sweeping of jobs HOT 6
- 404 not found (task in web view) HOT 3
- Make repository and project itself more friendly for future contributions HOT 2
- Support dependencies among jobs HOT 2
- Add a log config when starting SAQ HOT 1
- not working in windows due to asyncio has no add_signal_handler HOT 1
- Cronjob management Runtime support HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from saq.