etl/test/test_shared_message.cpp
John Wellbelove 97a6e6a035 Squashed commit of the following:
commit 04ba91bcccc8e12867962bc3746665f430672a23
Author: John Wellbelove <github@wellbelove.co.uk>
Date:   Sat Feb 6 10:43:34 2021 +0000

    Updated shared message unit test

commit fababc5cf748073464b4294a50c201cb0aa4fa13
Author: John Wellbelove <github@wellbelove.co.uk>
Date:   Fri Feb 5 09:41:52 2021 +0000

    Updated FSM generator

commit ff287bcf0a833ca70933354b42b8b036b422ff81
Author: John Wellbelove <github@wellbelove.co.uk>
Date:   Fri Feb 5 09:35:56 2021 +0000

    Updated message router generator

commit 52724e1e62b55dad81e2f80dd656026d828b3214
Author: John Wellbelove <github@wellbelove.co.uk>
Date:   Thu Feb 4 13:06:50 2021 +0000

    Updated unit tests

commit 0d89105262aa050577ccc17ddc6eece9f4fc5d18
Author: John Wellbelove <github@wellbelove.co.uk>
Date:   Tue Feb 2 20:09:35 2021 +0000

    Interim commit

commit ce8385ff24826103c7a55267ccf3f8a31f517e0b
Author: John Wellbelove <github@wellbelove.co.uk>
Date:   Tue Feb 2 18:13:29 2021 +0000

    Squashed commit of the following:

    commit 007b56d03513887636b84fc246e57d6c4f8b777e
    Author: John Wellbelove <github@wellbelove.co.uk>
    Date:   Tue Feb 2 18:09:51 2021 +0000

        Squashed commit of the following:

        commit 6107c4538be149137209d85e5f41031291bc7150
        Author: John Wellbelove <github@wellbelove.co.uk>
        Date:   Tue Feb 2 12:15:31 2021 +0000

            Added move constructor and move assignment to etl::shared_message

        commit c9a5716012db9b614ea67660ebf64dcb790ce571
        Author: John Wellbelove <github@wellbelove.co.uk>
        Date:   Sun Jan 31 12:36:29 2021 +0000

            Squashed commit of the following:

            commit e5f4eb6fb38c337c82fcc250f17a8f21eb788975
            Author: John Wellbelove <github@wellbelove.co.uk>
            Date:   Sun Jan 31 12:34:49 2021 +0000

                Squashed commit of the following:

                commit 23c5f1d3f9b13ff9e46ce3de96aefeb655d5ed97
                Author: John Wellbelove <github@wellbelove.co.uk>
                Date:   Sun Jan 31 12:32:35 2021 +0000

                    Fixed rollover error for etl::queue_spsc_atomic

                    Added 'required_alignment' parameter to 'allocate' for etl::imemeory_block_allocator.
                    Updated QueuedMessageRouter example

    commit e5f4eb6fb38c337c82fcc250f17a8f21eb788975
    Author: John Wellbelove <github@wellbelove.co.uk>
    Date:   Sun Jan 31 12:34:49 2021 +0000

        Squashed commit of the following:

        commit 23c5f1d3f9b13ff9e46ce3de96aefeb655d5ed97
        Author: John Wellbelove <github@wellbelove.co.uk>
        Date:   Sun Jan 31 12:32:35 2021 +0000

            Fixed rollover error for etl::queue_spsc_atomic

            Added 'required_alignment' parameter to 'allocate' for etl::imemeory_block_allocator.
            Updated QueuedMessageRouter example

commit c9a5716012db9b614ea67660ebf64dcb790ce571
Author: John Wellbelove <github@wellbelove.co.uk>
Date:   Sun Jan 31 12:36:29 2021 +0000

    Squashed commit of the following:

    commit e5f4eb6fb38c337c82fcc250f17a8f21eb788975
    Author: John Wellbelove <github@wellbelove.co.uk>
    Date:   Sun Jan 31 12:34:49 2021 +0000

        Squashed commit of the following:

        commit 23c5f1d3f9b13ff9e46ce3de96aefeb655d5ed97
        Author: John Wellbelove <github@wellbelove.co.uk>
        Date:   Sun Jan 31 12:32:35 2021 +0000

            Fixed rollover error for etl::queue_spsc_atomic

            Added 'required_alignment' parameter to 'allocate' for etl::imemeory_block_allocator.
            Updated QueuedMessageRouter example
2021-02-07 11:02:49 +00:00

231 lines
6.7 KiB
C++

/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2020 jwellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#include "UnitTest++/UnitTest++.h"
#include "etl/shared_message.h"
#include "etl/message.h"
#include "etl/message_router.h"
#include "etl/message_bus.h"
#include "etl/queue.h"
#include "etl/fixed_sized_memory_block_allocator.h"
#include "etl/reference_counted_message_pool.h"
namespace
{
constexpr etl::message_id_t MessageId1 = 1U;
constexpr etl::message_id_t MessageId2 = 2U;
constexpr etl::message_router_id_t RouterId1 = 1U;
constexpr etl::message_router_id_t RouterId2 = 2U;
//*************************************************************************
struct Message1 : public etl::message<MessageId1>
{
Message1(int i_)
: i(i_)
{
}
~Message1()
{
}
int i;
};
//*************************************************************************
struct Message2 : public etl::message<MessageId2>
{
~Message2()
{
}
};
//*************************************************************************
struct Router1 : public etl::message_router<Router1, Message1, Message2>
{
Router1()
: message_router(RouterId1)
, count_message1(0)
, count_message2(0)
, count_unknown_message(0)
{
}
void on_receive(const Message1& message)
{
++count_message1;
}
void on_receive(const Message2& message)
{
++count_message2;
}
void on_receive_unknown(const etl::imessage& message)
{
}
void clear()
{
count_message1 = 0;
count_message2 = 0;
count_unknown_message = 0;
}
int count_message1;
int count_message2;
int count_unknown_message;
};
//*************************************************************************
struct Router2 : public etl::message_router<Router2, Message1>
{
Router2()
: message_router(RouterId2)
, count_message1(0)
, count_message2(0)
, count_unknown_message(0)
{
}
void on_receive(const Message1& message)
{
++count_message1;
}
void on_receive_unknown(const etl::imessage& message)
{
++count_unknown_message;
}
void clear()
{
count_message1 = 0;
count_message2 = 0;
count_unknown_message = 0;
}
int count_message1;
int count_message2;
int count_unknown_message;
};
//*************************************************************************
struct Bus : public etl::message_bus<2U>
{
};
SUITE(test_shared_message)
{
Router1 router1;
Router2 router2;
Bus bus;
using pool_message_parameters = etl::atomic_counted_message_pool::pool_message_parameters<Message1, Message2>;
etl::fixed_sized_memory_block_allocator<pool_message_parameters::max_size,
pool_message_parameters::max_alignment,
4U> memory_allocator;
etl::atomic_counted_message_pool message_pool(memory_allocator);
//*************************************************************************
class Message2Allocator : public etl::ireference_counted_message_pool
{
public:
static etl::reference_counted_message<Message2, void>& Get()
{
static Message2Allocator allocator;
static Message2 message2;
static etl::reference_counted_message<Message2, void> rcm2(message2, allocator);
return rcm2;
}
void release(const etl::ireference_counted_message& msg) override
{
// Do nothing.
}
};
//*************************************************************************
TEST(test_move_constructor)
{
etl::shared_message sm1(std::move(etl::shared_message(message_pool, Message1(1))));
CHECK_EQUAL(1, sm1.get_reference_count());
}
//*************************************************************************
TEST(test_move_assignemnt)
{
etl::shared_message sm2 = etl::shared_message(message_pool, Message1(2));
sm2 = std::move(etl::shared_message(message_pool, Message1(3)));
CHECK_EQUAL(1, sm2.get_reference_count());
}
//*************************************************************************
TEST(test_send_to_routers)
{
bus.clear();
bus.subscribe(router1);
bus.subscribe(router2);
router1.clear();
router2.clear();
etl::shared_message sm1(message_pool, Message1(1)); // sm1 holds a Message1 that is owned by message_pool.
etl::shared_message sm2(message_pool, Message2()); // sm2 holds a Message2 that is owned by message_pool.
etl::shared_message sm3(Message2Allocator::Get()); // sm3 holds a Message2 that is owned by a statically allocated message pool.
etl::shared_message sm4(sm1); // sm4 is a copy of sm1.
bus.receive(sm1);
bus.receive(sm2);
bus.receive(sm1);
bus.receive(sm3);
bus.receive(sm4); // sm4 is a copy of sm1
bus.receive(RouterId2, sm1); // Only send sm1 to Router2
CHECK_EQUAL(2, sm1.get_reference_count());
CHECK_EQUAL(1, sm2.get_reference_count());
CHECK_EQUAL(1, sm3.get_reference_count());
CHECK_EQUAL(2, sm4.get_reference_count());
CHECK_EQUAL(3, router1.count_message1);
CHECK_EQUAL(2, router1.count_message2);
CHECK_EQUAL(0, router1.count_unknown_message);
CHECK_EQUAL(4, router2.count_message1);
CHECK_EQUAL(0, router2.count_message2);
CHECK_EQUAL(0, router2.count_unknown_message);
}
}
}