Skip to content

Commit

Permalink
test(python): move Python's Job.getChildrenValues tests to flow_tests.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Arvinje committed Oct 23, 2024
1 parent a3c63ba commit 7fa3ba7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 72 deletions.
70 changes: 70 additions & 0 deletions python/tests/flow_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,75 @@ async def process2(job: Job, token: str):
await parent_queue.obliterate()
await parent_queue.close()

async def test_get_children_values(self):
child_job_name = 'child-job'
children_data = [
{"bar": None},
{"baz": 12.93},
{"qux": "string value"}
]
parent_queue_name = f"__test_parent_queue__{uuid4().hex}"

processing_children = Future()

processed_children = 0
async def process1(job: Job, token: str):
nonlocal processed_children
processed_children+=1
if processed_children == len(children_data):
processing_children.set_result(None)
return children_data[job.data.get("idx")]

processing_parent = Future()

async def process2(job: Job, token: str):
children_values = await job.getChildrenValues()
processing_parent.set_result(children_values)
return 1

parent_worker = Worker(parent_queue_name, process2)
children_worker = Worker(queue_name, process1)

flow = FlowProducer()
await flow.add(
{
"name": 'parent-job',
"queueName": parent_queue_name,
"data": {},
"children": [
{"name": child_job_name, "data": {"idx": 0, "foo": 'bar'}, "queueName": queue_name},
{"name": child_job_name, "data": {"idx": 1, "foo": 'baz'}, "queueName": queue_name},
{"name": child_job_name, "data": {"idx": 2, "foo": 'qux'}, "queueName": queue_name}
]
}
)

await processing_children
await processing_parent

def on_parent_processed(future):
self.assertIn(children_data[0], future.result().values())
self.assertIn(children_data[1], future.result().values())
self.assertIn(children_data[2], future.result().values())

processing_parent.add_done_callback(on_parent_processed)

await parent_worker.close()
await children_worker.close()
await flow.close()

parent_queue = Queue(parent_queue_name)
await parent_queue.pause()
await parent_queue.obliterate()
await parent_queue.close()

async def test_get_children_values_on_simple_jobs(self):
queue = Queue(queue_name)
job = await queue.add("test", {"foo": "bar"}, {"delay": 1500})
children_values = await job.getChildrenValues()
self.assertEqual(children_values, {})

await queue.close()

if __name__ == '__main__':
unittest.main()
73 changes: 1 addition & 72 deletions python/tests/job_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

import unittest

from asyncio import Future
from bullmq import Queue, Job, Worker, FlowProducer
from bullmq import Queue, Job
from uuid import uuid4

queueName = f"__test_queue__{uuid4().hex}"
Expand Down Expand Up @@ -104,75 +103,5 @@ async def test_promote_delayed_job(self):

await queue.close()

async def test_get_children_values(self):
child_job_name = 'child-job'
children_data = [
{"bar": None},
{"baz": 12.93},
{"qux": "string value"}
]
parent_queue_name = f"__test_parent_queue__{uuid4().hex}"

processing_children = Future()

processed_children = 0
async def process1(job: Job, token: str):
nonlocal processed_children
processed_children+=1
if processed_children == len(children_data):
processing_children.set_result(None)
return children_data[job.data.get("idx")]

processing_parent = Future()

async def process2(job: Job, token: str):
children_values = await job.getChildrenValues()
processing_parent.set_result(children_values)
return 1

parent_worker = Worker(parent_queue_name, process2)
children_worker = Worker(queueName, process1)

flow = FlowProducer()
await flow.add(
{
"name": 'parent-job',
"queueName": parent_queue_name,
"data": {},
"children": [
{"name": child_job_name, "data": {"idx": 0, "foo": 'bar'}, "queueName": queueName},
{"name": child_job_name, "data": {"idx": 1, "foo": 'baz'}, "queueName": queueName},
{"name": child_job_name, "data": {"idx": 2, "foo": 'qux'}, "queueName": queueName}
]
}
)

await processing_children
await processing_parent

def on_parent_processed(future):
self.assertIn(children_data[0], future.result().values())
self.assertIn(children_data[1], future.result().values())
self.assertIn(children_data[2], future.result().values())

processing_parent.add_done_callback(on_parent_processed)

await parent_worker.close()
await children_worker.close()
await flow.close()

parent_queue = Queue(parent_queue_name)
await parent_queue.pause()
await parent_queue.obliterate()
await parent_queue.close()

async def test_get_children_values_on_simple_jobs(self):
queue = Queue(queueName)
job = await queue.add("test", {"foo": "bar"}, {"delay": 1500})
children_values = await job.getChildrenValues()
self.assertEqual(children_values, {})

await queue.close()

if __name__ == '__main__':
unittest.main()

0 comments on commit 7fa3ba7

Please sign in to comment.