|
| 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 <fastrtps/rtps/RTPSDomain.h> |
| 18 | +#include <fastrtps/rtps/participant/RTPSParticipant.h> |
| 19 | +#include <fastrtps/rtps/writer/RTPSWriter.h> |
| 20 | +#include <fastrtps/rtps/history/WriterHistory.h> |
| 21 | + |
| 22 | + |
| 23 | +namespace eprosima { |
| 24 | +namespace fastrtps { |
| 25 | +namespace rtps { |
| 26 | + |
| 27 | +using namespace testing; |
| 28 | + |
| 29 | +#define MAX_MESSAGE_SIZE 300 |
| 30 | + |
| 31 | +void cache_change_fragment( |
| 32 | + uint32_t max_message_size, |
| 33 | + uint32_t inline_qos_length, |
| 34 | + bool expected_fragmentation) |
| 35 | +{ |
| 36 | + uint32_t domain_id = 0; |
| 37 | + uint32_t initial_reserved_caches = 10; |
| 38 | + std::string max_message_size_str = std::to_string(max_message_size); |
| 39 | + |
| 40 | + RTPSParticipantAttributes p_attr; |
| 41 | + p_attr.properties.properties().emplace_back("fastdds.max_message_size", max_message_size_str); |
| 42 | + RTPSParticipant* participant = RTPSDomain::createParticipant( |
| 43 | + domain_id, true, p_attr); |
| 44 | + |
| 45 | + ASSERT_NE(participant, nullptr); |
| 46 | + |
| 47 | + HistoryAttributes h_attr; |
| 48 | + h_attr.memoryPolicy = DYNAMIC_RESERVE_MEMORY_MODE; |
| 49 | + h_attr.initialReservedCaches = initial_reserved_caches; |
| 50 | + h_attr.payloadMaxSize = 250; |
| 51 | + WriterHistory* history = new WriterHistory(h_attr); |
| 52 | + |
| 53 | + WriterAttributes w_attr; |
| 54 | + RTPSWriter* writer = RTPSDomain::createRTPSWriter(participant, w_attr, history); |
| 55 | + |
| 56 | + ASSERT_NE(writer, nullptr); |
| 57 | + |
| 58 | + CacheChange_t* change = writer->new_change(ALIVE); |
| 59 | + if (expected_fragmentation) |
| 60 | + { |
| 61 | + change->serializedPayload.length = 3 * max_message_size; |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + change->serializedPayload.length = max_message_size / 3; |
| 66 | + } |
| 67 | + change->inline_qos.length = inline_qos_length; |
| 68 | + history->add_change(change); |
| 69 | + |
| 70 | + auto result = change->getFragmentSize(); |
| 71 | + std::cout << "Fragment size: " << result << std::endl; |
| 72 | + if (expected_fragmentation) |
| 73 | + { |
| 74 | + ASSERT_NE(result, 0); |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + ASSERT_EQ(result, 0); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * This test checks the fragment size calculation for a cache change depending on the inline qos length. |
| 84 | + * The change.serializedPayload.length is set to 3 times the max_allowed_payload_size, so the fragment size should always be set. |
| 85 | + * In case of an overflow in the attribute high_mark_for_frag_ the fragment size will not be set, which is an error. |
| 86 | + */ |
| 87 | +TEST(WriterHistoryTests, final_high_mark_for_frag_overflow) |
| 88 | +{ |
| 89 | + for (uint32_t inline_qos_length = 0; inline_qos_length < MAX_MESSAGE_SIZE; inline_qos_length += 40) |
| 90 | + { |
| 91 | + cache_change_fragment(MAX_MESSAGE_SIZE, inline_qos_length, true); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +} // namespace rtps |
| 96 | +} // namespace fastdds |
| 97 | +} // namespace eprosima |
| 98 | + |
| 99 | +int main( |
| 100 | + int argc, |
| 101 | + char** argv) |
| 102 | +{ |
| 103 | + testing::InitGoogleTest(&argc, argv); |
| 104 | + return RUN_ALL_TESTS(); |
| 105 | +} |
0 commit comments