|
| 1 | +// Copyright 2020 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <gtest/gtest.h> |
| 16 | + |
| 17 | +#include <fastdds/rtps/RTPSDomain.hpp> |
| 18 | +#include <fastdds/rtps/participant/RTPSParticipant.hpp> |
| 19 | +#include <fastdds/rtps/writer/RTPSWriter.hpp> |
| 20 | +#include <fastdds/rtps/history/IPayloadPool.hpp> |
| 21 | +#include <fastdds/rtps/history/WriterHistory.hpp> |
| 22 | + |
| 23 | + |
| 24 | +namespace eprosima { |
| 25 | +namespace fastdds { |
| 26 | +namespace rtps { |
| 27 | + |
| 28 | +using namespace testing; |
| 29 | + |
| 30 | +#define MAX_MESSAGE_SIZE 300 |
| 31 | + |
| 32 | +void cache_change_fragment( |
| 33 | + uint32_t max_message_size, |
| 34 | + uint32_t inline_qos_length, |
| 35 | + bool expected_fragmentation) |
| 36 | +{ |
| 37 | + uint32_t domain_id = 0; |
| 38 | + uint32_t initial_reserved_caches = 10; |
| 39 | + std::string max_message_size_str = std::to_string(max_message_size); |
| 40 | + |
| 41 | + RTPSParticipantAttributes p_attr; |
| 42 | + p_attr.properties.properties().emplace_back("fastdds.max_message_size", max_message_size_str); |
| 43 | + RTPSParticipant* participant = RTPSDomain::createParticipant( |
| 44 | + domain_id, true, p_attr); |
| 45 | + |
| 46 | + ASSERT_NE(participant, nullptr); |
| 47 | + |
| 48 | + HistoryAttributes h_attr; |
| 49 | + h_attr.memoryPolicy = DYNAMIC_RESERVE_MEMORY_MODE; |
| 50 | + h_attr.initialReservedCaches = initial_reserved_caches; |
| 51 | + h_attr.payloadMaxSize = 250; |
| 52 | + WriterHistory* history = new WriterHistory(h_attr); |
| 53 | + |
| 54 | + WriterAttributes w_attr; |
| 55 | + RTPSWriter* writer = RTPSDomain::createRTPSWriter(participant, w_attr, history); |
| 56 | + |
| 57 | + ASSERT_NE(writer, nullptr); |
| 58 | + |
| 59 | + CacheChange_t* change = history->create_change(ALIVE); |
| 60 | + if (expected_fragmentation) |
| 61 | + { |
| 62 | + change->serializedPayload.length = 3 * max_message_size; |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + change->serializedPayload.length = max_message_size / 3; |
| 67 | + } |
| 68 | + change->inline_qos.length = inline_qos_length; |
| 69 | + history->add_change(change); |
| 70 | + |
| 71 | + auto result = change->getFragmentSize(); |
| 72 | + std::cout << "Fragment size: " << result << std::endl; |
| 73 | + if (expected_fragmentation) |
| 74 | + { |
| 75 | + ASSERT_NE(result, 0); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + ASSERT_EQ(result, 0); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * This test checks the get_max_allowed_payload_size() method of the BaseWriter class. |
| 85 | + * When setting the RTPS Participant Attribute property fastdds.max_message_size to a value lower than the |
| 86 | + * message overhead, if the method does not overflow the fragment size will be set. |
| 87 | + * If the max_message_size is big enough for the overhead, inline_qos and serializedPayload, |
| 88 | + * then no fragmentation will occur. |
| 89 | + */ |
| 90 | +TEST(WriterHistoryTests, get_max_allowed_payload_size_overflow) |
| 91 | +{ |
| 92 | + cache_change_fragment(100, 0, true); |
| 93 | + cache_change_fragment(MAX_MESSAGE_SIZE, 0, false); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * This test checks the fragment size calculation for a cache change depending on the inline qos length. |
| 98 | + * The change.serializedPayload.length is set to 3 times the max_allowed_payload_size, so the fragment size should always be set. |
| 99 | + * In case of an overflow in the attribute high_mark_for_frag_ the fragment size will not be set, which is an error. |
| 100 | + */ |
| 101 | +TEST(WriterHistoryTests, final_high_mark_for_frag_overflow) |
| 102 | +{ |
| 103 | + for (uint32_t inline_qos_length = 0; inline_qos_length < MAX_MESSAGE_SIZE; inline_qos_length += 40) |
| 104 | + { |
| 105 | + cache_change_fragment(MAX_MESSAGE_SIZE, inline_qos_length, true); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace rtps |
| 110 | +} // namespace fastdds |
| 111 | +} // namespace eprosima |
| 112 | + |
| 113 | +int main( |
| 114 | + int argc, |
| 115 | + char** argv) |
| 116 | +{ |
| 117 | + testing::InitGoogleTest(&argc, argv); |
| 118 | + return RUN_ALL_TESTS(); |
| 119 | +} |
0 commit comments