From 815da1b7ad44493860babfe01fbd81242251b26a Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Tue, 14 Nov 2017 14:30:48 +0200 Subject: [PATCH] MXS-1461 Add mock RouterSession --- .../test/maxscale/mock/routersession.hh | 94 +++++++++++++++++++ .../dbfwfilter/test/mock_routersession.cc | 80 ++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 server/modules/filter/dbfwfilter/test/maxscale/mock/routersession.hh create mode 100644 server/modules/filter/dbfwfilter/test/mock_routersession.cc diff --git a/server/modules/filter/dbfwfilter/test/maxscale/mock/routersession.hh b/server/modules/filter/dbfwfilter/test/maxscale/mock/routersession.hh new file mode 100644 index 000000000..744dd3e39 --- /dev/null +++ b/server/modules/filter/dbfwfilter/test/maxscale/mock/routersession.hh @@ -0,0 +1,94 @@ +#pragma once +/* + * Copyright (c) 2016 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2020-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ + +#include +#include +#include +#include +#include "../filtermodule.hh" +#include "mock.hh" + +namespace maxscale +{ + +namespace mock +{ + +class Backend; + +/** + * An instance of RouterSession is a router to which a filter forwards + * data. + */ +class RouterSession : private MXS_ROUTER_SESSION +{ + RouterSession(const RouterSession&); + RouterSession& operator = (const RouterSession&); + +public: + /** + * Constructor + * + * @param pBackend The backend associated with the router. + */ + RouterSession(Backend* pBackend); + ~RouterSession(); + + /** + * Set the router as the downstream filter of a particular filter. + * The filter will at the same time become the upstream filter of + * this router. + * + * @param pFilter_session The filter to set this router as downstream + * filter of. + */ + void set_as_downstream_on(FilterModule::Session* pFilter_session); + + /** + * Called by the backend to deliver a response. + * + * @return Whatever the upstream filter returns. + */ + int32_t clientReply(GWBUF* pResponse); + + /** + * Causes the router to make its associated backend deliver a response + * to this router, which will then deliver it forward to its associated + * upstream filter. + * + * @return True if there are additional responses to deliver. + */ + bool respond(); + + /** + * Are there responses available. + * + * @return True, if there are no responses, false otherwise. + */ + bool idle() const; + +private: + int32_t routeQuery(MXS_ROUTER* pInstance, GWBUF* pStatement); + + static int32_t routeQuery(MXS_FILTER* pInstance, MXS_FILTER_SESSION* pRouter_session, GWBUF* pStatement); + +private: + MXS_ROUTER m_instance; + Backend* m_pBackend; + FilterModule::Session* m_pUpstream_filter_session; +}; + +} + +} diff --git a/server/modules/filter/dbfwfilter/test/mock_routersession.cc b/server/modules/filter/dbfwfilter/test/mock_routersession.cc new file mode 100644 index 000000000..40d797318 --- /dev/null +++ b/server/modules/filter/dbfwfilter/test/mock_routersession.cc @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2016 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2020-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ + +#include "maxscale/mock/routersession.hh" +#include "maxscale/mock/backend.hh" + +namespace maxscale +{ + +namespace mock +{ + +RouterSession::RouterSession(Backend* pBackend) + : m_pBackend(pBackend) +{ + memset(&m_instance, 0, sizeof(m_instance)); +} + +RouterSession::~RouterSession() +{ +} + +void RouterSession::set_as_downstream_on(FilterModule::Session* pFilter_session) +{ + MXS_DOWNSTREAM downstream; + downstream.instance = reinterpret_cast(&m_instance); + downstream.session = reinterpret_cast(this); + downstream.routeQuery = &RouterSession::routeQuery; + + pFilter_session->setDownstream(&downstream); + + m_pUpstream_filter_session = pFilter_session; +} + +bool RouterSession::respond() +{ + return m_pBackend->respond(this); +} + +bool RouterSession::idle() const +{ + return m_pBackend->idle(this); +} + +int32_t RouterSession::routeQuery(MXS_ROUTER* pInstance, GWBUF* pStatement) +{ + ss_dassert(pInstance == &m_instance); + + m_pBackend->handle_statement(this, pStatement); + return 1; +} + +int32_t RouterSession::clientReply(GWBUF* pResponse) +{ + return m_pUpstream_filter_session->clientReply(pResponse); +} + +//static +int32_t RouterSession::routeQuery(MXS_FILTER* pInstance, + MXS_FILTER_SESSION* pRouter_session, + GWBUF* pStatement) +{ + RouterSession* pThis = reinterpret_cast(pRouter_session); + + return pThis->routeQuery(reinterpret_cast(pInstance), pStatement); +} + +} + +}