2017-05-19 17:57:07 -07:00
|
|
|
|
|
|
|
from ocrmypdf.pdfinfo import PdfInfo, PageInfo
|
|
|
|
from ocrmypdf.pipeline import JobContext, JobContextManager
|
|
|
|
from multiprocessing import Process
|
|
|
|
from multiprocessing.managers import BaseProxy
|
|
|
|
|
|
|
|
|
2017-05-23 16:32:13 -07:00
|
|
|
def test_jobcontext_proxy(resources):
|
|
|
|
# Prove that managers are set up correctly to share state among processes
|
2017-05-19 17:57:07 -07:00
|
|
|
manager = JobContextManager()
|
|
|
|
manager.register('JobContext', JobContext)
|
2017-05-23 16:32:13 -07:00
|
|
|
|
|
|
|
# Start the manager in a child process (or maybe thread)
|
2017-05-19 17:57:07 -07:00
|
|
|
manager.start()
|
|
|
|
|
2017-05-23 16:32:13 -07:00
|
|
|
# Tell the manager process to retrieve pdf info
|
2017-05-19 17:57:07 -07:00
|
|
|
context = manager.JobContext()
|
2017-05-23 16:32:13 -07:00
|
|
|
context.generate_pdfinfo(resources / 'graph.pdf')
|
2017-05-19 17:57:07 -07:00
|
|
|
|
2017-05-23 16:32:13 -07:00
|
|
|
# Get a copy of that information for this process
|
|
|
|
pdfinfo = context.get_pdfinfo()
|
|
|
|
assert len(pdfinfo) == 1
|
2017-05-19 17:57:07 -07:00
|
|
|
assert pdfinfo[0].rotation == 0
|
2017-05-23 16:32:13 -07:00
|
|
|
|
|
|
|
# Update information and send back to manager
|
|
|
|
pdfinfo[0].rotation = 90
|
2017-05-19 17:57:07 -07:00
|
|
|
context.set_pdfinfo(pdfinfo)
|
2017-05-23 16:32:13 -07:00
|
|
|
|
|
|
|
# Retrieve again, ensure it stayed changed
|
|
|
|
pdfinfo2 = context.get_pdfinfo()
|
|
|
|
assert pdfinfo2[0].rotation == 90
|
|
|
|
|
|
|
|
# Start a new process which gets its own proxy object
|
|
|
|
def client(context):
|
|
|
|
assert isinstance(context, BaseProxy)
|
|
|
|
pdfinfo = context.get_pdfinfo()
|
|
|
|
page = pdfinfo[0]
|
|
|
|
assert page.rotation == 90
|
|
|
|
page.rotation += 90
|
|
|
|
context.set_pdfinfo(pdfinfo)
|
2017-05-19 17:57:07 -07:00
|
|
|
|
|
|
|
p = Process(target=client, args=(context,))
|
|
|
|
p.start()
|
|
|
|
p.join()
|
|
|
|
assert p.exitcode == 0, "Child process failed"
|
|
|
|
|
2017-05-23 16:32:13 -07:00
|
|
|
assert context.get_pdfinfo()[0].rotation == 180
|