frommock_xqueue_serverimportMockXQueueServerfromlettuceimportbefore,after,worldfromdjango.confimportsettingsimportthreading@before.alldefsetup_mock_xqueue_server():# Retrieve the local port from settingsserver_port=settings.XQUEUE_PORT# Create the mock server instanceserver=MockXQueueServer(server_port)# Start the server running in a separate daemon thread# Because the thread is a daemon, it will terminate# when the main thread terminates.server_thread=threading.Thread(target=server.serve_forever)server_thread.daemon=Trueserver_thread.start()# Store the server instance in lettuce's world# so that other steps can access it# (and we can shut it down later)world.xqueue_server=server@after.alldefteardown_mock_xqueue_server(total):# Stop the xqueue server and free up the portworld.xqueue_server.shutdown()