aboutsummaryrefslogtreecommitdiff
path: root/profiling/server/src/basePipeServer/tests/BasePipeServerTests.cpp
blob: c97fecd94d05dff2ed61db75865f1dbf0e51e1de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// Copyright © 2020 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include <ConnectionHandler.hpp>

#include <SocketProfilingConnection.hpp>
#include <Processes.hpp>

#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test_suite.hpp>


BOOST_AUTO_TEST_SUITE(BasePipeServerTests)

using namespace armnn;
using namespace armnnProfiling;

BOOST_AUTO_TEST_CASE(BasePipeServerTest)
{
    // Setup the mock service to bind to the UDS.
    std::string udsNamespace = "gatord_namespace";

    // Try to initialize a listening socket through the ConnectionHandler
    BOOST_CHECK_NO_THROW(ConnectionHandler connectionHandler(udsNamespace, true));

    // The socket should close once we leave the scope of BOOST_CHECK_NO_THROW
    // and socketProfilingConnection should fail to connect
    BOOST_CHECK_THROW(profiling::SocketProfilingConnection socketProfilingConnection,
                      armnnProfiling::SocketConnectionException);

    // Try to initialize a listening socket through the ConnectionHandler again
    ConnectionHandler connectionHandler(udsNamespace, true);
    // socketProfilingConnection should connect now
    profiling::SocketProfilingConnection socketProfilingConnection;
    BOOST_TEST(socketProfilingConnection.IsOpen());

    auto basePipeServer = connectionHandler.GetNewBasePipeServer(false);
    // GetNewBasePipeServer will return null if it fails to create a socket
    BOOST_TEST(basePipeServer.get());

    profiling::BufferManager bufferManager;
    profiling::SendCounterPacket sendCounterPacket(bufferManager);

    // Check that we can receive a StreamMetaDataPacket
    sendCounterPacket.SendStreamMetaDataPacket();

    auto packetBuffer = bufferManager.GetReadableBuffer();
    const unsigned char* readBuffer = packetBuffer->GetReadableData();
    unsigned int readBufferSize = packetBuffer->GetSize();

    BOOST_TEST(readBuffer);
    BOOST_TEST(readBufferSize > 0);

    socketProfilingConnection.WritePacket(readBuffer,readBufferSize);
    bufferManager.MarkRead(packetBuffer);

    BOOST_TEST(basePipeServer.get()->WaitForStreamMetaData());
    BOOST_TEST(basePipeServer.get()->GetStreamMetadataPid() == armnnUtils::Processes::GetCurrentId());
    BOOST_TEST(basePipeServer.get()->GetStreamMetadataMaxDataLen() == MAX_METADATA_PACKET_LENGTH);

    // Now try a simple PeriodicCounterSelectionPacket
    sendCounterPacket.SendPeriodicCounterSelectionPacket(50, {1,2,3,4,5});

    packetBuffer = bufferManager.GetReadableBuffer();
    readBuffer = packetBuffer->GetReadableData();
    readBufferSize = packetBuffer->GetSize();

    BOOST_TEST(readBuffer);
    BOOST_TEST(readBufferSize > 0);

    socketProfilingConnection.WritePacket(readBuffer,readBufferSize);
    bufferManager.MarkRead(packetBuffer);

    auto packet1 = basePipeServer.get()->WaitForPacket(500);

    BOOST_TEST(!packet1.IsEmpty());
    BOOST_TEST(packet1.GetPacketFamily() == 0);
    BOOST_TEST(packet1.GetPacketId() == 4);
    BOOST_TEST(packet1.GetLength() == 14);

    // Try and send the packet back to the client
    basePipeServer.get()->SendPacket(packet1.GetPacketFamily(),
                                     packet1.GetPacketId(),
                                     packet1.GetData(),
                                     packet1.GetLength());

    auto packet2 = socketProfilingConnection.ReadPacket(500);

    BOOST_TEST(!packet2.IsEmpty());
    BOOST_TEST(packet2.GetPacketFamily() == 0);
    BOOST_TEST(packet2.GetPacketId() == 4);
    BOOST_TEST(packet2.GetLength() == 14);

    socketProfilingConnection.Close();
}

BOOST_AUTO_TEST_SUITE_END()