-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_socket.cc
97 lines (85 loc) · 2.84 KB
/
test_socket.cc
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
#include <string>
#include "homa_socket.h"
#include "mock.h"
#include "util.h"
// This file contains unit tests for homa_socket.cc and homa_socket.h.
class TestSocket : public ::testing::Test {
public:
HomaSocket sock;
TestSocket()
: sock(nullptr)
{
Mock::setUp();
}
~TestSocket()
{
}
/**
* Return a human-readable string describing all of the saved buffer
* info in a HomaSocket.
* \param sock
* Socket whose saved buffers should be scanned.
*/
std::string getSaved(HomaSocket *sock)
{
std::string result;
for (uint32_t offset: sock->savedBuffers) {
char buffer[100];
if (!result.empty()) {
result.append(" ");
}
snprintf(buffer, sizeof(buffer), "%d", offset);
result.append(buffer);
}
return result;
}
};
TEST_F(TestSocket, saveBuffers) {
struct homa_recvmsg_args recvArgs;
recvArgs.bpage_offsets[0] = 0;
recvArgs.bpage_offsets[1] = 1;
recvArgs.bpage_offsets[2] = 2;
recvArgs.num_bpages = 3;
sock.saveBuffers(&recvArgs);
EXPECT_STREQ("0 1 2", getSaved(&sock).c_str());
// Should be idempotent.
sock.saveBuffers(&recvArgs);
EXPECT_STREQ("0 1 2", getSaved(&sock).c_str());
}
TEST_F(TestSocket, getSavedBuffers) {
struct homa_recvmsg_args recvArgs;
for (uint32_t i = 0; i < 20; i++) {
recvArgs.bpage_offsets[0] = 2*i + 1;
recvArgs.num_bpages = 1;
sock.saveBuffers(&recvArgs);
}
EXPECT_EQ(20U, sock.savedBuffers.size());
// First attempt: limited by space in control.
recvArgs.num_bpages = 0;
sock.getSavedBuffers(&recvArgs);
EXPECT_EQ(static_cast<uint32_t>(HOMA_MAX_BPAGES), recvArgs.num_bpages);
EXPECT_EQ(1U, recvArgs.bpage_offsets[0]);
EXPECT_EQ(3U, recvArgs.bpage_offsets[1]);
EXPECT_EQ(static_cast<uint32_t>(2*HOMA_MAX_BPAGES - 1),
recvArgs.bpage_offsets[HOMA_MAX_BPAGES-1]);
// Second attempt: limited by available buffers.
recvArgs.num_bpages = 0;
sock.getSavedBuffers(&recvArgs);
EXPECT_EQ(static_cast<uint32_t>(20 - HOMA_MAX_BPAGES), recvArgs.num_bpages);
EXPECT_EQ(static_cast<uint32_t>(2*HOMA_MAX_BPAGES + 1), recvArgs.bpage_offsets[0]);
EXPECT_EQ(static_cast<uint32_t>(2*HOMA_MAX_BPAGES + 3), recvArgs.bpage_offsets[1]);
// Third attempt: no available buffers.
recvArgs.num_bpages = 0;
sock.getSavedBuffers(&recvArgs);
EXPECT_EQ(0U, recvArgs.num_bpages);
}
TEST_F(TestSocket, getSavedBuffers_argsAlreadyHasSomeBuffers) {
struct homa_recvmsg_args control;
sock.savedBuffers.push_back(10);
sock.savedBuffers.push_back(11);
control.num_bpages = 3;
sock.getSavedBuffers(&control);
EXPECT_EQ(5U, control.num_bpages);
EXPECT_EQ(10U, control.bpage_offsets[3]);
EXPECT_EQ(11U, control.bpage_offsets[4]);
}