blob: 57fc9e9da6ebeb8edfff7c0bdcc590482a4400ba [file] [log] [blame]
Finn Williams2ed809c2020-04-20 21:21:07 +01001//
2// Copyright © 2020 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Finn Williams0c8cb992020-05-07 10:38:15 +01006#include <ConnectionHandler.hpp>
Finn Williams2ed809c2020-04-20 21:21:07 +01007
8#include <SocketProfilingConnection.hpp>
9#include <Processes.hpp>
10
11#include <boost/test/test_tools.hpp>
12#include <boost/test/unit_test_suite.hpp>
13
14
15BOOST_AUTO_TEST_SUITE(BasePipeServerTests)
16
17using namespace armnn;
18using namespace armnnProfiling;
19
20BOOST_AUTO_TEST_CASE(BasePipeServerTest)
21{
22 // Setup the mock service to bind to the UDS.
23 std::string udsNamespace = "gatord_namespace";
24
25 // Try to initialize a listening socket through the ConnectionHandler
26 BOOST_CHECK_NO_THROW(ConnectionHandler connectionHandler(udsNamespace, true));
27
28 // The socket should close once we leave the scope of BOOST_CHECK_NO_THROW
29 // and socketProfilingConnection should fail to connect
30 BOOST_CHECK_THROW(profiling::SocketProfilingConnection socketProfilingConnection,
31 armnnProfiling::SocketConnectionException);
32
33 // Try to initialize a listening socket through the ConnectionHandler again
34 ConnectionHandler connectionHandler(udsNamespace, true);
35 // socketProfilingConnection should connect now
36 profiling::SocketProfilingConnection socketProfilingConnection;
37 BOOST_TEST(socketProfilingConnection.IsOpen());
38
39 auto basePipeServer = connectionHandler.GetNewBasePipeServer(false);
40 // GetNewBasePipeServer will return null if it fails to create a socket
41 BOOST_TEST(basePipeServer.get());
42
43 profiling::BufferManager bufferManager;
44 profiling::SendCounterPacket sendCounterPacket(bufferManager);
45
46 // Check that we can receive a StreamMetaDataPacket
47 sendCounterPacket.SendStreamMetaDataPacket();
48
49 auto packetBuffer = bufferManager.GetReadableBuffer();
50 const unsigned char* readBuffer = packetBuffer->GetReadableData();
51 unsigned int readBufferSize = packetBuffer->GetSize();
52
53 BOOST_TEST(readBuffer);
Rob Hughesbb46dde2020-05-20 15:27:37 +010054 BOOST_TEST(readBufferSize > 0u);
Finn Williams2ed809c2020-04-20 21:21:07 +010055
56 socketProfilingConnection.WritePacket(readBuffer,readBufferSize);
57 bufferManager.MarkRead(packetBuffer);
58
59 BOOST_TEST(basePipeServer.get()->WaitForStreamMetaData());
60 BOOST_TEST(basePipeServer.get()->GetStreamMetadataPid() == armnnUtils::Processes::GetCurrentId());
61 BOOST_TEST(basePipeServer.get()->GetStreamMetadataMaxDataLen() == MAX_METADATA_PACKET_LENGTH);
62
63 // Now try a simple PeriodicCounterSelectionPacket
64 sendCounterPacket.SendPeriodicCounterSelectionPacket(50, {1,2,3,4,5});
65
66 packetBuffer = bufferManager.GetReadableBuffer();
67 readBuffer = packetBuffer->GetReadableData();
68 readBufferSize = packetBuffer->GetSize();
69
70 BOOST_TEST(readBuffer);
Rob Hughesbb46dde2020-05-20 15:27:37 +010071 BOOST_TEST(readBufferSize > 0u);
Finn Williams2ed809c2020-04-20 21:21:07 +010072
73 socketProfilingConnection.WritePacket(readBuffer,readBufferSize);
74 bufferManager.MarkRead(packetBuffer);
75
76 auto packet1 = basePipeServer.get()->WaitForPacket(500);
77
78 BOOST_TEST(!packet1.IsEmpty());
79 BOOST_TEST(packet1.GetPacketFamily() == 0);
80 BOOST_TEST(packet1.GetPacketId() == 4);
81 BOOST_TEST(packet1.GetLength() == 14);
82
83 // Try and send the packet back to the client
84 basePipeServer.get()->SendPacket(packet1.GetPacketFamily(),
85 packet1.GetPacketId(),
86 packet1.GetData(),
87 packet1.GetLength());
88
89 auto packet2 = socketProfilingConnection.ReadPacket(500);
90
91 BOOST_TEST(!packet2.IsEmpty());
92 BOOST_TEST(packet2.GetPacketFamily() == 0);
93 BOOST_TEST(packet2.GetPacketId() == 4);
94 BOOST_TEST(packet2.GetLength() == 14);
95
96 socketProfilingConnection.Close();
97}
98
99BOOST_AUTO_TEST_SUITE_END()