aboutsummaryrefslogtreecommitdiff
path: root/profiling/server/src/basePipeServer/tests/BasePipeServerTests.cpp
blob: 8374102033c7a4a80cc461cc91fb3f0cbf849307 (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 and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include <server/include/basePipeServer/ConnectionHandler.hpp>

#include <client/src/BufferManager.hpp>
#include <client/src/SendCounterPacket.hpp>
#include <client/src/SocketProfilingConnection.hpp>

#include <common/include/Processes.hpp>

#include <doctest/doctest.h>

TEST_SUITE("BasePipeServerTests")
{
using namespace arm::pipe;

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
    CHECK_NOTHROW(ConnectionHandler connectionHandler(udsNamespace, true));

    // The socket should close once we leave the scope of CHECK_NOTHROW
    // and socketProfilingConnection should fail to connect
    CHECK_THROWS_AS(arm::pipe::SocketProfilingConnection socketProfilingConnection,
                      arm::pipe::SocketConnectionException);

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

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

    arm::pipe::BufferManager bufferManager;
    arm::pipe::SendCounterPacket sendCounterPacket(bufferManager, "ArmNN", "Armnn 25.0", "");

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

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

    CHECK(readBuffer);
    CHECK(readBufferSize > 0u);

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

    CHECK(basePipeServer.get()->WaitForStreamMetaData());
    CHECK(basePipeServer.get()->GetStreamMetadataPid() == arm::pipe::GetCurrentProcessId());
    CHECK(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();

    CHECK(readBuffer);
    CHECK(readBufferSize > 0u);

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

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

    CHECK(!packet1.IsEmpty());
    CHECK(packet1.GetPacketFamily() == 0);
    CHECK(packet1.GetPacketId() == 4);
    CHECK(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);

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

    socketProfilingConnection.Close();
}

}