MXS-1461 Add mock Upstream class
Upstream can be used as the upstream filter of a filter to be tested.
This commit is contained in:
109
server/modules/filter/dbfwfilter/test/maxscale/mock/upstream.hh
Normal file
109
server/modules/filter/dbfwfilter/test/maxscale/mock/upstream.hh
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
#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 <maxscale/cppdefs.hh>
|
||||||
|
#include <memory>
|
||||||
|
#include <maxscale/filter.h>
|
||||||
|
#include "../filtermodule.hh"
|
||||||
|
|
||||||
|
namespace maxscale
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace mock
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An instance of Upstream represents an upstream object of a filter.
|
||||||
|
*/
|
||||||
|
class Upstream : public MXS_FILTER_SESSION
|
||||||
|
{
|
||||||
|
Upstream(const Upstream&);
|
||||||
|
Upstream& operator = (const Upstream&);
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* A Handler can be used for processing responses.
|
||||||
|
*/
|
||||||
|
class Handler
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~Handler();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when a response is received from the backend.
|
||||||
|
*
|
||||||
|
* @param pResponse The response packet.
|
||||||
|
*
|
||||||
|
* @return 1 if processing should continue, 0 otherwise.
|
||||||
|
*/
|
||||||
|
virtual int32_t clientReply(GWBUF* pResponse) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when @reset is called on the @c Upstream instance.
|
||||||
|
*/
|
||||||
|
virtual void reset();
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param pHandler Optional response handler.
|
||||||
|
*/
|
||||||
|
Upstream(Handler* pHandler = NULL);
|
||||||
|
~Upstream();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a response handler
|
||||||
|
*
|
||||||
|
* @param pHandler The new response handler.
|
||||||
|
*
|
||||||
|
* @return The previous response handler.
|
||||||
|
*/
|
||||||
|
Handler* set_handler(Handler* pHandler);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* How many responses have been handled.
|
||||||
|
*
|
||||||
|
* @return The number of responses since last call to @c reset.
|
||||||
|
*/
|
||||||
|
size_t n_responses() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset the Upstream object. The number of counted responsed will
|
||||||
|
* be set to 0. If the Upstream object has a handler, then its @c reset
|
||||||
|
* function will be called as well.
|
||||||
|
*/
|
||||||
|
void reset();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set this object as upstream filter of provided filter.
|
||||||
|
*
|
||||||
|
* @param session The filter session whose upstream filter should be set.
|
||||||
|
*/
|
||||||
|
void set_as_upstream_on(FilterModule::Session& session);
|
||||||
|
|
||||||
|
private:
|
||||||
|
int32_t clientReply(GWBUF* pResponse);
|
||||||
|
|
||||||
|
static int32_t clientReply(MXS_FILTER* pInstance, MXS_FILTER_SESSION* pSession, GWBUF* pResponse);
|
||||||
|
|
||||||
|
private:
|
||||||
|
MXS_FILTER m_instance;
|
||||||
|
Handler* m_pHandler;
|
||||||
|
size_t m_n_responses;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
111
server/modules/filter/dbfwfilter/test/mock_upstream.cc
Normal file
111
server/modules/filter/dbfwfilter/test/mock_upstream.cc
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
/*
|
||||||
|
* 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/upstream.hh"
|
||||||
|
|
||||||
|
namespace maxscale
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace mock
|
||||||
|
{
|
||||||
|
|
||||||
|
//
|
||||||
|
// Upstream
|
||||||
|
//
|
||||||
|
Upstream::Upstream(Handler* pHandler)
|
||||||
|
: m_pHandler(pHandler)
|
||||||
|
, m_n_responses(0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Upstream::~Upstream()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Upstream::n_responses() const
|
||||||
|
{
|
||||||
|
return m_n_responses;
|
||||||
|
}
|
||||||
|
|
||||||
|
Upstream::Handler* Upstream::set_handler(Handler* pHandler)
|
||||||
|
{
|
||||||
|
Handler* pH = m_pHandler;
|
||||||
|
m_pHandler = pHandler;
|
||||||
|
return pH;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Upstream::reset()
|
||||||
|
{
|
||||||
|
m_n_responses = 0;
|
||||||
|
|
||||||
|
if (m_pHandler)
|
||||||
|
{
|
||||||
|
m_pHandler->reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Upstream::set_as_upstream_on(FilterModule::Session& filter_session)
|
||||||
|
{
|
||||||
|
MXS_UPSTREAM upstream;
|
||||||
|
upstream.instance = &m_instance;
|
||||||
|
upstream.session = this;
|
||||||
|
upstream.clientReply = &Upstream::clientReply;
|
||||||
|
upstream.error = NULL;
|
||||||
|
|
||||||
|
filter_session.setUpstream(&upstream);
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t Upstream::clientReply(GWBUF* pResponse)
|
||||||
|
{
|
||||||
|
int rv = 1;
|
||||||
|
|
||||||
|
++m_n_responses;
|
||||||
|
|
||||||
|
if (m_pHandler)
|
||||||
|
{
|
||||||
|
rv = m_pHandler->clientReply(pResponse);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
gwbuf_free(pResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
//static
|
||||||
|
int32_t Upstream::clientReply(MXS_FILTER* pInstance,
|
||||||
|
MXS_FILTER_SESSION* pSession,
|
||||||
|
GWBUF* pResponse)
|
||||||
|
{
|
||||||
|
Upstream* pUpstream = reinterpret_cast<Upstream*>(pSession);
|
||||||
|
ss_dassert(pInstance == &pUpstream->m_instance);
|
||||||
|
|
||||||
|
return pUpstream->clientReply(pResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Upstream::Handler
|
||||||
|
//
|
||||||
|
|
||||||
|
Upstream::Handler::~Handler()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Upstream::Handler::reset()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user