memgraph/tests/unit/bolt_common.hpp
Matej Ferencevic 017e8004e8 Refactor network stack
Summary:
Previously, the network stack `communication::Server` accepted connections and
assigned them statically in a round-robin fashion to `communication::Worker`.
That meant that if two compute intensive connections were assigned to the same
worker they would block each other while the other workers would do nothing.

This implementation replaces `communication::Worker` with
`communication::Listener` which holds all accepted connections in one pool and
ensures that all workers execute all connections.

Reviewers: buda, florijan, teon.banek

Reviewed By: buda

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1220
2018-02-22 16:29:17 +01:00

94 lines
2.1 KiB
C++

#include <array>
#include <cstring>
#include <functional>
#include <iostream>
#include <random>
#include <vector>
#include <glog/logging.h>
#include "gtest/gtest.h"
/**
* TODO (mferencevic): document
*/
class TestSocket {
public:
explicit TestSocket(int socket) : socket_(socket) {}
TestSocket(const TestSocket &) = default;
TestSocket &operator=(const TestSocket &) = default;
TestSocket(TestSocket &&) = default;
TestSocket &operator=(TestSocket &&) = default;
int id() const { return socket_; }
bool Write(const uint8_t *data, size_t len, bool have_more = false,
const std::function<bool()> & = [] { return false; }) {
if (!write_success_) return false;
for (size_t i = 0; i < len; ++i) output.push_back(data[i]);
return true;
}
void SetWriteSuccess(bool success) { write_success_ = success; }
std::vector<uint8_t> output;
protected:
int socket_;
bool write_success_{true};
};
/**
* TODO (mferencevic): document
*/
class TestBuffer {
public:
TestBuffer(TestSocket &socket) : socket_(socket) {}
void Write(const uint8_t *data, size_t n) { socket_.Write(data, n); }
void Chunk() {}
bool Flush() { return true; }
private:
TestSocket &socket_;
};
/**
* TODO (mferencevic): document
*/
void PrintOutput(std::vector<uint8_t> &output) {
fprintf(stderr, "output: ");
for (size_t i = 0; i < output.size(); ++i) {
fprintf(stderr, "%02X ", output[i]);
}
fprintf(stderr, "\n");
}
/**
* TODO (mferencevic): document
*/
void CheckOutput(std::vector<uint8_t> &output, const uint8_t *data,
uint64_t len, bool clear = true) {
if (clear)
ASSERT_EQ(len, output.size());
else
ASSERT_LE(len, output.size());
for (size_t i = 0; i < len; ++i) EXPECT_EQ(output[i], data[i]);
if (clear)
output.clear();
else
output.erase(output.begin(), output.begin() + len);
}
/**
* TODO (mferencevic): document
*/
void InitializeData(uint8_t *data, size_t size) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 255);
for (size_t i = 0; i < size; ++i) {
data[i] = dis(gen);
}
}